If anyone could help me out with this function I wrote on a thumbnail display code for my site. The function is supposed to show specific thumbnails from a table in my database. For instance if you click on business category it would display all the thumbnails from the table with the category business in them.
My code is below.
function template_display() {
$results4 = mysql_query("SELECT * FROM templates ORDER BY tempid DESC");
while($row4 = mysql_fetch_array($results4)) {
$name = $row4['name'];
$price = $row4['price'];
$uniq_price = $row4['uniq_price'];
$designer = $row4['designer'];
print "<table width='400' border='0' cellspacing='0' cellpadding='0' align='center'>
<tr>
<td width='150' valign='top'>
<div align='center'><img src='/files/templates/thumbs/$name.gif' border='1'><a href='preview.php?name=$name'><img src='/images/button_preview.gif' width='59' height='14' border='0'></a>
<a href='cart.php?txt_itemcode=$tempid&txt_mode=addition_new'><img src='images/button_addtocart.gif' width='77' height='14' border='0'></a>
</div>
</td>
<td width='10'> </td>
<td valign='top'>
<table width='100%' border='0' cellspacing='1' cellpadding='1'>
<tr>
<td><b>Name: </b>$name</td>
</tr>
<tr>
<td><b>Price: $</b>$price</td>
</tr>
<tr>
<td><b>Unique: $</b>$uniq_price</td>
</tr>
<tr>
<td><b>Designer: </b>$designer</td>
</tr>
</table>
</td>
</tr>
</table><br><br>";
}
}
When I use this code it will display the thumbnails the way I want, but it will display all the thumbnails in the database. No matter what category I click on it displays all the thumbs from the database.
Can someone please help me order my thumbs by $catid. I have tried different things but they all seem to not work.
Any help is greatly appreciated.
Thanks
Matt Urban
Help with Accessing Data from MySQL DB with PHP Function
Moderator: General Moderators
results4 = mysql_query("SELECT * FROM templates WHERE category = '".$catid."' ORDER BY tempid DESC");
Using the line of code above with the WHERE clause added, I get no errors produced from it but still nothing will display now when i click on a link such as cataglog.php?catid=5 and there should be 3 thumbnails shown under that category.
Please help?
Thanks for the time
Using the line of code above with the WHERE clause added, I get no errors produced from it but still nothing will display now when i click on a link such as cataglog.php?catid=5 and there should be 3 thumbnails shown under that category.
Please help?
Thanks for the time
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Have you passed $catid into the function? Or do you access it as a superglobal? If you haven't there's a couple of things you could do. If you are using PHP version 4.1 or greater you can reference catid as $_GET['catid'] - but I would do some validation on it before putting it into the SQL statement (e.g. if you expect it to be a number check it with is_numeric()). Or if you've already done that before you call the function just declare the variable global in the function:
You might also want to consider moving your SQL statements out of the call to mysql_query() and into a variable so that you can echo them to make sure variables are being passed as expected. E.g.:
then if things aren't going as expected you can just do:
to check that the SQL statement looks as it should.
Mac
Code: Select all
function template_display() {
global $catid;
// rest of function
}Code: Select all
$sql = "SELECT * FROM templates WHERE category = '".$catid."' ORDER BY tempid DESC";
$results4 = mysql_query($sql);then if things aren't going as expected you can just do:
Code: Select all
echo $sql;Mac