Page 1 of 1
Simple Counter causing it to skip "1". Why is this?
Posted: Mon Aug 15, 2011 9:14 am
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.
Re: Simple Counter causing it to skip "1". Why is this?
Posted: Mon Aug 15, 2011 9:22 am
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++;
Re: Simple Counter causing it to skip "1". Why is this?
Posted: Mon Aug 15, 2011 9:28 am
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!!
Re: Simple Counter causing it to skip "1". Why is this?
Posted: Tue Aug 16, 2011 4:54 am
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.