SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help me with this loop?

    I have the following while loop that displays a subregion name and then all of the bands in that subregion:

    PHP Code:
    $old_subregion_name ''
     while (
    $row mysql_fetch_array($result)) {
     
    $subregion_name $row["subregion_name"];
     
    $band_name $row["band_name"];
     
    $display_page $row["display_page"];
     
    $name_prefix $row["name_prefix"];
     
    $band_id $row["band_id"];
        if (
    $old_subregion_name != $subregion_name) {
           
    $old_subregion_name $subregion_name;
     echo 
    "<br><b>$subregion_name</b><br><br>";
        } 
     if (
    $display_page=='yes') {
     echo 
    "<a href=\"bands.php?id=$band_id\">";
     }
     if (
    $name_prefix) {
     echo 
    "$name_prefix ";
     }
     echo 
    "$band_name";
     if (
    $display_page=='yes') {
     echo 
    "</a>";
     }
     echo 
    "<br>";
     
     } 
    However, I've run into a bit of problem in that not all bands have been assigned a subregion. What I'd like is for all bands without a subregion to be listed at the end after a few <br> tags, only I can't for the life of me figure out where in this loop it should go. Any ideas?
    Daniel

    http://www.dfbpunk.com

  2. #2
    SitePoint Zealot manoloweb's Avatar
    Join Date
    Aug 2003
    Location
    Mexico
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wondering...

    PHP Code:
    $old_subregion_name ''
    //Holder for NO SUBREGION bands
    $nu_sub=array();
    while (
    $row mysql_fetch_array($result)) {
    //Open new IF to check subregion
    if ($row["subregion_name"]=="") {
    // Push the band in the $no_sub Array
    $no_sub[]=$row;
    // Else you just do it as you were doing it
    } else {
     
    $subregion_name $row["subregion_name"];
    $band_name $row["band_name"];
    $display_page $row["display_page"];
    $name_prefix $row["name_prefix"];
    $band_id $row["band_id"];
        if (
    $old_subregion_name != $subregion_name) {
         
    $old_subregion_name $subregion_name;
    echo 
    "<br><b>$subregion_name</b><br><br>";
        } 
    if (
    $display_page=='yes') {
    echo 
    "<a href=\"bands.php?id=$band_id\">";
    }
    if (
    $name_prefix) {
    echo 
    "$name_prefix ";
    }
    echo 
    "$band_name";
    if (
    $display_page=='yes') {
    echo 
    "</a>";
    }
    echo 
    "<br>";

    // Close if subregion
    }
    echo 
    "<br><br><br>";
     
    // Now display the bands with no sub region
    if (count($no_sub)) {
    echo 
    "Bands with no sub region:<br>";
    foreach (
    $no_sub as $row) {
        
    $band_name $row["band_name"];
        
    $display_page $row["display_page"];
        
    $name_prefix $row["name_prefix"];
        
    $band_id $row["band_id"];
     
        if (
    $display_page=='yes') {
            echo 
    "<a href=\"bands.php?id=$band_id\">";
        }
        if (
    $name_prefix) {
            echo 
    "$name_prefix ";
        }
        echo 
    "$band_name";
        if (
    $display_page=='yes') {
            echo 
    "</a>";
        }
        echo 
    "<br>";
    }


  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what you mean by "holder" and "push." Do you have an example?
    Daniel

    http://www.dfbpunk.com

  4. #4
    SitePoint Zealot manoloweb's Avatar
    Join Date
    Aug 2003
    Location
    Mexico
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually the code is ready to run.

    I just commented so you can know what I did.

    ;-)

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm... that gives me the exact same results as the code I posted earlier. Is it possible that maybe the results aren't being pulled into the no_sub array properly?
    Daniel

    http://www.dfbpunk.com

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This line is definitely the problem:

    PHP Code:
    if ($row["subregion_name"]=="") { 
    when I change it to this:

    PHP Code:
    if ($row["subregion_name"]) { 
    All of the bands get pushed into the no_sub array. So does anyone have any ideas about why the syntax in the first example isn't pulling out the bands with no subregion listed?
    Daniel

    http://www.dfbpunk.com

  7. #7
    SitePoint Zealot manoloweb's Avatar
    Join Date
    Aug 2003
    Location
    Mexico
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, ME


    ;-)


    It is because I was comparing the field to an empty string, but what it does contain when no sub-region is recorded is actually a NULL value.

    My mistake


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
  •