Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    mat
    mat is offline
    Regular Coder
    Join Date
    Jul 2002
    Posts
    199
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Grouping results under category headings

    I am wanting to to group my results by category (under that category heading)

    Code with simplified html:
    PHP Code:
    <? while ($vacancies mysql_fetch_assoc($vacancies_result)) { ?>

        <? if ($current_category != $vacancies['category']) { $current_category $vacancies['category'?>
            <strong><? echo $current_category ?></strong>    
        <? ?>

        <br>
        <? echo $vacancies['title'?>

    <? ?>

    What I am looking for is something like this:

    Transport
    bus driver
    taxi driver


    But i'm getting:

    Transport
    bus driver

    Transport
    taxi driver





  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to check if
    $vacancies['category']

    is differnt from the $vacancies['category']) of the previous record (which you store in a variable that after the comparison is set to $vacancies['category']), and only print the break and category when it's different.

    More info and examplecode:
    http://www.codingforums.com/showthre...light=previous
    http://www.codingforums.com/showthre...light=previous
    http://www.codingforums.com/showthre...light=previous

  • #3
    mat
    mat is offline
    Regular Coder
    Join Date
    Jul 2002
    Posts
    199
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, thanks. checked the examples and I think i understand better now, yet I must still missing something as I still get the same result with the following code:

    PHP Code:
        <? $previous_cat "x"?>
        <? while ($vacancies mysql_fetch_assoc($vacancies_result)) { ?>
            
            <? if ($previous_cat != $vacancies['category']){ ?>
                <br><strong><? echo $vacancies['category']  ?></strong>
            <? ?>
                
            <br>
            <? echo $vacancies['title'?> 

            <? $previous_cat $vacancies['category']; ?>
            <? echo $previous_cat ?>
        <? ?>

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php 
    $previous_cat 
    "x"
    while (
    $vacancies mysql_fetch_assoc($vacancies_result)) { 
          if (
    $previous_cat != $vacancies['category']){ 
                echo (
    '<br /><strong>' $vacancies['category'] .'</strong>');
          } 
          echo (
    '<br />' .  $vacancies['title'] );
          
    $previous_cat $vacancies['category'];
    }
    ?>
    should work just fine.

    Oh yeah, in case you didn't realise --> you need to have an 'ORDER BY category ' clause in your selectstatement.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •