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
}
?>
Related
in PHP
for($i=1; $i <= 5; $i++){
$link = ''.$i.'';
}
?>
in HTML
<div class="goLeft">
<?php echo $link; ?>
</div>
When I echo the $link outside of the for loop, only 1 link is displaying. But when I echo the $link inside of the loop. All links is displaying.
Now you can get the variable outside the forloop
$link =array();
for($i=1; $i <= 5; $i++){
$link[] = ''.$i.'';
}
print "<pre>";
print_r ($link);
print "</pre>"; exit;
Link was getting overwritten for every iteration in your loop. You need to concat your links instead of overwritting them. You can do it as in the following snippet. Declare $link as an empty string and use .= to append your links to it. Then all your links will be displayed when you call echo $link
$link = "";
for($i=1; $i <= 5; $i++){
$link .= ''.$i.'';
}
Why not simply put html inside loop
for($i=1; $i <= 5; $i++){
$link = '<div class="goLeft">';
$link .= ''.$i.'';
$link .= '</div>';
echo $link;
}
Or if just need anchor in loop
<div class="goLeft">
for($i=1; $i <= 5; $i++){
echo ''.$i.'';
}
</div>'
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>";
I want to do this:
<?php echo $factuurgegevens->aantal.$i; ?>
But it doesn't work, how can I handle this?
So I have to get:
<?php echo $factuurgegevens->aantal1; ?>
<?php echo $factuurgegevens->aantal2; ?>
<?php echo $factuurgegevens->aantal3; ?>
<?php echo $factuurgegevens->aantal4; ?>
You can use curly braces ({}) to make up the dynamic variables:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
However you should really use an array because that is made for things like this:
$factuurgegevens->aantal = array();
$factuurgegevens->aantal[1] = 'something';
$factuurgegevens->aantal[2] = 'something';
$factuurgegevens->aantal[3] = 'something';
Do this:
$name = 'aantal';
for($i = 0 ; $i < 5 ; $i++)
echo $factuurgegevens->{$name . $i};
You can do this by placing your string in a variable.
for($i=0;$i<10;$i++)
{
$var = 'aantal'.$i;
echo $factuurgegevens->$var;
}
<?php
$i=0;
while($i < 101){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
?>
This code generates a table with 100 rows and 2 columns. But what I want to do is that show ordered numbers (upp to 100) in the left side of the rowcells and show something else (ex. pow(rownumber) ) in the right side of the rowcells. How can I do that?
Try this, Will output 100 rows with the number and its power in two columns
<table>
<?php
for($i = 0; $i <= 100; $i++){
echo sprintf('<tr><td>%s</td><td>%s</td></tr>',
$i,
pow($i, 2)
);
}
?>
</table>
Is this what you are looking for?
for($i=0; i<100; i++){
if($i%2==0){
echo "<tr>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>Something Else</td>".PHP_EOL;
if($i%2==0){
echo "</tr>".PHP_EOL;
}
}
You can use your modulus (and a for loop too)
for ($i = 1; $i <= 100; $i++){
$mod = ($i%2==0) ? true : false;
if($mod) echo "<tr>".PHP_EOL;
echo "<td>".$i."</td>".PHP_EOL;
echo "<td>". foo($bar) ."</td>".PHP_EOL;
if($mod) echo "</tr>".PHP_EOL;
}
<?php
$i=0;
$j=0;
while($i < 101){
if($i%2==0){
$j++;
echo "<tr>"."<td>".$j."</td>".PHP_EOL;
}
echo "<td>".$i."</td>".PHP_EOL;
$i++;
if($i%2==0){
echo "<td>any text</td>"."</tr>".PHP_EOL;
}
}
?>
Why use modulo for only two options? This solution seems much easier. Your $data is an array of the things you want to display, currently the alphabet.
$data = range('a','z');
foreach($data as $num => $elem) {
echo "<tr>".PHP_EOL;
echo "<td>".$num.</td>".PHP_EOL;
echo "<td>".$elem.</td>".PHP_EOL;
echo "</tr>".PHP_EOL;
}
If you want to make it loop 100+ times, just make the array that size.
Foreach documentation
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 6 months ago.
I have created a for loop.
I am trying to loop through lots of members and create a row for each member in a table.
It's looping through too many times.
Is it the right type of loop to use?
<?php
for ($i = 1; $i = count($u); $i++)
{
?>
<tr>
<?php
echo "<td>$i</td>";
?>
<?php
foreach ($u as $username)
{
echo "<td>$username</td>";
}
?>
<?php
foreach ($p as $points)
{
echo "<td>$points</td>";
}
?>
</tr>
<?
}
?>
$u and $p are arrays.
Thanks
You can shorten it a bit and remove the inner loops:
<php
for ($i = 1; $i <= count($u); $i++)
{
echo '<tr>';
echo "<td>$i</td>";
echo "<td>$u[$i]</td>";
echo "<td>$p[$i]</td>";
echo '</tr>';
}
?>
$i = count($u) in the loop is what's causing the problem:
for ($i = 1; $i = count($u); $i++)
On every iteration of the loop, you're assigning count($u) to $i using a single =. Should be
for ($i = 1; $i <= count($u); $i++)
or
for ($i = 0; $i < count($u); $i++)
<?php
$count = count($u);
for ($i = 1; $i < $count; $i++) {
echo '<tr>';
echo '<td>' . $i . '</td>';
foreach ($u as $username) {
echo '<td>' . $username . '</td>';
}
foreach ($p as $points) {
echo '<td>' . $points . '</td>';
}
echo '</tr>';
}
?>
You should count $u before you loop it.