php itertation loop in hidden input not iterating - php

I've got one nagging little bug in this script. I'm going through my cart items and passing them into hidden inputs. The cart_id ($obj->id) is working fine into the value="" but my iteration loop that gives each value a unique name="" (cart_id_1, cart_id_2 etc) is NOT iterating.
<?php
$pass_cart_q = "SELECT c.id FROM carts AS c WHERE c.user_session_id='$sid'";
$result = $mysqli->query($pass_cart_q);
$i = 1;
while ($obj = $result->fetch_object()) {
echo "<input type=\"hidden\" name=\"cart_id_".$i."\" value=\" .$obj->id. \"><br>";
$i = $i++;
}
mysqli_close();?>
Each name field is coming through as cart_id_1

$i=$i++;
That's the problem just do:
$i++

Please replace $i = $i++; with just $i++.
$i = 1;
$i = $i++;
echo $i, "\n"; // 1
$i = 1;
$i = ++$i;
echo $i, "\n"; // 2
$i = 1;
$i++;
echo $i, "\n"; // 2
$i = 1;
++$i;
echo $i, "\n"; // 2

What $i = $i++ will cause it literally this: "make $i equal to $i and then increase it by one", but the $i will still remain the same. To solve this, simply replace $i = $i++; with $i++.
Manual Entry

you are assigning the incremented value to $i variable. and hence it is not able to iterate. instead you should remove that assignment variable $i and it should only be $i++

Related

Is my second for loop able to iterate using the increment from my first loop?

What I want is the first loop iterating from 1 to 4 and the second loop from 5 to 6.
Here is my code:
<?php
for ($i = 1 ; $i <= 4 ; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
for ($i = 1 ; $i <= 2 ; $i++)
{
echo $i . "<br>";
}
?>
The loops you've given are:
1st loop: from 1 to 4
2nd loop: from 1 to 2
First loop is ok, but seconds needs to be modified. Use $i<=6 and don't initialize $i variable.
This will give you:
1st loop: from 1 to 4
2nd loop: from (value that 1st loop have ended)+1 to 6, so (4+1) to 6, 5 to 6
<?php
$i = 0; // be sure 'i' is visible in both loops
for ($i=1; $i<=4; $i++) // form 1 to 4
{
echo $i . "<br>";
}
?>
<hr>
<?php
$i++; // start from 5, not 4
for (; $i<=6; $i++) // from the previous value to 6
{
echo $i . "<br>";
}
?>
Your problem
The second for loop resets your $i variable to 1:
for ($i = 1 ; $i <= 2 ; $i++)
Solution
You can use a while loop instead of your second for loop:
<?php
for ($i = 1; $i <= 4; $i++)
{
echo $i . "<br>";
}
?>
<hr>
<?php
while ($i <= 6) // `<= 6` instead of `<= 2`, since we keep $1's value
{
echo $i . "<br>";
$i++;
}
?>
Rather than using two loops for this, why not just output the <hr> tag at the appropriate point within the same one? If you carry on with adding extra loops, first of all you'll run into confusing problems like this about (re-)initialising variables, and you'll also quickly end up with a lot of unnecessary duplicated code.
You can use the PHP modulo operator (%) to output the <hr> tag after every fourth element, which will both reduce the complexity and be a lot more extensible if you later add more elements:
for ($i=1; $i<=6; $i++) {
echo $i . "<br>";
if ($i % 4 === 0) {
echo "<hr>";
}
}
See https://eval.in/976102

how to fix php for loop output displaying 1?

Here is my php code given below when I run it in my browser its display 1 continuously. My question is why its show like this? After execution its displays a fatal error : maximum execution time is exceeded. what is that?
<?php
for ($i=1; $i<=5; $i+1) {
echo $i." ";
}
?>
Give me proper answer. Make me sure that what is the execution time of php code?
TIA
$i+1 does not increment the value of $i. It only adds 1 to what is in $i but it does not assign it back to $i. Your loop does this:
$i = 1
while ($i<=5) {
echo $i." ";
$i+1;
}
$i+1 on it's own doesn't do anything.
You need something like $i = $i + 1. Or for short $i += 1. Or even shorter and better: $i++.
for ($i=1; $i<=5; $i++) {
echo $i . " ";
}
It is because of $i+1 in for loop. This is basically an expression and it produces a result but you never assign this result to $i. Therefore you would rather do something like $i = $i + 1 or, in real life, use incrementation $i++. So the final code will looks like:
for ($i = 1; $i <= 5; $i++) {
echo $i." ";
}
use $i++ not $i+1, $i++ is $i=$i+1
Change your code to
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
because first you must set value for $i
You need to do
<?php
for( $i = 1; $i <= 5; $i++) {
echo $i." ";
}
?>
Now you just say 1 + 1 but you don't assign it to anything. You could use $i = $i + 1 but it's the same as $i++
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
in a for loop, every turn you need to increase value of $i. but you forgot to increase value of $i. You wrote $i +1 but it needs to be assigned with new value of $i.
In short, you should change $i +1 to $i = $i +1 or $i ++
the right code:
<?php
for ($i=1; $i<=5; $i = $i+1) {
echo $i." ";
}
?>
You are not changing the value of $i in the loop. Either $i =$i +1 or $i++ instead of $i + 1 will do.
You've problem in printing the line. It should look like
<?php
for ($i=1; $i<=5; $i++) {
echo $i;
}
?>

PHP increment string value each iteration

I want to increment the int on the end of my string which together makes up the complete value.
$btnid = 'btnid1';
for($i = 1; $i < $countP; $i++) {
$btnid = 'btnid' . ++;
}
I tried different types of concatenation but I can't seem to get it to work if I just set it to 1 it works but I need the string there too.
Just append $i to the string btnid in each loop iteration.
$string = 'btnid';
for($i = 1; $i < $countP; $i++) {
$btnid = $string . $i;
}
As a side note (and seriously, DO NOT actually do this) so long as you don't go past btnid9, you can just increment the string.
$btnid = 'btnid1';
for($i = 1; $i < 9; $i++) {
$btnid++;
}
echo $btnid; // btnid9
If you go over, things get a bit weird:
$btnid++;
echo $btnid; // btnie0
Manual page: http://php.net/manual/en/language.operators.increment.php

PHP loop X amount of times

I have a string called $columns which dynamically gets a value from 1 to 7. I want to create a loop of <td></td> for however many times the value of $columns is. Any idea how I can do this?
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
Here's a more readable way to achieve this:
foreach(range(1,$columns) as $index) {
//do your magic here
}
If you just need to use number of repeat count:
for ($i = 0; $i < 5; $i++){
// code to repeat here
}
just repeat $n times? ... if dont mind that $n goes backwards...
the advantage is that you can see/config "times" at the beginning
$n = 5;
while (--$n >= 0)
{
// do something, remember that $n goes backwards;
}
I like this way:
while( $i++ < $columns ) echo $i;
Just bear in mind if $columns is 5, this will run 5 times (not 4).
Edit: There seems to be some confusion around the initial state of $i here. You are welcome to initialise $i=0 beforehand if you wish. This is not required however as PHP is a very helpful engine and will do it for you automatically (tho, it will throw a notice if you happen to have those enabled).
There is a str_repeat() function in PHP, which repeats a string a number of times. The solution for your problem would be:
str_repeat( '<td></td>', $columns );
If $columns is a string you can cast to int and use a simple for loop
for ($i=1; $i<(int)$columns; $i++) {
echo '<td></td>';
}
A for loop will work:
for ($i = 0; $i < $columns; $i++) {
...
}
You can run it through a for loop easily to achieve this
$myData = array('val1', 'val2', ...);
for( $i = 0; $i < intval($columns); $i++)
{
echo "<td>" . $myData[$i] . "</td>";
}
Why use logic at all, don't waste those CPU cycles!
<td colspan="<?php echo $columns; ?>"></td>

I need to identify a number stored in array

I have an array that storing numbers between 1 and 31. The numbers need to be echoed as a select drop down options.
$number = array();
echo '<select id=\'day\'>';
for ($i = 0; $i < 31; $i++)
{
$number[] = $i;
echo '<option value=\'{$number[]}\'>{$number[]}</option>';
}
echo '</select>';
You don't even need to build an array:
for ($i = 1; $i < 32; $i++) echo '<option value="'.$i.'">'.$i.'</option>';
You can use array_fill() to make the array (http://ca.php.net/array_fill)
Or just echo out the numbers as you iterate from 1->31.
Beyond that, wrapping your number in the appropriate HTML for a select box should work. Could you expand on the question?
<?php
print '<select name="foo">';
foreach($number as $n){
print "<option value=\"$n\">$n</option>;
}
print '</select>';
?>
Though as other posters pointed out, why bother building an array when you could just for($i = 1; $i < 32; $i++){ print "<option value=\"$n\">$n</option>"; }?

Categories