SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot thetzfreak's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What's wrong with this code? Not all rows being displayed

    Hey, I'm having some trouble displaying a query result using a particular format. I have this code:

    PHP Code:
    $result mysql_query("SELECT * FROM list ORDER BY category");
    if( 
    mysql_num_rows($result) > ) {
        while(
    $row mysql_fetch_array($result)) {
            
    $catsData[$row["category"]][] = $row;
        }
    }
    $i 0;
    foreach(
    $catsData as $k => $v){
        
    //$k = category name
        //$v = array of rows
        
    echo $k."<br />";

        foreach (
    $v as $kk => $vv) {
            
    //$vv = individual row
            
    echo "--".$vv['title']."<br />";
        }

    As you can see, I'm trying to display items under their corresponding categories, like so:

    Category 1
    --item
    --item
    --item

    Category 2
    --item
    --item

    Problem is, only the first item in each category is being displayed, not all of them. Why is this? It seems that only the first row in each category is being recorded into the array. Why is this?

    Thanks a lot.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use print_r() on your arrays to see what they contain, at different points in your code.

  3. #3
    SitePoint Zealot thetzfreak's Avatar
    Join Date
    Aug 2004
    Location
    United States
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    crmalibu,

    I already did, and I did notice that not all rows are being added to the array (only the first row). However, the problem is I can't figure out why. I don't know the source of the problem.

    Thanks.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2009
    Location
    Maidenhead, UK
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did you try to dump $catsData to see what it ends up being?

    try:
    PHP Code:
    print_r($catsData); 
    Also a suggestion to simplify your code:

    PHP Code:
    $result mysql_query("SELECT * FROM list ORDER BY category");
    $currentCategory '';
    if( 
    mysql_num_rows($result) > ) {
        while(
    $row mysql_fetch_array($result)) {
            if (
    $currentCategory != $row["category"]) {
                 echo 
    $row['category'].'<br />';
                 
    $currentCategory $row['category'];
            }
            echo 
    $row['title'].'<br/>;
        }

    -------------------------------------------------------
    UK postcode->address lookup : www.CraftyClicks.co.uk
    a piece of my brain : adam.stylo.name (at your own risk)
    -------------------------------------------------------

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if( mysql_num_rows($result) > ) { 
        while(
    $row mysql_fetch_array($result)) {
            
    print_r($row);
            
    $catsData[$row["category"]][] = $row
        } 
    }
    print_r($catsData); 
    You don't really need to check mysql_num_rows(). If theres no rows, the while loop will never run.

  6. #6
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This has been solved already.. link.


Bookmarks

Posting Permissions

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