PDA

View Full Version : while inside of while



pkcidstudio
04-27-2007, 10:01 PM
I was looking on php.net, and it mentioned that you could do a (while inside of while)
<?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
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

mburt
04-27-2007, 10:58 PM
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.

pkcidstudio
04-28-2007, 12:32 AM
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?

nwalton
04-28-2007, 04:55 AM
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:


$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).

pkcidstudio
04-30-2007, 01:47 PM
$num = mysql_numrows($query);
This is what I have for $num should it be different?

soloWebDev
05-01-2007, 06:15 PM
what is your main query?

pkcidstudio
05-01-2007, 06:17 PM
Any thoughts on why this would be hanging up? Everything I've read said this should work.




$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(

0 => 'filetype0',

1 => 'filetype1',

2 => 'filetype2',

3 => 'filetype3',

4 => 'filetype4',

5 => '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();

nwalton
05-01-2007, 08:19 PM
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).

pkcidstudio
05-01-2007, 08:34 PM
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.

pkcidstudio
05-02-2007, 04:49 PM
- solved -