Store images in array PHP - 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.

Related

Correct php code for echoing html with variables

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!

php variable after a variable in for loop

I need a way to do this
for($i=1;$i<=10;$i++){
$id$i = "example" . $i;
}
notice the second line has $id$i
so for the first loop $id1 will equal example1
in the second loop $id2 will equal example2
and so on...
Thank you very much!
You can use variable variable names to do this; however, it's probably much more convenient if you just used an array:
for($i = 1, $i <= 10, $i++) {
$id[] = "example" . $i;
}
You can convert a string into a variable (the name of the variable), if you put another $ in front of it:
$str = "number";
$number = 5;
$$str = 8;
echo $number; // will output 8
So in your example, you could do it like that:
for($i = 1; $i <= 10; $i++) {
$str_var = "id".$i;
$$str_var = "example".$i;
}
It would be much better to use an array, but you could do this:
for($i=1; $i<=10; $i++){
$var ="id$i";
$$var = "example" . $i;
}
Here's what I would recommend doing instead:
$ids = array;
for($i = 1; $i <= 10; $i++) {
$ids[$i] = "example" . $i;
}
You could create an array of size $i with a name of $id, and insert each element into a different index.
for($i=1;$i<=10;$i++){
$id[$i] = "example" . $i;
}
$var = array();
for($i=1; $i<=10; $i++) {
$var['id' . $i] = 'example' . $i;
}
extract($var, EXTR_SKIP);
unset($var);
but why not use a simple array?

How can I store results from MySQL to PHP array?

I tried storing data to php array from mysql using under code. But it's not working.
This code:
echo $answerArray[count][i];
shows the correct result. But this code:
echo $answerArray[0][0];
doesn´t show anything.
What should I do to fix it?
Thank you.
Full code:
$count = 0; //answer count
$answerArray = array();
while ($row = mysqli_fetch_array($resultFromR, MYSQLI_ASSOC)) { //add array from db
for($i = 0; $i < $questionNumber; $i++) {
$j = $i + 1;
$answerArray[count][i] = $row["num$j"];
echo $answerArray[count][i]; //is working.
}
$count++;
}
echo '<br />';
echo $answerArray[0][0]; //something wrong!!! I cannot get anything from this.
maybe this will work
$answerArray[$count][$i] = $row["num$j"];
(add $ before count and i)
Not sure but shouldn't the [i] be [$i]?

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.

how do i reverse the order when echoing my results?

i have this bit of code:
<?php
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?>
and currently it echos the links in order they appear on the website. I would like for it to echo the results in a reverse order (the last link on example.com to echo as the first with this code)
any help is appreciated! thanks.
for ($row = $count - 1; $row > -1 ; $row--) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
PHP has a function for it: array_reverse.
$a[1] = array_reverse($a[1]);
Also why would you use ["$row"] instead of [$row]? There is no functional difference as numeric strings get converted back to numbers when using them as indexes, so don't worry about that, but just because something can be done doesn't mean you should do it.
<?php
$file = file_get_contents('http://example.com');
preg_match_all("/<a href=(.*?links.*?)>.*?<\/a>/i", $file, $a);
$a = array_reverse($a);
$count = count($a[1]);
for ($row = 0; $row < $count ; $row++) {
$linkurls = $a[1]["$row"];
echo ' '.$linkurls.' <br>';
}
?>

Categories