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>";
Related
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!!!!!
I'm not sure but how to simply add current session id with a link/download links ?
<?php if($images) { $i=0; for ($index = 0; $index < count($images); $index++){ $i++ ?>
Download<br/>
<?php }}?>
For example:
<?php if($images) { $i=0; for ($index = 0; $index < count($images); $index++){ $i++ ?>
Download<br/>
<?php }}?>
I got value using this:
$count = <?php $details->shop_image_count; ?>
I want to use this $count in my for loop, but I am not getting the value of count
<?php
for($i=1;$i<($count);$i++){
echo '<li data-target="#shopCarousel" data-slide-to="' . $i . '"></li>';
}
?>
You should use something like this
$count = $details->shop_image_count;
instead of
$count = <? echo $details->shop_image_count;?>
Try this
$count = $details->shop_image_count;
Insted of this
$count = <? echo $details->shop_image_count;?>
And your for loop
<?php
for ($i=1; $i<$count; $i++){
echo '<li data-target="#shopCarousel" data-slide-to="'.$i.'"></li>';
}
?>
No need to close php in the loop. Make your code more readable and maintainable, like below:
<?php
for ($i = 1; $i <= $details->shop_image_count; $i++)
echo '<li data-target="#shopCarousel" data-slide-to="' . $i . '"></li>';
?>
Further, you start with 1, so you want to loop while $i <= $details->shop_image_count
$count = <? echo $details->shop_image_count;?> is not correct syntax.
Try
<?php
$count = $details->shop_image_count;
for($i=1; $i<($count); $i++)
{
?>
<li data-target="#shopCarousel" data-slide-to="<?php echo $i;?>"></li>
<?php
}
?>
$count = $details->shop_image_count;
Then use this method for better application
for($i=1;$i<(intval($count));$i++){
?>
<li data-target="#shopCarousel" data-slide-to="<? echo $i?>"></li>
<?php
}
?>
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++)
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;
?>