Page 1 of 1

function showdata () {

Posted: Sun Oct 24, 2004 2:17 am
by Jim_Bo
I dont even know if I can use the function () to do what I want .. At the moment I have say 20 pages .. the only thing that changes is the query .. I want to beable to create a page as shown below .. adding all 20 querys to the one page .. So its easy to maintain and obviously dont have 20+ pages to deal with.. Then call back any query by just using a link ie: http://www.blabla.com/products.php?action=lawnmower

Am I on the right track .. can function () acheive this?

Code: Select all

<?php

function showdata (){

echo "<br><center><font face="verdana" size="1" color="black">$totalEntries Products Listed</center></font><br>";

if($row["links"]!=NULL){
$url_display="<img src=".$row["links"].">";
}else{
$url_display="No Preview";
}

echo "<table width="90%" height="146" border="1" align="center" cellpadding="5" cellspacing="1" bordercolor="000000">
              <tr> 
                <td height="18" colspan="4" valign="middle" bgcolor="#0099CC"> 
                  <div align="left"><color="FFFFFF"><strong>$row[product]</strong></div></td>
              </tr>
              <tr> 
                <td width="20%" rowspan="4"><div align="center">$url_display</td>
                <td width="80%" height="89" colspan="3" valign="top" background="../square.bmp">$row[desc]</td>
              </tr>
              <tr> 
                 <td width="147" nowrap background="../square.bmp" div align="center"><font face="verdana" size="1">PART No: $row[name]</font></div></td>
                <td width="151" height="10" nowrap background="../square.bmp" div align="center"><font face="verdana" size="2" color="FF0000">$$row[price]</font></div></td>
                <td width="147" nowrap background="../square.bmp" div align="center"><font face="verdana" size="1"><a href="http://www.blabla.com/cart.php?action=add_item&id=$row[itemId]&qty=1">ADD 
                    ITEM</a> - <a href="http://www.blabla.com/cart.php">VIEW CART</a></font></div></td>
              </tr>
            </table><br>";
} 
// --------------------------------------------------------------------------------- lawnmower
function lawnmower (){

include("db.php");
    
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$result = mysql_query("select * from items where filter='lawnmower'");
$totalEntries = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {

showdata ();

   }
}

// --------------------------------------------------------------------------------- weedeater
function weedeater (){

include("db.php");

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName); 
$result = mysql_query("select * from items where filter='weedeater'");
$totalEntries = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {

showdata ();

   }
}

?>
When I goto url http://www.blabla.com/products.php?action=lawnmower .. I get no errors .. and a blank page .. when using the entire code in one page .. with out using funtion () it works fine.

Can anyone help push me in the right direction to acheive this?

Cheers

Posted: Sun Oct 24, 2004 2:22 am
by m3mn0n
The variable scope is all wrong.

You need to intake variables within the function's arguments, and then for global variables set within an include or whatever, you need to use global to identify to the PHP parser that you want that variable used within the function.

It is explained better, and with more detail here: http://php.net/variables.scope

Posted: Sun Oct 24, 2004 2:45 am
by Jim_Bo
Hey,

I am like a total noobie .. and cant make alot of sence of the examples in that link .. well not to intergrate it into what I have .. would someone be kind enough to get it to display one of the variables to get the loop started to help me make some sence of it .. I manage to get a table to display .. but no data displays in the table .. but the $totalEntries count works ..

Arrg .. sux to be a noobie

Cheers

Posted: Sun Oct 24, 2004 2:57 am
by m3mn0n
Well instead of integrating those examples into your script right away, just practice in seperate files so you can better understand the concept of variable scope and how to use it properly.

Then setting up proper variable scope within your script should be a snap. :)

Posted: Sun Oct 24, 2004 8:26 am
by John Cartwright
Tip 1. Your functions names should be relevant to its content, ie. your function weedeater() is no good :P

Posted: Mon Oct 25, 2004 3:36 am
by Jim_Bo
:arrow:

Posted: Mon Oct 25, 2004 3:57 am
by Jim_Bo
Hey Sami,

Thanks for the advice ..

After some hours and gain of more grey hairs .. lol .. :idea: .. I think I have it figured out .. Makes it harder for me to understand when the examples at php.net (php in general) arnt in english .. err as in they use math I guess .. which makes it harder for me personally to figure it out :oops:

Cheers