Lamez Posted August 20, 2008 Share Posted August 20, 2008 ok I am working on my football pool website, and I am having it where the admin can add bowls, so I have a page where you enter how many bowls so here is the for loop after that question: <?php $num = $_POST['bowl_num']; if ($num == (0)){ header("Location: ?".$get."=".$start_over); } echo '<form method="post" action="?'.$get.'='.$add.'" enctype="multipart/form-data">'; echo '<table width="100%" border="0">'; for ($counter = 1; $counter <= $num; $counter++) for ($counter = 1; $counter <= $num; $counter++) { ?> <tr> <td width="10">Bowl Number: <?php echo $counter; ?></td> <td width="10"><input name="<?php echo $counter; ?>" type="file" class="form" id="<?php echo $counter; ?>"></td> </tr> <?php }//end for loop echo ' <tr> <td colspan="2"><label> <input type="submit" name="bowl_up_con" id="bowl_up_con" value="Continue" /> </label></td> </tr> </table>'; echo '</center>'; $_SESSION['num'] = $counter; $_SESSION['up'] = true; ?> so then when the admin clicks continue, it takes them to this for loop: <?php $num = $_SESSION['num']; if($_SESSION['up'] !== (true)){ header("Location: ?".$get."=".$start_over); } for ($counter = 1; $counter <= $num; $counter++) { $image = $_POST[$counter]; $id = $counter; $date = ""; $network = ""; $place = ""; $query = "INSERT INTO `foot_bowls` (id, name, image, date, network, place) VALUES ('".$id.",' '".$name."', '".$image."', '".$date."', '".$network."', '".$place."')"; mysql_query($query); }//end loop echo "Added ".$counter." bowls"; ?> Well you see at the end there where it says: Added ".$counter." bowls"; well if I set 10 bowls it comes out to be 12, it is adding 2 for some reason, anybody know why? Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/ Share on other sites More sharing options...
Lamez Posted August 20, 2008 Author Share Posted August 20, 2008 also it is adding 1 more row than is should, and echoing out 2 more than it should. Why!? It made it look all funky, maybe this helps you help me! First For loop: <?php echo '<center>'; $num = $_POST['bowl_num']; if ($num == (0)){ header("Location: ?".$get."=".$start_over); } echo '<form method="post" action="?'.$get.'='.$add.'" enctype="multipart/form-data">'; echo '<table width="100%" border="0">'; for ($counter = 1; $counter <= $num; $counter++) { ?> <tr> <td width="10">Bowl Number: <?php echo $counter; ?></td> <td width="10"><input name="<?php echo $counter; ?>" type="file" class="form" id="<?php echo $counter; ?>"></td> </tr> <?php }//end for loop echo ' <tr> <td colspan="2"><label> <input type="submit" name="bowl_up_con" id="bowl_up_con" value="Continue" /> </label></td> </tr> </table>'; echo '</center>'; $_SESSION['num'] = $counter; $_SESSION['up'] = true; ?> Second For loop: <?php $num = $_SESSION['num']; if($_SESSION['up'] !== (true)){ header("Location: ?".$get."=".$start_over); } for ($counter = 1; $counter <= $num; $counter++) { $image = $_POST[$counter]; $id = $counter; $name = "OH-YA !"; $date = "not_set"; $network = "this"; $place = "Texas"; $query = "INSERT INTO `foot_bowls` (id, name, image, date, network, place) VALUES ('".$id."', '".$name."', '".$image."', '".$date."', '".$network."', '".$place."')"; mysql_query($query)or die(mysql_error()); }//end loop echo "Added ".$counter." bowls"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-620899 Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 Sounds silly but, where is your session_start()? Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-620908 Share on other sites More sharing options...
Lamez Posted August 20, 2008 Author Share Posted August 20, 2008 it is in my head.php which is not shown. I could show the whole code if needed. Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-620928 Share on other sites More sharing options...
Lamez Posted August 20, 2008 Author Share Posted August 20, 2008 bump. Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621151 Share on other sites More sharing options...
php_dave Posted August 20, 2008 Share Posted August 20, 2008 I cant see why it would be 2 more but it makes sense that it is 1 more. Your for loop is checking that the value if $counter is equal or less than $num.. if it is not then break the loop - that means the final value of $counter is 1 more than $num. As your loop can only loop through to the value of $num (there is no break condition) you might as well just display Added ".$num." bowls. Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621168 Share on other sites More sharing options...
Lamez Posted August 20, 2008 Author Share Posted August 20, 2008 I sill do not understand, if you read one of my last topics, I am having a hard time figuring it all out. So do I need to make it a equal sign instead? Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621391 Share on other sites More sharing options...
akitchin Posted August 20, 2008 Share Posted August 20, 2008 you need to adjust it to be <, rather than <=. that should solve the issue of displaying one more row than necessary, and if you make sure you change both loops, should solve the insertion of 2 more. there's no reason to change $_SESSION['num'] to $counter, since $num already contains what you want. that's what was causing the 2 insertion; at the end of the first for() loop, $counter was 1 greater than $num, and you're going over THAT by 1 in the second for loop as well. you compound the error by assigning $_SESSION['num'] to $counter. Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621399 Share on other sites More sharing options...
Lamez Posted August 20, 2008 Author Share Posted August 20, 2008 well if I change it to < then it displays one less If I want 2 bowls it only shows 1! Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621424 Share on other sites More sharing options...
akitchin Posted August 20, 2008 Share Posted August 20, 2008 then leave it as <= (i figured that was what was necessary), but leave $_SESSION['num'] as $num or $_POST['num']. setting it to $counter will give you an incorrect starting number for the second for loop. Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621435 Share on other sites More sharing options...
Lamez Posted August 20, 2008 Author Share Posted August 20, 2008 oh ok gotcha, well some how I made a never ending loop, with a mysql query and it max my 100,000 connections, so I have to wait an hour , man I am stupid Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621439 Share on other sites More sharing options...
akitchin Posted August 20, 2008 Share Posted August 20, 2008 that's why it's always best practice to construct your queries within loops, but not execute them until you're sure the loop is working as it should. Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621440 Share on other sites More sharing options...
Lamez Posted August 20, 2008 Author Share Posted August 20, 2008 Ya, it is a lesson learned. for loops are the most complex loops in PHP. From the manual! http://us3.php.net/for Quote Link to comment https://forums.phpfreaks.com/topic/120491-solved-for-loop-adding-too-many/#findComment-621446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.