Variable restriction

Hi guys,

I’m just started using php + sql and I really have a problem.
I’ve written a piece of code which should display some products with correlating images and description (out of a database), corresponding to ONE product category.

This is my code:

[code][php]<?php

$cat=2;
$limit=20;
$i=2;

while($i<="$limit"){

$res=mysql_query(“select * from testdb WHERE id=$i AND cat=$cat”) or die(mysql_error());
$row=mysql_fetch_array($res);
$prijs=$row[“prijs”];
$descr=$row[“descript”];
$naam=$row[“naam”];
$prodnr=$row[“id”];
$i++;
echo ‘

<img src="190/’;echo"$prodnr";echo’.jpg" alt=“Blikjes” />
’;

echo’

<h1 id="’;echo"$prodnr";echo’">’;echo"$naam";echo’
’;echo"$descr";echo’
Prijs:’;echo’ €’;echo"$prijs";echo’ Productnummer: ‘; echo"$prodnr";echo’
’;

}

?>[/php][/code]

Problem is: There are gaps between the list of products shown. The gap is caused by the fact that for a certain $i value there is no corresponding $prodnr (since there are a few gaps in my db) or the fact that the product with that $i value as id doesn’t belong to the chosen category. How can I make the code so that it only displays products corresponding with the chosen category.

Thanks in advance!!!

$res=mysql_query("select * from testdb WHERE cat=$cat ORDER BY id") or die(mysql_error());
while($row=mysql_fetch_array($res)){

You query the database for only category, order it by id to keep it in sequence.

Sponsor our Newsletter | Privacy Policy | Terms of Service