Jump to content

[SOLVED] For Loop adding too many?


Lamez

Recommended Posts

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?

Link to comment
Share on other sites

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! :D

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";
?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.