PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I figured that much myself, but thanks for your input anyway. The real problem (for me) is that results from one category can be listed on more than one page and the name/id of that category must be displayed on all these pages.
$category_name_list = Array( ... ); // List of all category names
$prev_category = -1; // Variable to check if category has changed
foreach ( $results as $item )
{
// If category is not the same as for previous item (or this is first item), then output category name
if ( $item['category_id'] != $prev_category )
{
$prev_category = $item['category_id'];
echo '<b>'.$category_name_list[$prev_category].'</b><br />';
}
// Output item
echo $item['product_name'].'<br />';
}
Sorry, me again. That is totally confusing to me. Could you please explain your code. I can't get this to work as expected. Is there any other way? Thank you
The comments are very self explanatory, although the reason it's not working is most likely because you have not ordered by `category_id` in your query. If ordering by category_id is not possible, what you want to do is put the results into a new array, using the category id as the key, then on each new iteration check for the existence of the key against the category id, and if exists simply add to the stack. If not, create a new key.
Khm, sorry to reopen this topic. I have one more question:
Now I have my items sorted by category and paginated as described above. What if I wanted my items inside each category to by futher sorted by name (alphabetically)? Thanks in advance!