Jump to content

PHP to display products/categories | revisited: strange error


matto1376

Recommended Posts

Buddski helped out yesterday displaying data from a mySQL query.

Here is the run down - the mySQL pulls out something like this:

 

Cat Name 1        Product Name1

Cat Name 1        Product Name2

Cat Name 2        Product Name3

Cat Name 2        Product Name4

Cat Name 2        Product Name5

Cat Name 3        Product Name6

Cat Name 3        Product Name7

 

 

And using PHP, I wanted to display this:

 

Cat Name 1        Product Name1

                          Product Name2

Cat Name 2        Product Name3

                          Product Name4

                          Product Name5

Cat Name 3        Product Name6

                          Product Name7

 

With Buddski's help, I got this far:

 

 

                          Product Name2

Cat Name 2        Product Name3

                          Product Name4

                          Product Name5

Cat Name 3        Product Name6

                          Product Name7

 

 

Notice it omits the very first mySQL row.....and I am not sure why.

 

In Dreamweaver where I constructed the query, if I test it, all rows return as you would expect.

If I output results through a normal repeat region and dynamic text, also no problem.

 

 

Here is the mySQL query:

$colname_rsProductRange = "-1";
if (isset($_GET['productRange'])) {
  $colname_rsProductRange = (get_magic_quotes_gpc()) ? $_GET['productRange'] : addslashes($_GET['productRange']);
}
mysql_select_db($database_connPriefert, $connPriefert);
$query_rsProductRange = sprintf("SELECT ItemID, productRange, productRangeTitle, ItemName, ItemThumb FROM items WHERE productRange = %s AND visible = 'Yes' ORDER BY ItemID ASC", GetSQLValueString($colname_rsProductRange, "text"));
$rsProductRange = mysql_query($query_rsProductRange, $connPriefert) or die(mysql_error());
$row_rsProductRange = mysql_fetch_assoc($rsProductRange);
$totalRows_rsProductRange = mysql_num_rows($rsProductRange);
?>

 

And here is the Buddski's code:

<div id="products">     
<?php 
		$old_cat = null;
while ($data = mysql_fetch_assoc($rsProductRange)) {
if (is_null($old_cat) || $old_cat != $data['productRangeTitle']) {
	echo '<div class="levelFourTitleBox" style="background-image:url(../images/Products/thumbs/'.$data['ItemThumb'].')"><span class="levelFourTitle">'.$data['productRangeTitle'].'</span></div>';
	$old_cat = $data['productRangeTitle'];
}
echo '<a href="productDetail.php?ID='.$data['ItemID'].'">'.$data['ItemName'].'</a>     ';
}
?>
</div>

 

 

This displays a category name, then lists the items in that category without repeating the category name. The only hiccup is that first row return that disappears.

I know I am doing something wrong but I am not sure what!!

 

Thanks in advance guys...

 

 

 

 

 

With a simplified code example, would this not work?

 

$last='';
$query = mysql_query("SELECT catname,product FROM table ORDER BY catname, product asc");
while($row = mysql_fetch_assoc($query)){
$catname = $row['catname'];
$product = $row['product'];


if($last != $catname){
echo "<h2>$catname</h2>";
}
echo "<p>$product</p>";

$last = $catname;

}

Trap for young players....

 

I found this on another forum, it worked:

because you grab the first row here

$row = mysql_fetch_row($query);

 

so when it gets here

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

 

it is always on the second row

 

maybe try mysql_data_seek before your fetch array to reset the pointer

 

 

So I added this line before I called the recordset:

mysql_data_seek($query,0) ;

 

Disco.

 

Thanks all.

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.