Forum Moderators: coopster

Message Too Old, No Replies

PHP function not working

         

urbley

4:38 pm on Nov 22, 2004 (gmt 0)

10+ Year Member



Perhaps someone could look at this and tell me why it isn't working... cos I havent the feintest idea! =( It's really all I have left to do on something i've been working on for a long time now. Would be greatful for any help given. As to what's wrong... it's not showing the rows that should contain my products details... i'e not running the showProducts() function

Thanks

Stephen

Here is the code for the entire page...

<?

if(isset($_COOKIE["myLoggedInCookie"])){

echo "

<html>

<head>

<title>View All Products</title>

<style type='text/css'>

table { background-color:#eeeeee; }
th { background-color:#cccccc; font-family:verdana; font-size:12pt; color:#333399; border-width:2px; border-color:#3333ff; }
td { background-color:#eeeeee; font-family:verdana; font-size:12pt; color:#333399; border-width:1px; border-color:#3333ff; }

</style>

</head>

<body>

<center>

<br/><br/>

<table width='600'>
<tr>
<th colspan='5'>Products</th>
</tr>
<tr>
<th>Section</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>Image Url</th>
</tr>
<?php
showProducts();
?>

</table>

<br /><br />

</center>

</body>
</html>

";

}

else {

echo "<script>alert('You are not logged in'); parent.location='index.php';</script>";

}

function showProducts(){

$db = mysql_connect("localhost", "username", "password");
mysql_select_db("databaseName");

$dbQuery="SELECT * FROM products order by section asc";
$result = mysql_query($dbQuery,$db);

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

$section=$arrayVar["section"];
$name=$arrayVar["name"];
$price=$arrayVar[price];
$desc=$arrayVar["desc"];
$imgurl=$arrayVar["imgurl"];

echo "<tr><td>$section</td>" .
" <td>$name</td>" .
" <td>$price</td>".
" <td>$desc</td>".
" <td>$imgurl</td></tr>";
}
}

?>

Robber

5:36 pm on Nov 22, 2004 (gmt 0)

10+ Year Member



after a quick glance it looks like you have <?php
showProducts();
?> sitting in the middle of a print (or was it echo) statement

Maybe try changing it:

echo "blah".showProducts()."blah";

dreamcatcher

9:30 pm on Nov 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, try using return in your function:

Change:

echo "<tr><td>$section</td>" .
" <td>$name</td>" .
" <td>$price</td>".
" <td>$desc</td>".
" <td>$imgurl</td></tr>";

to:

$data = "<tr><td>$section</td>" .
" <td>$name</td>" .
" <td>$price</td>".
" <td>$desc</td>".
" <td>$imgurl</td></tr>";
return $data;

urbley

10:26 am on Nov 23, 2004 (gmt 0)

10+ Year Member



this place never fails to please! thanks a lot fellas. Nothing more frustrating than almost being finished... and just not having the will to even try and work it out for your self! =P

Thanks again

Steve