Simple Counter causing it to skip "1". Why is this?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Simple Counter causing it to skip "1". Why is this?

Post by simonmlewis »

Code: Select all

$token = strtok($string,"|");
while($token) 
{ 
$count = ($count + 1);
            $accessories = mysql_query ("SELECT * FROM products WHERE id = '$token'");
            while ($accslist = mysql_fetch_object($accessories))
            { echo "
<input type='hidden' name='X"; echo $count; echo "include' value='on'>
<input type='hidden' name='X"; echo $count; echo "storeid' value='44175'>
<input type='checkbox' name='X"; echo $count; echo "itemcode' value='$accslist->code'>";
            }
$token = strtok("|");
}
I have this issue with a counter. I'm trying to make the first loop of $count be a 1, then a 2 on thesecond loop, then 3 and so on.

But each time it always starts at 2, and when I read it it's obvious why. Because $count is a maths sum.

How do I set it so $count starts at 1, and on the second loop it goes up by one each time?

I know it will be simple, but this is just foxxing me for hours.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: Simple Counter causing it to skip "1". Why is this?

Post by Dodon »

Does this help?

Code: Select all

$token = strtok($string,"|");
$count =0;
 while($token) 
{ 
$count = ($count + 1);
             $accessories = mysql_query ("SELECT * FROM products WHERE id = '$token'");
             while ($accslist = mysql_fetch_object($accessories))
             { echo "
 <input type='hidden' name='X"; echo $count; echo "include' value='on'>
 <input type='hidden' name='X"; echo $count; echo "storeid' value='44175'>
 <input type='checkbox' name='X"; echo $count; echo "itemcode' value='$accslist->code'>";
             }
 $token = strtok("|");
 }
 
first thing you do in the while loop is adding 1 to count, if count before the loop was 1 it'll be 2 as soon as you enter the loop thus first time.

btw an alternative for adding 1 to a variable is: $count++;
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Simple Counter causing it to skip "1". Why is this?

Post by simonmlewis »

It's still starting at "X2.....".
If I echo "$count" before the "While ($acclist...." script, it shows "1".
But if I put it in the next line of echo ", then it shows 2.
Is this because $count is a maths equasion, and therefore, whereever you put "$count", it immediately just adds one to it anyway.

Just guessing here, as my brain is fried!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: Simple Counter causing it to skip "1". Why is this?

Post by Dodon »

Code: Select all

$token = strtok($string,"|");
$count =0;  // Obviously count is 0 at this point
while($token) 
{ 
$count = ($count + 1); // first time you enter the loop it'll be 1, second time you enter the loop it'll be 2 etc
             $accessories = mysql_query ("SELECT * FROM products WHERE id = '$token'");
             /* If nothing was found in query it'll skip the next content of the while loop and moving on the the first while loop (while($token)) and adding 1 to $count again, resulting in $count being 2.*/ 
             while ($accslist = mysql_fetch_object($accessories)) 
             { echo "
 <input type='hidden' name='X"; echo $count; echo "include' value='on'>
 <input type='hidden' name='X"; echo $count; echo "storeid' value='44175'>
 <input type='checkbox' name='X"; echo $count; echo "itemcode' value='$accslist->code'>";
             }
 $token = strtok("|");
 }
 
So make sure the first time you enter the loop it actually finds something in the mysql query otherwise it'll just skip the second while loop and grabs a new $token. next it goes through the while loop and adds 1 to $count which results in $count being 2.
Post Reply