Correct php code for echoing html with variables - php

I try to echo some html code with php. But it doesn't work at all. i got two variables from a session and want to loop it to show it.
$anz is 10 and $fl is the file name.
<div id="magazine">
<?php
SESSION_START();
$fl = $_SESSION["filesx"];
$anz = $_SESSION["anz"];
$i = 0;
while ($i < $anz){
echo "<div style='background-image: url($fl+$anz.jpg);'></div>"
$i++;
}
?>
</div>
$fl is the file name and i need to add $anz and .jpg.
Because there are 10 files.

got it!
<?php
SESSION_START();
$fl = $_SESSION["filesx"];
$anz = $_SESSION["anz"];
$anz = $anz -1;
$i = 0;
$jpg = "jpg";
echo $fl.$anz.$jpg;
while ($i <= $anz){
$x = $fl.$i;
echo "<div style='background-image: url($x.$jpg);'></div>";
$i++;
}
?>
Thanks to all!

Related

How to put a variable name in for loop?

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
}
?>

Why this PHP code does not run - Code fitter Mail

I have code
require_once('verify.php');
$file = 'mail.txt';
$allfile = file($file);
for ($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i];
$dep = 'admin#ngocuong.net';
$kq = verifyEmail($cuong, $dep);
if($kq == '2') {
echo 'OK<br />';
} else {
echo 'Nope<br />';
}
}
echo 'Done!!!';
with $cuong = 'cuong.fl310#gmail.com'; code run. Return is OK.
But i put all my list email into file mail.txt. And run code read line in file txt. $cuong = $allfile[$i] code not run.
I had show
for($i = 0; $i <= 10; $i++) {
$cuong = $allfile[$i]
echo $cuong;
}
it print list email in file txt. With $i= 0; $cuong = 'cuong.fl310#gmail.com' But code not run. Return enqual: Nope !!!
Plz help me fix it.
Thanks for readding!!!

Naming PHP object children in for loop

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;
}

Using a array to print the result in a list

i just create a php file to print the result in a list using array
my code is:
<?php $targets = ["Test"=> array(
"fake1"=>"http://fakeimg.pl/$i/",
"fake2"=>"http://fakeimg.pl/$i/?text=Hello")
]; ?>
<ul id="gallery_img">
<?php
$start = 100;
$end = 200;
for( $i=$start; $i<=$end; $i++){
if($i <= 99 && $i >= 10){
$target = $targets["Test"]["fake1"];
}if($i >= 100){
$target = $targets["Test"]["fake2"];
}
echo "<li><img src=\"$target\" alt=\"$i\" title=\"$i\" /></li>";
}
?>
</ul>
the for() is to keep the numbers between 100 to 200, and print the img in the list, the src for the image is on the array.
i had a problem when i declare a variable $i in the array, i call it like this $target = $targets["Test"]["fake2"] the $i variable don't take it from the for() so what i done? help me please.
It seems like there are a few syntax errors in how the $targets array is declared. Try this:
<?php $targets = array("Test"=>array(
"fake1"=>"http://fakeimg.pl/",
"fake2"=>"http://fakeimg.pl/?text=Hello")
); ?>
<ul id="gallery_img">
<?php
$start = 100;
$end = 200;
for( $i=$start; $i<=$end; $i++){
if($i <= 99 && $i >= 10){
$target = $targets["Test"]["fake1"];
}if($i >= 100){
$target = $targets["Test"]["fake2"];
}
echo "<li><img src=\"$target\" alt=\"$i\" title=\"$i\" /></li>";
}
?>
</ul>
Additionally - since $start is 100, the first condition will never be met. If you want some output with the ["fake1"] contents, set $start = 11
You are trying to use the strings contained in the $target array as templates. They will not be dinamically reevaluated in your for loop or practically anywhere else. You can use sprintf to achieve what you desire, however:
<?php $targets = ["Test"=> array(
"fake1"=>"http://fakeimg.pl/%d/",
"fake2"=>"http://fakeimg.pl/%d/?text=Hello")
]; ?>
<ul id="gallery_img">
<?php
$start = 100;
$end = 200;
for( $i=$start; $i<=$end; $i++){
if($i <= 99 && $i >= 10){
$target = sprintf($targets["Test"]["fake1"], $i);
}if($i >= 100){
$target = sprintf($targets["Test"]["fake2"], $i);
}
echo "<li><img src=\"$target\" alt=\"$i\" title=\"$i\" /></li>";
}
?>
</ul>
In the code above, I modified the lines 2, 3, 12 and 14, outlined below:
/* line 02 */ "fake1"=>"http://fakeimg.pl/%d/",
/* line 03 */ "fake2"=>"http://fakeimg.pl/%d/?text=Hello")
/* line 12 */ $target = sprintf($targets["Test"]["fake1"], $i);
/* line 14 */ $target = sprintf($targets["Test"]["fake2"], $i);
By the way, the body of the first if clause will never be executed. It'd be nice to review your code with care.

Store images in array PHP

I'm really new at php just doing some work, I want to save images in a php array and then show them in the screen, but I cannot save them or display them.
<?php
$min = 1;
$max = 9;
$number1 = rand($min,$max);
for ($i=1 ; $i<=$number1 ; $i++){
$firstN [$i] = echo "<img src='index.jpg' border='0'>";
}
echo $firstN [1];
?>
This is what I got , and the last line is to test it but nothing works, I google the topic but it doesn't help.
Thanks in advance.
As long as index.jpg is in the same directory as your file, this should work:
<?php
$firstN = array();
$min = 1;
$max = 9;
$number1 = rand($min, $max);
for ($i = 0; $i < $number1; $i++){
$firstN[] = '<img src="index.jpg" border="0">';
}
echo $firstN[0];
?>
Cleaned up the code a bit. When storing information in the array, you don't use echo and, like Mister pointed out, you had a space in the echo at the bottom of the code between the array-variable and the brackets.

Categories