I want to read 5 attributes of the database. The 5 attributes have the names post_image_1, post_image_2, post_image_3, post_image_4 and post_image_5. Now I want to show the 5 images on my page, with a for loop.
Here is the loop:
for($i = 1; $i <= 5; $i++){
echo "<img src='image/$row[post_image_$i].png' height='250px' width='250px'>";
}
Now I get an error:
Parse error: syntax error, unexpected '$i' (T_VARIABLE), expecting ']' in
I hope that's enough info to help me. :P
Since you're using arrays, do this:
for($i = 1; $i <= 5; $i++){
echo '<img src="image/'.$row['post_image_'.$i].'png" height="250px" width="250px">';
}
Try this, using string concatenation, it gets at the right field in the array. I'm assuming $row is declared already and contains the keys 'post_image_1', 'post_image_2'...
for($i = 1; $i <= 5; $i++){
echo "<img src='image/" . $row['post_image_' . $i] . ".png' height='250px' width='250px'>";
}
Related
I am trying to increment two separate numbers in the same for loop. For example I want the first number to be:
0,2,4,6,8,10 etc
I want the second number to be:
1,3,5,7,9 etc
I have tried what was suggested here but could get it to work:
How to increment a number by 2 in a PHP For Loop
My code so far:
$count = count($toyList);
echo $count;
for($i=0; $i<$count; $i++){
$json = '{"toy": {"toyname":"' . $toyList[$i+2] . ',"status":"' . $toyList[$i+3] . '"}}';
}
Any help would be greatly appreciated. Thanks.
Introduce an additional variable and increase that inside the loop after you have done with it whatever you intend to do. I don't have an environment for PHP at hand, but it should look something like this:
$count = count($toyList);
echo $count;
$j = 1;
for($i=0; $i<$count; $i++){
$json = '{"toy": {"toyname":"' . $toyList[$i+2] . ',"status":"' . $toyList[$i+3] . '"}}';
$j = $j + 2;
}
i am trying to print 2 4 6 8 10 with spaces, but it prints 246810.
for($i = 1; $i<= 5; $i++){
echo $i * 2.' ';
}
i am getting error : "This page isn’t working"
This might help.
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
try:
for ($i = 1; $i <= 5; $i++) {
echo ($i * 2) . ' ';
}
the main reason that your code fails is that the 2 is connected with a dot .
php interprets the 2 as an invalid type as it expects a digit next to it. e.i. 2.0
separating the 2 with a space or in the example with a parenthesis (to explicitly tell the logic) will make the code work
Problem:-2.-> php interprets the 2 as an invalid type because it is expecting a digit next to it something like 2.0. when you add space that simply tell it to apply multiplication first and then add space with it.
1.Either add a space before . like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo $i * 2 .' ';
}
Output:-https://eval.in/856709
2.Or put multiplication into bracket like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
Output:-https://eval.in/856710
Try this,
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
Very new to PHP. I have sucessfully got this piece of code working to display 4 images from a directory, however it shows the first 4 images by name (001.png, 002.png, 003.png and 004.png) which are the lowest numbers and happen to be the least recently uploaded:
<?php
$pictures = glob("directory/*.png");
for( $i=0; $i<=3; $i++ ){
echo "<img src=\"".$pictures[$i]."\" />";
}
?>
I am looking to change that to get the 4 most recently uploaded in a directory by name.
In other words, I would like to display the last 4 images with the highest number.
I have tried this below however I am getting Parse error: syntax error, unexpected T_VARIABLE on line 4
<?php
$pictures = glob("directory/*.png");
$no_pictures = count($pictures)-1
$limit = $no_pictures-3
for( $i = $no_pictures; $i >= $limit; $i--; ){
echo "<img src=\"".$pictures[$i]."\" />\n";
}
?>
Any help is appreciated. Thanks for your time.
You are missing the ending semicolons ; on lines 3 & 4 [edit] AND you have an extra one in your for loop after $i-- -
<?php
$pictures = glob("directory/*.png");
$no_pictures = count($pictures)-1; // was missing ;
$limit = $no_pictures-3; // was missing ;
for( $i = $no_pictures; $i >= $limit; $i--){ // removed ; after $i--
echo "<img src=\"".$pictures[$i]."\" />\n";
}
?>
I have a table and each row contains, among other things, 5 columns which may or may not contain an image file name. Let's say I've retrieved that row and put it into an assoc array. I want to loop through and echo those image file names (cols may or may not all be populated) into html tags, but only if that column has an image file name in it. Is there a better way to do it than this?
for ($i = 1; $i < 6; $i++){
if($item_array['image_' . {$i}]{
echo "<li><img src=\"images/work-items/$item_array['image_' . {$i} . '.jpg'\"/></li>"
}
Your syntax was all over the place:
for ($i = 1; $i < 6; $i++){
if(isset($item_array['image_' . $i])){
echo '<li><img src="images/work-items/'. $item_array['image_' . $i] . '.jpg"/></li>';
}
}
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++