1-7 for loop only works with 1, not 2+?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

1-7 for loop only works with 1, not 2+?

Post by JAB Creations »

The following for loop gets stuck at '1'. Skipping 1 and starting at 2 and other things hasn't helped. It just won't do anything from 2 or greater. This for loop is killing me right now. What have I goofed up in the logic? :|

Code: Select all

for ($every = 1; $every <= 7; $every += 1)
 {
 $cat = 'cat_'.$every;
 if (isset($_POST[$cat]))
  {
   $mysql_game_add_tag = mysql_query("INSERT INTO `gamer`.`game_category` (`category_id`, `game_id`) VALUES ('".$_POST['cat_'.$every]."', '".$game_id."')");
   if (!$mysql_game_add_tag) {$_SESSION['status'] = '<b>Error occurred adding tags for a game to the database:</b> '. mysql_error(); header("location:".$truepage."?error"); exit();}  
   else {return "success";}
  }
  else {return "success";}
 }
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: 1-7 for loop only works with 1, not 2+?

Post by JAB Creations »

Here is a test file I've done...and it seems to work fine there though not in my OP and I'm just not seeing the problem...

Code: Select all

<?php
for ($every = 1; $every <= 7; $every += 1)
{
 $cat = 'cat_'.$every;
 if (isset($_POST[$cat])) {echo '<div>'.$cat.'</div>';}
}
?>
 
<form action="" method="post">
<fieldset>
<div><label for="cat_1"><input checked="checked" class="checkbox" id="cat_1" name="cat_1" type="checkbox" value="1" />Action</label></div>
<div><label for="cat_2"><input checked="checked" class="checkbox" id="cat_2" name="cat_2" type="checkbox" value="2" />Action</label></div>
<div><label for="cat_3"><input checked="checked" class="checkbox" id="cat_3" name="cat_3" type="checkbox" value="3" />Action</label></div>
<div><label for="cat_4"><input checked="checked" class="checkbox" id="cat_4" name="cat_4" type="checkbox" value="4" />Action</label></div>
<div><label for="cat_5"><input checked="checked" class="checkbox" id="cat_5" name="cat_5" type="checkbox" value="5" />Action</label></div>
<div><label for="cat_6"><input checked="checked" class="checkbox" id="cat_6" name="cat_6" type="checkbox" value="6" />Action</label></div>
<div><label for="cat_7"><input checked="checked" class="checkbox" id="cat_7" name="cat_7" type="checkbox" value="7" />Action</label></div>
</fieldset>
 
<fieldset>
<legend>Options</legend>
<div><input type="submit" value="Add this game now!" /></div>
</fieldset>
</form>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: 1-7 for loop only works with 1, not 2+?

Post by JAB Creations »

Nevermind! I was returning in the middle of the loop, DOH! :|

...maybe too much Red Bull. :mrgreen:
Post Reply