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>';
}
}
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;
}
Is it possible to control a for-loop when it reaches a certain condition?
Explanation:
I'm retrieving the folder path to a collection of images from a database: these images are then printed out via a for-loop. What I would need to do is control how these images are displayed on the page (say, 5 images per row).
As of now, the for-loop prints out 40 images in a single row, which makes you scroll to the furthest right of the page.
Is there a solution for controlling the for-loop, as in for instance, after 5 successful loops, echo out a < br >? Here's a vulgar thought:
for ($i = 1; $i < $rows; $i++) {
$path = $image[$i];
$folder_path = $path['folder_path']; //since it's an array
echo '<img src="' . $folder_path . '">';
//pseudocode
if ($i == 5) {
echo '<br>';
...continue with the loop
}
}
I know the pseudocode looks crazy, but that's pretty much what I need to do: loop for x amount of instances, add something, then continue.
As per #m69's comment, the best option would be to use the % (modulus) comparison operator. As explained by the PHP docs:
$a % $b returns the remainder of $a divided by $b.
So, in your case:
for ($i = 0; $i < $rows; $i++) {
$path = $image[$i];
$folder_path = $path['folder_path']; //since it's an array
echo '<img src="' . $folder_path . '">';
if ($i % 5 == 0) { //do this if $i divided by 5 has a remainder of 0
echo '<br>';
}
}
As a side note, you should set $i to 0 at the beginning of your for loop, assuming $rows is set to the number of rows returned from your query. Setting it to 1 will keep it from iterating through the last row, because $i will == 40 (assuming 40 rows), and so will NOT be < 40.
The same loop. The condition for inserting the is ($i % 5 == 0), which means (if this element is the fifth one of his series) will be useful for you.
<?php
for ($i = 1; $i < $rows; $i++) {
$path = $image[$i];
$folder_path = $path['folder_path'];
echo '<img src="' . $folder_path . '">';
if ($i % 5 == 0) {
echo '<br>';
}
}
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'>";
}
for ($i=1; $i<=4; ++$i) {
echo "The number is " . $i . "\n";
}
This will output:
The number is 1
The number is 2
The number is 3
The number is 4
How can i make a loop that will give me output like so:
The number is 1
The number is 1
The number is 1
The number is 1
The number is 2
The number is 2
The number is 2
The number is 2
etc
Thanks for any help.
Without nested loops: this would suffice for a single loop.
for($i=0;$i<9*4;$i++)
{
echo "The number is ".(1+floor($i/4));
}
So you want
for ($i=1; $i<=2; ++$i) {
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
echo "The number is " . $i . "\n";
}
But let's avoid the repetition with a loop!
for ($i=1; $i<=2; ++$i) {
for ($j=1; $j<=4; ++$j) {
echo "The number is " . $i . "\n";
}
}
Essentially, you want to print something four times inside the loop... so you can write four echo statements. A better way to do this would be to use nested for loops.
for ($i=1; $i<=4; ++$i) {
for ($j=1; $j<=4; ++$j) {
echo "The number is " . $i . "\n";
}
}
For every iteration of the outer loop, the inner one prints the statement four times. One thing to be careful with nested loops is the variables used in the conditions. If you mix them up, you could have weird issues including an infinite loop.
You need two nested loops, like so:
for( $i = 1; $i <= 4; ++$i) {
for( $j = 1; $j <= 4; ++$j) {
echo "The number is " . $i . "\n";
}
}
One of a million of possible solutions could be using single loop and str_repeat() function.
for ($i=1; $i<=4; $i++)
echo str_repeat("The number is $i\n", 4);
which is probably the best way to make multiple repeats of same string.
you can make two loops
for($i = 1; $i <= 4; $i++) {
for($j = 1; $j <= 4; $j++) {
echo 'The number is '.$i."\n";
}
}
You've got the same loop, but four iterations within:
for ($i=1; $i<=4; ++$i) {
for($j=0;$j<4;$j++) {
echo "The number is " . $i . "\n";
}
}
I randomized a set of questions using the following code:
for($i=0; $i < count($nwi); $i++)
$itemorder[$i] = $i;
shuffle($itemorder);
$_SESSION["itemorder"] = $itemorder;
A few pages later, a portion of the questions are presented:
for ($i=0; $i<40; $i++) {
if ($i % 10 ==0) echo $header;
echo '<tr class="';
if(in_array($itemlabel[$_SESSION["itemorder"][$i]], $errors)) echo 'skip';
elseif ($i % 2 == 0) echo 'even';
else echo 'odd';
echo '">';
echo '<td>' . ($i + 1) . ".</td><td>" . $itemtext[$_SESSION["itemorder"][$i]] . '</td>';
for ($j = 1; $j <= 6; $j++) {
echo "\n" . '<td';
if ($j == 6) echo ' style="background-color:#999;"';
echo '><input type="radio" name="';
echo $itemlabel[$_SESSION["itemorder"][$i]];
echo '" value="';
echo $j; //value of the input
echo '"';
if ($_POST[$itemlabel[$_SESSION["itemorder"][$i]]] == $j) echo " checked";
echo '></td>';
}
At the end of the survey, I am trying to put the answers to the questions (which should range in value from 1-8) into my SQL database:
"INSERT INTO surveydata
(id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....
VALUES
('$_SESSION[id]', '$_SESSION[itemorder][0]',
'$_SESSION[itemorder][1]', '$_SESSION[itemorder][2]',
'$_SESSION[itemorder][3]',...
I am getting only zeros in my SQL database regardless of how I answer the questions. Any suggestions?
Well, first I don't see anything assigning anything to the session values, but the issue with your code is in this pattern: '$_SESSION[itemorder][1]'. First, I would make sure that MySQL is expecting a varchar there and not an int. If it is an int, good form would be to make sure it isn't quoted.
More importantly, though, when you have an associative array in PHP, you need to make sure PHP expects that.
This
$a = array("hi"=>array("world"=>0)); echo "$a[hi][world]";
Outputs
Array[world]
Put braces around the lookup to make sure it knows to treat it as an array, and then put quotes around all string indexes:
// note the braces and quotes
$a = array("hi"=>array("world"=>"here")); echo "{$a["hi"]["world"]}";
Outputs
"here"
But, I wonder if you wouldn't be better off just using implode:
$columns = implode(',', $_SESSION['itemorder']);
$query = "INSERT INTO surveydata
(id, agree_pppg_01,agree_pppg_02,agree_pppg_03,....
VALUES ('{$_SESSION["id"]}', $columns )";
I do feel obliged to point out that that system does not seem scaleable, and column names like agree_pppg_02 are not descriptive. You may want to go to the codereview stackexchange site to see if they can't offer tips on database design.