generating more than one image with a for loop - php

I am new to the php gd library. I have developed code for generating an image with the gd library and the creation of the image is successful. I have tried to generate more images of same type with a for loop but when i tried to add a button near all the images it didn't fit on one image. It appears like this:
My code
<?php
$num = 5;
for ($j = 1; $j <= $num; $j++) {
echo "<img src=yeah.php /><br><br>";
echo "<button>clickme</button>";
}
?>
I want to fit the button in all the 5 images ..Hope you guys can help me out
Thanks in advance..

<?php
$num = 5;
for ($j = 1; $j <= $num; $j++) {
echo "<img src=yeah.php /><button>clickme</button><br><br>";
}
?>
not sure if it is answer worthy..
this is just a basic html output you output a image then 2 linebreaks then your button
and repeated this process 5 times given the loop that is why you have the current final results.

Related

PHP: For loop, how do I set a variable as a maximum?

I am trying to be able to make a loop that lets me place stars on a website, to show a rating (1 to 5) on the website, that you can put down with fields. However, I do not know how to convert the field correctly.
I am using WordPress with Custom Field types, this is were I define the variable.
I have a variable that I get from somewhere else, and I have tried the following two ways:
<?php
$star = intval(the_field('rating'));
for ($i=0; $i < $star; $i++){
intval(the_field('rating'));
}
?>
and the second one:
<?php
$star = number_format(the_field('rating');
for ($i=0; $i < $star; $i++){
number_format(the_field('rating'));
}
?>
Thank you very much for you help in advance.
First the_field echos the value, you need get_field to return the value. Then just repeat whatever string you're using to display a star or empty star:
$stars = get_field('rating');
echo str_repeat('*', $stars) . str_repeat('O', 5-$stars);
With a loop just check if your counter is less than or equal to the star rating:
$stars = get_field('rating');
for ($i=1; $i <= 5; $i++){
echo ($i <= $stars) ? "*" : "O";
}

More elegant solution to this nested loop, anyone?

I have a php procedure that scans a directory where its content is thumbnail images of pdf files. The thumbnails are then displayed in a table, and included in an iframe of a parent web page. Each thumbnail is itself a hyperlink when clicked will open the actual pdf file. To avoid having a horizontal scroll bar, 9 images in a table row is perfect. I wrote a nested loop that in effect acts like a word wrap, where it displays 9 images and then begins another row. The actual code is much more entailed, so I have it pared down to a bare minimum example. It seems almost counter intuitive on first glance, decrementing $i on the second line of the outer loop, but it works. I wonder if anyone has a more elegant solution?
$ary = array(1,2,3,4,5,6,7,8,9,10);
for ($i=1; $i<(count($ary)+1); $i++) {
$i = $i-1;
for($j=0; $j<9; $j++) {
if ($i === count($ary)) break;
echo ($ary[$i].", ");
$i+=1;
}
echo "<br>";
}
The completed code now where $ndx is the count of the array, $dir is the scanned directory containing the png images, and $rtDir is the directory where the pdf's are saved:
if ($ndx > 0) {
$tbl = '<div id="draggable" class="ui-widget-content">
<ul>
<table><tr>';
/* place 9 images on one row */
foreach ($myfiles as $index => $image) {
$pdf = basename($image, ".png");
$pdf = $pdf . ".pdf";
$pdf = $rtDir.$pdf;
$tbl .= '<td>
<span class="zoom">
<a href="'.$pdf.'" target="_blank">
<li><img id="pdfthumb'.$index.'" class="myPhotos" alt="pdf'.$index.'" src="'.$dir.$image.'" ></li>
</a>
</span>
</td>';
if ($index % 9 == 8) {
/* end the current row and start a new one */
$tbl.= "</tr></table><br><br><table style='margin-top:-40px'><tr>";
}
}
$tbl .= "</tr></table></ul></div>";
printf($tbl);
unset($myfiles);
}
Thanks everyone for your suggestions.
So you want 9 images on each row? There are usually two logical choices:
Alternative 1: You use array_chunk(), e.g. like this:
$chunks = array_chunk($images, 9);
foreach ($chunks as $chunk) {
foreach ($chunk as $image) {
// image printing goes here
}
echo '<br'>;
}
Alternative 2: You use the modulo operator, e.g. like this:
foreach ($images as $index => $image) {
// images printing goes here
if ($index % 9 == 0) { // or 8, since it's a 0-index array... I don't remember
echo '<br>';
}
}
I mostly use the second version, myself, if I have to - or I ask one of our designers to make it fit to a proper width through css. Do note also that the second version won't work if your images array is associative.
$b = count( $ary ) - 1 ;
for( $i = 0 ; $i <= $b ; $i++ ) :
echo $ary[$i] ;
if( ( $i + 1 ) % 9 == 0 ) :
echo "<br>" ;
endif ;
endfor ;
like the above comment i like modulus but i also prefer the for loop, hope this helps.

PHP echo line X times different text

Title might not make complete sense, couldn't think of a way to explain it so I will do a mock PHP example.
<?php
$startNumber = 1;
$endNumber = 15;
$fileExt = '.jpg';
?>
And then on the page it will echo from the $startNumber to the $endNumber in something like a foreach
<?php echo("<img src=\"#.jpg\">"); ?>
Hope this is making sense...
I'd also like to point out that the numbers < 10 start with a 0 so it will need to be in the image source link.
echo is not a function, it's a language construct. Don't use ().
Also you need to use a loop like for() to do that (Also see sidenote of Fred -ii).
$endNumber = 15;
for($i = 1; $i <= $endNumber; $i++) {
if($i < 10) {
$i = "0".$i;
}
echo "<img src=\"".$i.".jpg\" />";
}
If this is not what you asked, add more information.

PHP: Run through multiple URLs and display their content

Let's say we have an URL looking something like this.
http://domain.com/index.php?p=1&u=A1b2C3d4E5f6G7h8I9j
The number span after ?p= goes from 1 to 999 and the rest is unchanged.
Every URL contains one short line of text.
What would a script look like which can run through all 999 URLs and display their contents?
It would be easy. You can use the FOR LOOP.
<?php
for($i=1; $i < 1000; $i++) {
echo file_get_contents('http://domain.com/index.php?p=' . $i . '&u=A1b2C3d4E5f6G7h8I9j');
}
Documentations:
For Loop
file_get_contents() method
This is going to be resource intensive. But taking a shot in the dark, this should work:
<?php
$urlContent = array();
$urlStart = 'http://domain.com/index.php?p=';
$urlEnd = '&u=A1b2C3d4E5f6G7h8I9j';
$count = 1;
while($count <= 999){
$urlContent[$count] = file_get_contents($urlStart.$count.$urlEnd);
$count++;
}
foreach($urlContent as $content){
echo $content.'\n';
}
?>
This code is Untested

how to generate a random image output

i have some basic php code that pulls an image from a particular image folder when the user asks using a form.
I will have many image folders and want to generate a random image instead of using
case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/A_001.jpg\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\"/> </a>" ; break;
My question is this as the form is processed with someone using the letter A the picture of that letter appears. The php code for this is
if (array_key_exists('check_submit', $_POST))
{
$letters = $_POST['Comments'];
$num_letters = strlen($letters);
for($i = 0; $i < $num_letters; $i++)
{
switch ($letters[$i]) {
case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/A_001.jpg\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\" alt=\"Image A\"/>
</a>" ;
break;
This only pulls the exact image i have asked, but i have hundreds in that folder and would like a more simple code to work with.
Please can someone help, they gave advise on using random image from folder, but that only works as a starting point not on the code I already have.
Thanks for your time
The solution posted at the link below should achieve the functionality you are looking for.
https://stackoverflow.com/a/4478788/1152375
so you should be able to do something like
case 'A' : echo "<a href=\"Alphabet-Letters/Letters-A\">
<img src=\"image/data/A/" . random_pic("folder_with_pics") . "\" id=\"A1\" width=\"70\" height=\"120\" title=\"A1\</a>";
break;
before you get to the output code, you will want to get a list of all files in the relevent directory ( http://php.net/manual/en/function.dir.php ) in a numbered array.
count the number of items in the array ( http://php.net/manual/en/function.count.php ) and randomly pick one ( http://php.net/manual/en/function.rand.php ) using the min and max settings of rand.
Try using the scandir function to find all the files in the folder, and then use the rand function to randomly choose one:
if(!empty($_POST['check_submit']))
{
$letters = strtoupper(trim($_POST['Comments']));
$num_letters = strlen($letters);
for($i = 0; $i < $num_letters; $i++)
{
$letter = $letters[$i];
$folder = 'image/data/'.$letter;
$files = scandir($folder);
array_shift($files);
array_shift($files);
$index = rand(0, count($files) - 1);
$file = $files[$index];
echo "<a href=\"Alphabet-Letters/Letters-{$letter}\">\n";
echo "<img src=\"image/data/{$letter}/{$file}\" id=\"{$letter}{$index}\" width=\"70\" height=\"120\" title=\"{$letter}{$index}\" alt=\"Image {$letter}\"/>\n";
echo "</a>\n";
}
}
Found the answer by using #Buchow_php and trial and error.
for ($i=0; $i<$num_letters; $i++)
{
$image_num = '$image'.$i;
echo "<input type=\"hidden\" name=\"option[$image_num]\" value=\"$skus[$i]\" />";
}
together with the previous code and now it brings all the image files into an array for posting to my submit

Categories