Echo a variable with $i PHP - php

I am trying to make a dynamic for loop with some database connection.
<?php for ($i = 1; $i <= 8; $i++): ?>
<?php echo $question[0]->option
<?php endfor; ?>
Where the option is in database stored like this: option1, option2, option3 etc..
I have the $i variable which does the counting, but I do not know how I put that into the $question[0]->option variable. Tried $question[0]->option,$i etc, but no luck.

Try this:
<?php
for ($i = 1; $i <= 8; $i++) {
$num = "option".$i;
echo $question[0]->$num;
}
?>

You don't have to open and close the php tags in a block of only php code.
<?php
for ($i = 1; $i <= 8; $i++):
$optionname = "option$i";
echo $question[0]->$optionname
endfor;
?>

Related

how to display cycles in php added elements to each cycle in php?

I need your help to solve this problem, I am trying to write the necessary code so that the output is as follows:
PHP! PHP!! PHP!!! PHP!!!! PHP!!!!!
<?php
$y = ['!'];
for($i = 1; $i <= 5; $i++)
{
print "PHP!"."$y\t";
if($y+1 <5)
$y="!";
$y+'!';
}
?>
Until the second word, it adds one more exclamation point and in the rest of the repetitions it has the same number, that is two exclamation points.
How can I solve this problem?
Try this:
<?php
for ($i = 0; $i < 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
You can try it:
<?php
for($i = 1; $i <= 5; $i++)
{
echo "PHP";
for($j = 1; $j <= $i; $j++){
echo "!";
}
}
?>
You can also use str_repeat():
<?php
for ($i = 1; $i <= 5; $i++) {
echo "PHP".str_repeat ("!", $i);
}
?>
Output will be: PHP!PHP!!PHP!!!PHP!!!!PHP!!!!!

Create several DIV containers using for loop in PHP

Just wanted to ask how can I (if it is possible at all) create several DIV containers using for loop in PHP. Thank You.
for ($i = 0; $i < 20; $i++) {
echo '<div>$i</div>';
}
<?php for ($i = 0; $i < 20; $i++) { ?>
<div><?= $i; ?></div>
<?php } ?>
<?php for ($i = 0; $i < 20; $i++): ?>
<div><?= $i; ?></div>
<?php endfor; ?>
If we're really going down this silly road...
echo "<ul>";
foreach(array_fill(0, 999, 'why!?') as $key => $really) {
echo "<li>$key: $really</li>";
}
echo "</ul>";

Running for loop inside other for loop

Can anyone help me understand why a variable takes its initial value after incrementing the variable? below is code:
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
echo $k+3;
echo $l+3;
}
In this we have two for loops running one inside other. Here we run three times the outside for loop, inside this we are running other for loop again. The problem we are facing is that when inner for loop end we have incremented $k and $l both by 3 but it always take value 0 and 3 respectively.
we have incremented $k and $l both by 3
Nope, you only print the result of your values plus 3, but you do not set them anywhere in the loop:
Instead of
echo $k+3;
echo $l+3;
write
$k = $k + 3;
$l = $l + 3;
You should try removing the "echo" and incrementing the variable in each loop. Then print them out after.
Try this:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
$j = $j++;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
?>
Gives you:
9
12
TRY this.
$k += 3;
$l += 3;
echo $k;
echo $l;
Try This
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
First Increment the value and store it in the variable
$k = $k+3;
$l = $l+3;
Then You need to print using
echo $k.'<br>';
echo $l;
#Harinarayan First of all you need to read about echo() http://php.net/manual/en/function.echo.php
echo — Output one or more strings
echo does not manipulate expression as you did in your question like:
echo $k+3;
instead of using echo for the increment you should first increment the variable and then echo that variable like below:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k += 3;
$l += 3;
echo $k;
echo "<br>";
echo $l;
}

php undefined offset in for loop

This has baffled me for quit a while. I want to compare my data with the data coming next to determine when to change the row.
<?php
$seasons; // laravel eloquent model from controller
$i = 0;
$max = count($seasons);
for($i; $i<$max; $i++):
$x = $i+1;
print_r($seasons[$i]); // ok
print_r($seasons[1]); // ok
print_r($seasons[0+1]); // ok
print_r($seasons[$x]); // undefined
print_r($seasons[$i+1]); // undefined
endfor;
?>
<?php
$seasons; // laravel eloquent model from controller
$i = 0;
$max = count($seasons);
for($i; $i<$max; $i++):
$x = $i+1;
print_r($seasons[$i]); // ok
print_r($seasons[1]); // ok
print_r($seasons[0+1]); // ok
if(isset($seasons[$x])){
print_r($seasons[$x]); // undefined
}
endfor;
?>
By the way, $x and $i+1 these both lines are same. because $x = $i + 1; and again you are doing $i + 1.
i over look that $x could be over the array index limit at the last loop, so i put in a check to solve the problem. if someone else have better way of doing it, let me know too.
$i = 0;
$max = count($seasons);
for($i; $i<$max; $i++):
$x = $i+1;
if($x < $max):
if($seasons[$i]->month != $seasons[$x]->month)
// do something
endif
endif;
endfor;

PHP Array, Get every 4 results and output in a loop

I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)

Categories