Results 1 to 10 of 10

Thread: while inside of while

  1. #1
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default while inside of while

    I was looking on php.net, and it mentioned that you could do a (while inside of while)
    PHP Code:
    <?PHP
    $i
    =0;
    while(
    $i count($one)) {
       
       while(
    $a each($two)) {
           echo 
    $a[1]." - ".$one[$i].", "
       }
       
    $i++;
       
    }
    ?>
    but when I try this it crases my system. no good i know. I am wondering if anyone has any experiance with this?

    piece of my code with what i am trying to do

    PHP Code:
         <?php 
          
    echo "<form name='namestoupdate' method='post' action='update.php'>\n";
          
    $i 0;
          while (
    $i $num) {
          
           
    $id mysql_result($query,$i,"id");
           
    $title mysql_result($query,$i,"title");
           
    $filetype mysql_result($query,$i,"filetype");
           
    $document mysql_result($query,$i,"document");
           echo 
    "<tr>";
           echo 
    "<input type='hidden' name='id[$i]' value='$id'>";
           echo 
    "<td>".$id."</td>";
           echo 
    "<td><input type='text' size='20' name='title[$i]' value='$title'></td>";

           

           
    //This is where I want the second while stmt to go
           
    echo "<td><select name=filetype>";
           echo 
    '<option value="'.$filetype[$i].'" selected="'.$filetype.'">'.$filetype.'</option>';
           echo 
    "</select></td>";



           echo 
    "<td><input type='text' size='20' name=document[$i]' value='$document></td>";

           echo 
    "</tr>";
              
               ++
    $i;
          }
                     
          echo 
    "<input type='submit' value='submit'>";
          echo 
    "</form>";
          
    mysql_close();
         
    ?>


    Thanks in advance

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    while loops are repeat until the statement returns false, and since no values are assigned it repeats forever. However, on most servers the PHP software has a limit to these loops, and should keep the browser from crashing.
    - Mike

  3. #3
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mburt View Post
    while loops are repeat until the statement returns false, and since no values are assigned it repeats forever. However, on most servers the PHP software has a limit to these loops, and should keep the browser from crashing.
    Thanks mburt, That makes sence. Do you have any experiance with what I am trying to accomplish? Any tips or tutorials or could you help me complete my task?

  4. #4
    Join Date
    Apr 2007
    Location
    Phoenix, AZ
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
    while ($i < $num) { ...
    You may have it somewhere else, but from what I can see, you don't have "$num" defined anywhere. That means there's no place for your while loop to stop. If you give "$num" a number, you'll set a limit to the number of times the loop will iterate. For instance:

    Code:
    $num = 50;
    $i = 0; 
    while ($i < $num) { 
    
         // some code to execute
    
         i++
    }
    This would execute the code in the block 50 times (from i = 0 to i = 49).

  5. #5
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    $num = mysql_numrows($query);
    This is what I have for $num should it be different?

  6. #6
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    what is your main query?

  7. #7
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Any thoughts on why this would be hanging up? Everything I've read said this should work.

    PHP Code:

         $query 
    "SELECT DISTINCT* FROM $tbl_name ORDER BY filetype ASC"

         
    $result mysql_query($query);

         if (!
    $result) {

          echo 
    "Could not successfully run query ($query) from DB: " mysql_error();

          exit;

         }

         
    $num mysql_numrows($result); 

    echo 
    "<form name='namestoupdate' method='post' action='update.php'>\n"

    $ArrayOfFileTypes = array( 

               
    => 'filetype0'

               
    => 'filetype1'

               
    => 'filetype2',

               
    => 'filetype3'

               
    => 'filetype4',

               
    => 'filetype5'

               
    ); 

         
    $i 0

         while (
    $i $num) { 

    $filetype mysql_result($result,$i,"filetype"); 

    echo 
    "<tr>";



         echo 
    "<td><select name=filetype>"

          for(
    $i 0$i count($ArrayOfFileTypes); ++$i) {

           echo 
    '<option value="'.$filetype[$i].'" selected="'.$filetype.'">'.$filetype.'</option>'

          }

          echo 
    "</select></td>"

          echo 
    "</tr>"



              ++
    $i

         } 

    echo 
    "<input type='submit' value='submit'>"

         echo 
    "</form>"

         
    mysql_close(); 

  8. #8
    Join Date
    Apr 2007
    Location
    Phoenix, AZ
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
    while($i < $num){
      ...
      
      for($i = 0; $i < count($ArrayOfFileTypes); $i++){
        ...
      }
    }
    I think the problem is that you're using the same variable to iterate both loops. If both the while and the for loops are trying to use $i, it seems bound to cause some problems. Pick a different var for the second loop and see what you get (and don't forget to change the variable references within that loop).

  9. #9
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That Worked!
    Ok, So now it is working but for some reason it is displaying the same option, the correct amount of times. Any thoughts of how I could fix that. It is selecting the correct value, just not offering the other options. Thanks in advance, this is the farthest I've gotten so far with this.

  10. #10
    Join Date
    Jun 2006
    Posts
    78
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    - solved -

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
  •