Using a array to print the result in a list - php

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.

Related

How to read the last result of a while loop using php

i need to put the last while loop result into a variable. How can i return only the last one?
<?php
$x = 0;
$r = rand(0,5);
$d = 10 + $r;
$h = 143;
$counter = 0;
while ($h >= 0) {
echo "The number is: $h <br>";
$h-=$d;
$counter++;
}
?>

warning occuring in php word occurence

I have written the following code to count the number of string occurrences in a given file.
PHP
<?php
$p = fopen("g.txt", "r");
$q = fread($p, filesize("g.txt"));
$t = explode(" ", $q);
$m = explode(" ", $q);
$i = 0;
$j = 0;
$r = 0;
$count = 0;
$c = count($t);
$d = array();
echo "count of".
"<br/>";
for ($i = 0; $i < $c; $i++) {
for ($j = $i; $j < $c; $j++) {
if ($t[$i] == $t[$j]) {
$count = $count + 1;
}
}
for ($r = $i + 1; $r < $c; $r++) {
if ($t[$i] == $t[$r])
unset($t[$r]);
}
echo $t[$i].
"=".$count.
"<br/>";
$count = 0;
}
?>
I am getting a notice of undefined offset on line numbers 17 and 24, though my output is coming out to be correct. Can you please help me in rectifying the above problem?
The problem is that you are deleting items from the array $t. You saved the count in $c, but the actual count will change by your last inner loop.
Even if you replace $c by count($t) everywhere, it will go wrong, because the last loop should be in reverse order, otherwise you skip items. For instance if you have the list 'a', 'b', 'c'. then when you delete 'b' and increment $r, you will not check 'c' at all.
So, if I fix those things, your code becomes as below. Although I didn't really check it for other issues. Frankly, I don't really get what is should do. ;-)
<?php
$p=fopen("g.txt","r");
$q=fread($p,filesize("g.txt"));
$t=explode(" ",$q);
$m=explode(" ",$q);
$i=0;
$j=0;
$r=0;
$count=0;
$d=array();
echo "count of"."<br/>";
for($i=0; $i<count($t); $i++)
{
for($j=$i; $j<count($t); $j++)
{
if($t[$i]==$t[$j])
{
$count=$count+1;
}
}
for($r=count($t) - 1; $r > $i; $r--)
{
if($t[$i]==$t[$r])
unset($t[$r]);
}
echo $t[$i]."=".$count."<br/>";
$count=0;
}
?>
In conclusion, you should do more tests. If the outcome of this script was okay, then it was by accident.

php array is printed infinite time using while loop

I know i Can easily do this with a for loop but i am trying to do it in while loop and I get Google infinite times. Why is this? I Knew something was wrong however because i was going over a tutorial on tuts+ I thought it was my mistake. Then I read the comments section of the video and the instructor says sorry I forgot to increment the I.
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
while ( $i < 10) {
echo "<li>$month[$i]</li>";
}
?>
Add i++ after the echo statement.
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
while ( $i < 10) {
echo "<li>$month[$i]</li>";
$i++;
}
It happens because $i is always equal to 0 in your code.
You need to increment it (as stated in the other solution) for instance using $i++ in the loop.
Note that it's generally better to use foreach which will iterate over each element of the array:
$months = array('google', 'html5nurse' , 'facebook');
foreach($months as $month){
echo $month."<br/>";
}
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
while ( $i++ < 10)
{
echo "<li>$month[$i]</li>";
}
?>
You should be using $i++ to increment $i and also using count so that the loop does not continue after there are no more results in the array.
<?php
$month = array('google', 'html5nurse' , 'facebook');
$i = 0;
$count = count($month);
while ($i < $count)
{
echo "<li>$month[$i]</li>";
$i++;
}
?>

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.

Create HTML elements in loop

Given a number like i.e: 6 I need to generate 6 DIV elements.
For example:
$number = 6;
// PHP generates the DIV for $number of times (6 in this case).
How can I do it? I am not an expert of PHP loops, if this is the case. Thanks!
Example uses of the different types of loops you could use. Hopefully you can see how they work.
Foreach Loop
$element = "<div></div>";
$count = 6;
foreach( range(1,$count) as $item){
echo $element;
}
While Loop
$element = "<div></div>";
$count = 0;
while($count < 6){
$count++;
echo $element;
}
Simple For Loop
$element = "<div></div>";
$count = 6;
for ($i = 0; $i < $count; $i++) {
echo $element;
}
function generateDIVs($number)
{
for ($i = 0; $i <= $number; $i++)
{
echo "<div><div/>";
}
}
In order to generate 6 div elements loop is necessary.
using while loop:
$count = 1;
while($count <= 6){
$count++;
echo "<div></div>";
}
using for loop:
$count = 6;
for ($i = 0; $i < $count; $i++) {
echo "<div></div>";
}
for ($i = 0; $i < 6; $i++) {
echo "<div class=\"example\"></div>";
}
Note that IDs (the # part) have to be unique on a page, so you can't have 6 different divs with the same #example id.
http://php.net/manual/en/control-structures.for.php
Here are some examples I use often, for quick mock-upping repeated HTML while working with PHP
array_walk() with iteration index and range
$range = range(0, 5);
array_walk($range, function($i) {
echo "
<section class='fooBar'>
The $i content
</section>
";
});
If tired of escaping double quotes \" or using ' or concatenation hell, you could simply use Heredoc.
$html = function($i) {
echo <<<EOT
<section class="fooBar">
The $i content
</section>
EOT;
};
array_map($html, range(1, 6));
The only small "disadvantage" of using Heredoc is that the closing EOT; cannot have leading and following spaces or tabs - which might look ugly in a well structured markup, so I often place my functions on top of the document, and use <?php array_map($html, range(0, 5)) ?> where needed.
str_repeat() when an index is not needed
$html = "
<section class='fooBar'>
Some content
</section>
";
echo str_repeat($html, 6);
you need echo command. Basically you are generating html by printing a string. Example
echo '<div> </div>';
will generate 1 div. You need it 6 times. You might want to use loop as well, but this is too basic question and I gave you a start.

Categories