select group by order by, HELP ME please

Discussion in 'MySQL' started by draligus, Dec 8, 2007.

  1. #1
    i have a mysql database.
    table : articles
    title : titles of articles
    category : categories of articles
    text : text of articles

    i want to print this to my webpage with a single php code:

    category 1 (category)
    1. title 1 (title)
    .... text text text ........ (text)
    2. title 2 (title)
    .... text text text ........ (text)

    category 2 (category)
    1. title 1 (title)
    .... text text text ........ (text)
    2. title 2 (title)
    .... text text text ........ (text)

    category 3 (category)
    1. title 1 (title)
    .... text text text ........ (text)
    2. title 2 (title)
    .... text text text ........ (text)

    my code is this :
    <?
    $resultb = mysql_query("SELECT category, title, text FROM articles GROUP BY category ORDER BY title");
    $i=1;
    while ($resultat = mysql_fetch_array($resultb)) {
    $title=$resultat[title];
    $text=$resultat[text];
    $category=$resultat[category];
    ?>
    <b><?=$category?></b><br> &nbsp;• <?=$title?> <br> <?=$text?>
    <?
    $i++;
    $s++;
    }
    ?> 
    PHP:
    but i could not be successfull. please help me
     
    draligus, Dec 8, 2007 IP
  2. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't think that GROUP BY is what you are looking for then.. you are only going to get one row for each of the grouped results. How about something like this?:

    <?
    $lastcategory='';
    $categorycount=0;
    $titlecount=0;
    $hDb = mysql_connect("localhost","mysqluser","mysqlpass");
    mysql_select_db('test',$hDb);
    $resultb = mysql_query("SELECT category, title, text FROM articles ORDER BY category,title");
    $i=1;
    while ($resultat = mysql_fetch_array($resultb)) {
            $titlecount++;
            $title=$resultat[title];
            $text=$resultat[text];
            $category=$resultat[category];
            if ($category != $lastcategory) {
                    $titlecount=1;
                    $categorycount++;
                    $lastcategory = $category;
                    print "<br>category " . $categorycount . " (" . $category . ")<br>";
            }
            print $titlecount . ". " . $title . "<br>" . $text . "<br>";
            $i++;
            $s++;
    }
    ?>
    PHP:
    I think sorting by category then title and printing the category only when it has changed gets you closer to the results you are loooking for..
     
    drunnells, Dec 9, 2007 IP
    draligus likes this.
  3. draligus

    draligus Well-Known Member

    Messages:
    389
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #3
    thank you very very very much. i have asked this question in several forums. but you are the only one who could help me. thanks a lot.
     
    draligus, Dec 10, 2007 IP
  4. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No problem! Glad that could be of some help.
     
    drunnells, Dec 10, 2007 IP