Php while loop exclude first of array - php

<div id="prettyphoto" align="left"> <img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" />
<ul class="gallery clearfix">
<?php foreach ($images as $image) { ?>
<li><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></li>
<? } ?>
</ul>
</div>
I am trying to take this foreach loop of thumbnails and exclude the first image and have it load as a larger image I can't seem to figure out the proper way to accomplish this.

You could use array_shift():
<?php $firstImage = array_shift($images); ?>
<!-- do something with $firstImage -->
<?php foreach ($images as $image): ?>
...
<?php endforeach; ?>
or if you don't need a reference to the first image, array_slice():
<?php foreach(array_slice($images, 1) as $image): ?>
...
<?php endforeach; ?>
Also note the use of the alternative syntax for control structures which makes reading the mixture of PHP and HTML a bit easier (but is not related to your problem).

You can iterate the array "manually", with next(), current(), etc:
<?php
$images = array(
array('vpid' => 1, ),
array('vpid' => 2, ),
array('vpid' => 3, ),
array('vpid' => 4, ),
);
$image = current($images);
?>
<div id="prettyphoto" align="left"> <img src="http://images.idealer1.com/getimage/700/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" />
<ul class="gallery clearfix">
<?php
next($images);
while(list($idx, $image) = each($images)) { ?>
<li><img src="http://images.idealer1.com/getimage/100/<?php echo $image['vpid'] ?>.jpg" width="100" alt="" /></li>
<? } ?>
</ul>
</div>
Read: http://www.php.net/manual/en/function.each.php
With array_shift() you could "loose" the first image (you have to put it back in after you're done).
With array_slice() you would duplicate data, which is bad practice in general.

you can use some idea from this:
foreach($array as $key=>$value)
{
if($key==0)
echo "code for the large image";
else
echo "code for the thumbnail image";
}

Related

unable to fetch image from the database

I am trying to display images on my web page, the content is getting fetched from my database, but the issue I'm facing is in displaying the image. please, can anyone guide me how should I display the image?
I mean to say the path what I should give
here 'image' is my column name and this is my view
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/');$image?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
Hope this will help you :
Use this : <img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
The whole code should be like this :
<?php
if( !empty($results) ) {
foreach($results as $row) {?>
<div class="col-sm-4">
<img src="<?php echo base_url('uploads/'.$row->image);?>" alt="">
<h3><?php echo $row->title; ?></h3>
<p><?php echo $row->content; ?></p>
</div>
<?php
} ?>
<?php }
?>
If $image is the column name(which contains image name) then Replace
<img src="<?php echo base_url('uploads/');$image?>" alt="">
with
<img src="<?php echo base_url();?>uploads/<?php echo $row->image;?>" alt="">
If $image has image path then remove ';' and add '.' on echo base_url('uploads/');$image?> line
You issue here is the following code;
<?php echo base_url('uploads/');$image?>
This is because you are not concatenating the string, rather, just saying it's variable name, so, use of the following will provide the output;
<?php echo base_url('uploads/') . $image' ?>
This replaces the semi-colon part way through for a period (.) which is the PHP concatenation operator
Obviously, there is the issue you are not setting $image, which would require (before usage) of;
$image = $row['image_column_name'];
// Or
$image = $row->img_column_name

Two items per foreach loop

Apologies if this has already been asked (I cannot find an answer) but I am using PHP and I am building a slider, but would like two images per slide, not one. So, in theory the foreach() needs to include two per each.
An example of the setup is as follows:
<?php foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php endforeach; ?>
I was thinking I could do something like a count...
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
<li></li>
<?php endif; ?>
<?php endforeach; ?>
But I got a little confused as this would insert something every 2... not include two of whatever the foreach loop is fetching at once.
Hope this makes sense and thanks in advance
Why so complicated? Use a simple for loop instead:
<?php for ($i=0; $i<count($page->images)-1; $i+=2) { ?>
<img src="<?php echo $page->images[$i]->url; ?>"/>
<img src="<?php echo $page->images[$i+1]->url; ?>"/>
<?php } ?>
Or even more elegant, a do/while loop:
<?php $i=0; do { ?>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<?php } while ($i<count($page->images)) ?>
Compared to using a foreach loop these approaches have another advantage: you do not create copies of all objects. This can make a huge difference if those objects are non-trivial.
Okay, I managed to work this out... hopefully it will help.
<div class="each-slide">
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
</div><div class="each-slide">
<?php endif; ?>
<?php endforeach; ?>
</div>

PHP, Returning not null values in an unordered list only

I have the following sql query that fetches images from an establishment table.
The main photo ie, establishment_mainphoto_url is always inputed and can never be left null. However, the other 5 fields can contain photos or be left null, or a combination of some null and some not.
I have a jcarousel slide-image show that calls images from an unordered list for display.
The code:
$establishment_id = $row_establishment_view['establishment_id'];
$carouselSQL = mysql_query("SELECT establishment_mainphoto_url,
establishment_photo_url1,
establishment_photo_url2,
establishment_photo_url3,
establishment_photo_url4,
establishment_photo_url5
FROM establishment
WHERE establishment_id=$establishment_id");
$carouselImg = mysql_fetch_assoc($carouselSQL);
The jquery carousel code:
<div class="carousel_container"><!--Carousel list of images-->
<div class="infiniteCarousel">
<div class="wrapper">
<ul>
<?php if(!empty($carouselImg['establishment_mainphoto_url'])) {?>
<li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_mainphoto_url'];?>" height="390" width="500"/></li>
<?php } ?>
<?php if(!empty($carouselImg['establishment_photo_url1'])) {?>
<li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url1'];?>" height="390" width="500"/></li>
<?php } ?>
<?php if(!empty($carouselImg['establishment_photo_url2'])) {?>
<li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url2'];?>" height="390" width="500"/></li>
<?php } ?>
<?php if(!empty($carouselImg['establishment_photo_url3'])) {?>
<li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url3'];?>" height="390" width="500"/></li>
<?php } ?>
<?php if(!empty($carouselImg['establishment_photo_url4'])) {?>
<li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url4'];?>" height="390" width="500"/></li>
<?php } ?>
<?php if(!empty($carouselImg['establishment_photo_url5'])) {?>
<li><img alt="" src="Establishment_Images/<?php echo $carouselImg['establishment_photo_url5'];?>" height="390" width="500"/></li>
<?php } ?>
</ul>
</div>
</div>
</div>
Say a user enters the establishment_mainphoto_url, establishment_photo_url1 and establishment_photo_ur4 and leaves the other fields empty.
Right now the carousel shows the images but with blanks in between. E.g after the main image and image 1, it will have 2 blank "images" where 2 and 3 are supposed to be and then image 4 will show, then a blank for 5, and the carousel then starts over.
How do I fix it so that the 3 images show in line in the slideshow with no "blanks"?
<div class="carousel_container"><!--Carousel list of images-->
<div class="infiniteCarousel">
<div class="wrapper">
<ul>
<?php
foreach ( $carouselImg as $image ) {
if ( empty($image) ) continue;
echo '<li><img alt="" src="Establishment_Images/' . $image . '" height="390" width="500"/></li>';
}
?>
</ul>
</div>
</div>
</div>
Are you sure the $carouselImg['establishment_photo_url5'] is really empty? What does the generated html look like? Does it have more <li> than images should appear? Maybe there are some spaces in the database that makes empty($blah) consider it is non-empty?

Using Array_merge for 2 for_each arrays

Im using this to display a list of thumnails called from my cms:
<?php if($gallery_images) { ?>
<?php
$slide_page = 1;
foreach($gallery_images as $count => $image) { ?>
<li><a href="<?php echo $image->getResizedImage(); ?>" rel="example1" title="********"><img width="125" height="80" src="<?php echo $image->getThumbnailImage()
?>" /></a></li>
<?php if(($count+1) % 3 == 0) {
$slide_page += 1;
?>
It calls images from within my CMS and displays them in groups of 3, with some added jquery to scroll through the sets.
What im trying to do is merge this with my videos within the same list.
The video code is as follows:
<?php foreach($videos as $count => $video) { ?>
<img src="{thumbnail}" />Video A
<?php } ?>
Ive tried using the array_merge function but seem to be having difficulties any help would be greatly appreciated
It's easy:
foreach (array_merge($gallery_images, $videos) as $count => $value) { }
You may also want to look at array_chunk().
Update:
<? foreach (array_merge($images, $videos) as $key => $value): ?>
<? if (is_object($value) === true): ?>
<? if (method_exists($value, 'getLocation') === true): ?>
<img src="{thumbnail}" />Video A
<? elseif (method_exists($value, 'getResizedImage') === true): ?>
<img width="125" height="80" src="<?= $image->getThumbnailImage(); ?>" />
<? endif; ?>
<? endif; ?>
<? endforeach; ?>

flickr phpflickr api

Overview
I am trying to get a photo feed on to my site using Flickr's api and the phpflickr library. I can successfully get the photoset on to my site, but it shows all the photos from every photoset, what I was hoping to achieve was to show the primary photo from each photoset, and then if the user clicked on the image it would show the full photoset in a lightbox/shadowbox.
My Code
<div id="images" class="tabnav">
<ul class="items">
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<li>
<a rel="shadowbox['<?=$count;?>']" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<p><?=$ph_set['description'];?></p>
</a>
</li>
<?php endforeach; ?>
<?php $count++; ?>
<?php endforeach; ?>
</ul>
</div>
Another Attempt
I have also tried calling the getPhotos function differently, instead of sending it without any parameters I sent it with parameters
$photos = $f->photosets_getPhotos($photoset_id, NULL, NULL, 1, NULL);
The above code stopped the showing all the photos from each photoset and started showing just the primary image, but it also stopped making the rest of the photos accesible to me.
Is there something I can do to make this work? I am totally out iof ideas.
Regards and thanks
I came up with this soltion, thought I would post it in case anyone else hits this problem,
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<li>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<?php if($parentID == $ph_set['parent']): ?>
<a rel="lightbox[album<?=$count;?>]" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<?php endif;?>
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<?php if($ph_set['description'] != null) :?>
<p><?=$ph_set['description'];?></p>
<?php endif; ?>
<?php if($parentID == $ph_set['parent']): ?>
</a>
<?php endif;?>
<?php endforeach; ?>
</li>
<?php $count++; ?>
What you'll probably want to do is starting by iterating through the whole array and grouping each album into a separate array first and making a special array for your album's main photo.
Then you can easily iterate through the arrays to display each album and the code becomes much more maintainable.

Categories