i have a simple webpage where i am trying to echo multiple images, my code is like below
<?php for($l=1;$l<=45;$l++){?>
<div class="thumb" style="background-image: url(l<?=$l?>.jpg);"></div>
<?php } ?>
so here the images are displayed fine in an order from 1 to 45, but what i want is images to display randomly everytime the page is loaded, can anyone please tell me how to accomplish this, thanks in advance
As mentioned in the comments, just create an array and shuffle it.
$images = [];
for ($l = 1; $l <= 45; $l++) {
$images[] = "<div class='thumb' style='background-image: url(l{$l}.jpg);'></div>";
}
shuffle($images);
echo implode("\n", $images);
Related
I have an array of 34 logos. I would like to display them but I want to make a slider and have 6 logos on each slides.
I have difficulties to create the loop. My code is in Php but it's the logic who matters here I think. Here what I have done so far. I miss the loop in the middle.
$number_of_slides = round($logo_array, 0, PHP_ROUND_HALF_UP);
for($i ; $i < $number_of_slides ; $i++):
<div class="slide">
......
</div>
<?php endfor; ?>
Thanks for any help
If you already have your array split it into chunks:
$logo_array = [];
$chunks = array_chunk($logo_array, 6); // 6 is a size of chunk
foreach ($chunks as $chunk) {
// do dome stuff for each chunk, open some wrapper div or whatever
foreach ($chunk as $logo) {
// output current logo
}
// do dome stuff for each chunk, close some wrapper div or whatever
}
Or, if you want to do it in one loop, you could use some modulo logic:
$number_of_slides = 25;
$str='<div class="slide">';
for ($i=0 ; $i < $number_of_slides-1 ; $i++) {
$str.= $logo_array[$i].($i%6==5?"</div>\n<div class=\"slide\">":' ');
}
$str.=$logo_array[$i].'</div>';
echo $str; // output the whole string
A simplified demo can be found here: http://rextester.com/DVMPR99965
This not have any sense... your slider is in JS I presume? If it is, all logos would necessarily be into page and is your slider that will take care of the rest.
Some sliders have a specific verbose to separate the itens, others don't, just needing to set the max number of itens to be displayed into page.
What I would do? Well:
<div class="slide">
<?php
foreach($logo_array as logo){
echo "<img src='{$logo[src]}'>"; //or other tag pattern of your slider
}
?>
</div>
Hi I'm a newbie with PHP and this site, so please be nice :)
I'm currently having trouble working the below PHP foreach code out as I'm trying to echo all images in a HTML table 3 column but it echo's with 2 only.
UPDATE: I've managed to fix some issues thanks to the comments guy's, thank you. However, I', now experiencing another issue which is confusing.
Basically, If I have one picture in a folder, it will echo that one picture, but If I put two pictures there, it echo's out with 4, 1 first picture echo's with 2 and the second is with 2 as well. Basically showing 4 images even though I have 2 images in that folder. I can't seem to fix this..
Here's the code:
<?php
// get images
$images = glob($imagedir.'/' . "*.png");
$i = 0;
echo'<table><tr>';
foreach($images as $image)
{
$i++;
echo '<td><img src="'.$image.'" height="200"></td>';
if($i == 3)
{
echo '</tr><tr>';
$i = 0;
}
}
echo '</tr></table>';
?>
Thanks in Advance
You're code for rendering the HTML is just fine.
If you have duplicates, the content of your imagedir must be wrong.
A few remarks:
glob($imagedir.'/' . "*.png"); also include directories which names end as .png.
Depending on the amount of images, the last table row will not be completely filled with table cells.
It's good practice not using the php closing tag ?> at the end a the php file.
I've altered you code to avoid above to problems.
I'm sure there more/other ways to do this, but this came in mind first.
<?php
$imagedir = 'images';
//Get *.png files only
$images = array_filter(glob("$imagedir/*.png"), 'is_file');
//Make image array divisble by 3 (columns)
while (count($images) % 3 != 0) {
$images[] = '';
}
echo'<table><tr>';
for ($i = 1; $i <= count($images); $i++) {
//Render TD if $image is not empty
if ($images[$i-1] != '' != '') {
echo '<td><img src="' . $images[$i-1] . '">', "<br>Image $i</td>";
}
//Close table row after 3 TD's
if($i % 3 == 0)
{
echo '</tr><tr>';
}
}
echo '</tr></table>';
Hello I am trying to create the lightbox gallery in my WordPress, I managed to get most of the code working well for grabbing images out of gallery. However I don't know how to get their URL for their original files....
PHP
<?php $post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
$item = 0;
while ($array_id[$item] != ''){
if($item == 0){
echo wp_get_attachment_image($array_id[$item], 'medium');
}else{
echo "<a ref='".wp_get_attachment_image($array_id[$item])."'>";}
$item++;
}
?>
So my idea is to display IMG of a first image in the gallery which I have accomplished with the first ECHO, not for all other images I only want the links to the original files.
My final code needs to look like this:
<a rel="group1" href="image_big_1.jpg"><img src="image_small_1.jpg" alt=""/></a>
<a rel="group1" href="image_big_3.jpg"></a>
<a rel="group1" href="image_big_4.jpg"></a>
<a rel="group1" href="image_big_5.jpg"></a>
<a rel="group1" href="image_big_6.jpg"></a>
Also what would you suggest for rel='' I need this to be unique for every post with galleries I am not sure what to use I can't use TITLE because if they enter more then one gallery with same title there will be issue. I am guessing maybe some sort of (RANDOM) prefix defined by current_time?
Please help
Thank you
I found a solution and edited my code to work:
This is code to accomplish above stated:
<?php $post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
$item = 0;
while ($array_id[$item] != ''){
$url = wp_get_attachment_image_src($array_id[$item]);
if($item == 0){
echo "<a href='".$url[0]."'>".wp_get_attachment_image($array_id[$item], 'gallery-thumb')."</a>";
}else{
echo "<a href='".$url[0]."'</a>";}
$item++;
}
?>
Reference used: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
Thank you all!
hi guys i am trying to do this like i have 5 frames (fancy border) and i have items list. when items load every item load different frame. when 5 frame done then 6th frame repeat frames list. below my script
<?php
$allgfts=mysql_query("select id,image_url from {$statement} order by id limit {$startpoint}, {$limit}");
while($gfts=mysql_fetch_array($allgfts))
{
$id=$gfts['id'];
$image=$gfts['image_url'];
?>
<div id="pic-1">
<div class="thumbnail-item">
<?php echo '<img src="images/'.$image.'" alt="" width="161" height="161" class="thumbnail g-size" />'; ?>
<span><?php echo 'Readmore';?></span>
<?php echo '<a class="gtbtn" href="g_buy.php?id='.$id.'">Get This</a>';?>
</div>
</div>
<?php
}
?>
I think you're asking how to echo a list of images, with that list wrapping to a new line every fifth item.
Given an array of results from a table (consider using PDO, by the way), I would do the following:
//$arr being the array
$x=1; //start counter
$list = '<ul>'; //using a list, because that's what it is
for($i=0;$i<count($arr);$i++) {
$list.='<li class="thumbnail-item">';
$thumb ='<a href="g_detail.php?id='.$arr[$i][id].'">';
$thumb.='<img src="images/'.$arr[$i][image_url].'" alt="" class="thumbnail g-size" /></a>';
$thumb.='<span><a href="g_detail.php?id='.$arr[$i][id].'>Readmore</a></span>';
$thumb.='<a class="gtbtn" href="g_buy.php?id='.$arr[$i][id].'">Get This</a>';
$list.=$thumb;
$list.='</li>';
if($x%5 == 0)||($x==count($arr)) {
$list.='</ul>';
if($x<count($arr)) {
$list.='<ul>';
}
}
$x++;
}
echo $list;
This is untested, but broadly speaking should work.
Use the remainder operator("%"). I don't know what your table structure looks like, but I am going to assume your product ID loads in sequential order, starting with 1.
In your WHILE loop, use the following:
$remainder = $id % 5;
if($remainder == 1){
//load my DIV with frame 1
}
else($remainder == 2){
//load my DIV with frame 2
}
......
I am quite new to PHP and I did not want to ask anything here until I absolutely could not find a way to do this.
I have a situation where I would like to use PHP to read and display images from a directory but I would like the first image read to be echo'd uniquely
My current code as follows:
$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
foreach ($images as $image){
echo '<div class="item">'."\r\n\t\t";
echo '<img src="'.$image.'" alt=""></div>'."\r\n\t";}
Just a simple directory pull and echo with formatting.
This will be for a BootStrap carousel and unfortunately the first image in line has to have <div class="item active"> or the whole thing fails.
So to reiterate, the first image pulled needs to be <div class="item active"> while the rest need to be <div class="item">
I think I went through an entire pack of cigarettes wracking my brain around this.
use a flag for that. hope to help.(-:
$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
$flag=1;
foreach ($images as $image){
echo '<div class="item' .($flag?' active':''). '">'.PHP_EOL."\t\t";
echo '<img src="'.$image.'" alt=""></div>'.PHP_EOL."\t";
$flag=0;
}
How about using a for loop instead of a foreach loop. When index == 0 you can add "active" to your div's class.
Note I didn't run this code- but it should give you the right idea.
$imageDir = "img/landMarks/carousel/";
$images = glob($imageDir.'*.jpg');
for($i = 0; $i < count($images); $i++){
//If the index is 0, it's the first image.
$class = ($i == 0) ? 'item active' : 'item';
//Dynamically change your div's class
echo "<div class='$class'>\r\n\t\t";
echo '<img src="'.$images[$i].'" alt=""></div>'."\r\n\t";
}