tags:

views:

34

answers:

2

Hi!

I didn't get any code that worked. Of course I could have use then wrongly because I'm a beginner. Some told me to use MySQL subqueries other told me to use PHP foreach achieve it. What I want is to show the search results of a keyword separated by groups of categories, something like that:

Search results for Item, 3 itens in 2 categories:

Category 1:

  • Item 1
  • Item 10
  • Category 2:

    • Item 1003

    Can someone explain me it as simple as possible.
    Thanks n advance!

    +1  A: 

    I use a single request which return name of category for each item and I use PHP to display it

    <?php
        $cat;
        while($result = $statement->fetch()) {
            if($result['cat'] !== $cat) {
                $cat = $result['cat'];
                /* display cat */
            }
            /* display items */
        }
    ?>
    
    MatTheCat
    It just works! It's incredible! Thank you very much!
    tenshimsm
    Hope it's solved ? ^^
    MatTheCat
    Solved it flawlessly!
    tenshimsm
    From the FAQ : When you have decided which answer is the most helpful to you, mark it as the accepted answer by clicking on the check box outline to the left of the answer. This lets other people know that you have received a good answer to your question. Doing this is helpful because it shows other people that you're getting value from the community. (If you don't do this, people will often politely ask you to go back and accept answers for more of your questions!) ;)
    MatTheCat
    Thanks for the tip, again.
    tenshimsm
    +1  A: 

    You can use Group By in mysql

    http://www.plus2net.com/sql_tutorial/sql_having.php

    Or as you get the result in php you can loop it based on each row

    zod
    Thank you for your answer I'll try it.
    tenshimsm