Jump to content

How to Print single variable in while loop


golfer

Recommended Posts

I currently have a problem where I'm trying to list alot of products with the category heading only being returned once and then the long list of products within that category being listed.

For Example

 

Football boots

Nike

Addidas

Puma

etc

 

Whereas at the moment I'm getting.

Football boots

Nike

Football boots

Addidas

Football boots

Puma

 

My current code for this is as follows.

 

<?php while($row = $db->fetchrow($stuff)) { ?>

<li><a href="store-<?php echo $row['cat_id']; ?>/<?php echo seo_makeSafeURI($row['cat_title']); ?>.html"><strong><?php echo stripslashes(htmlentities($row['cat_title'])); ?></strong></a></li>

 

 

<li><a href="store-<?php echo $row['cat_id']; ?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><?php echo stripslashes(htmlentities($row['prod_title'])); ?></a></li>

<?php } ?>

 

Any help would be much appreciated.

 

 

Link to comment
Share on other sites

Think about this, how would a human perform this task? You would 'remember' the current category and only process a category heading when the category name changes.

 

How do computers 'remember' values? They store them in variables. How do computers test if a value changes? They use an if(){} conditional test.

 

$current_category = ''; // initialize to a value that will never exist as data
while($row = mysql_fetch_assoc($result)){
    // test if the category changed
    if($current_category != $row['cat_title']){
        // category changed (or it is the first one detected), output the heading here
       ....
       // remember the current category for the next test
       $current_category = $row['cat_title'];
    }
    // output the product data here
    ....
}

Link to comment
Share on other sites

That makes sense thankyou. I am however getting an error from my updated code. For the variable $current_category can you call this anything?

 

<?php

$current_category='cat';

while($row = $db->query($stuff)) {

if($current_category != $row['cat_title']){ ?>

<li><a href="store-<?php echo $row['cat_id']; ?>/<?php echo seo_makeSafeURI($row['cat_title']); ?>.html"><strong><?php echo stripslashes(htmlentities($row['cat_title'])); ?></strong></a></li>

 

<?php  $current_category=$row['cat_title']; ?>

 

<li><a href="store-<?php echo $row['cat_id']; ?>-<?php echo $row['prod_id']; ?>/<?php echo seo_makeSafeURI($row['prod_title']); ?>.html"><?php echo stripslashes(htmlentities($row['prod_title'])); ?></a></li>

<?php }

}?>

Link to comment
Share on other sites

The Sql is fine and does exactly what I need. The error is in the code I have changed to make the title only appear once. I will continue to change things to see what works.

 

Select * from

(products LEFT JOIN categories_products_link ON products.prod_id = categories_products_link.prod_id)

LEFT JOIN categories ON categories_products_link.cat_id = categories.cat_id;

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.