Results 1 to 9 of 9

Thread: Proper use of a loop inside a while loop

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default Proper use of a loop inside a while loop

    Could someone please illustrate the best way to create a nested WHILE loop to do something like this? I am trying to learn best practices and the php & mySQL documentation is a bit abstruse. Thanks.

    Code:
    while($event = mysql_fetch_assoc($result)){
        while startdate stays the same { 
          echo day title; 
          echo events for this day; 
      }
    }
    Last edited by kuau; 08-03-2008 at 02:10 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Is this what you want:
    PHP Code:
    $startdateStatic $startdate
    while($event mysql_fetch_assoc($result)){
        while (
    $startdate==$startdateStatic) { 
         
    //comments
      
    }

    Where I put '//comments' I don't really understand what you want. I kinda do, but I can only do it with the fields.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    kuau (08-03-2008)

  4. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Dear Nile: Forget the loop for now. Can you please tell me why this query calculates incorrectly? There are actually zero records where approved = 0 yet $num displays as "1". It should be "0".

    Code:
    $sql = "SELECT COUNT(*) FROM event WHERE approved = 0 AND startdate >= '" . $today . "'";
    $result = mysql_query($sql,$connection) or die("Couldn't execute $sql query."); 
    $num = mysql_num_rows($result); ?>
    <b><?php echo $num;?></b></td>

  5. #4
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Sorry, I'm hardly any good at querys. And can't find out the problem.
    Jeremy | jfein.net

  6. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    OK no worries. How about this? I created a location table that fills a drop-down list on my form and for some reason it refuses to display a certain location, but it displays all the others no prob. I was wondering if it is because it is the only one that starts with a number, ie. 9th Life Sanctuary. Or maybe because it is the first one when sorted alphabetically. Could my code somehow skip the first record? Thanks.

    Code:
    $sql = "SELECT `l_id`, `location` FROM `location` WHERE `ok` = 1 ORDER BY location";	
    $result_loc = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
    $row = mysql_fetch_array($result_loc);
    while($row = mysql_fetch_row($result_loc)){	?>
      <option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
    <?php } ?>
    Last edited by kuau; 08-03-2008 at 04:40 AM.

  7. #6
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What certain location does it not show?
    Jeremy | jfein.net

  8. #7
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    9th Life Hawaii

  9. #8
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Does it have some sort of unique ID? Or will it just not display normally?
    Can I also see the whole code and if you can a link to the page.
    Jeremy | jfein.net

  10. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    602
    Thanks
    266
    Thanked 13 Times in 13 Posts

    Default

    Dear Nile: I figured it out!!! I ascertained that it was because the code was skipping the first record. It had nothing to do with the record itself, only the fact that it was first. And what caused it was because I fetched the row twice. This is now correct:

    Code:
    $sql = "SELECT `l_id`, `location` FROM `location` WHERE `ok` = 1 ORDER BY location";	
    $result_loc = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
    while($row = mysql_fetch_row($result_loc)){	?>
      <option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
    <?php } ?>
    Yahooooo!! Thanks. Maybe it is something similar with the mysql_num_rows($result);

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
  •