echo array of images using codeigniter and mysql - php

I have had GROUP_CONCAT in MySQL database, so I have created view where is a row of an array of images. here it is:
I am using PHP CodeIgniter and I try to display this images on my page, but there is some problem: it shows only those images, which are not in an array in the table row.
here is my code to display:
<?php foreach ($get_hotels as $geth) { ?>
<td><img src="<?php echo base_url()."uploads/hotels/".trim(str_replace(",", " ", $geth->hotel_images));?>" width="73" height="53"></td>
<?php } ?>
So, my question is how to display pictures in same <td> as an array? any suggestions?

You can try with explode, like this :
$images = explode(',',$your query);
<?php foreach ($images as $key => $geth) { ?>
<td><img src="<?php echo base_url()."uploads/hotels/".$geth;?>" width="73" height="53"></td>
<?php } ?>
Hope this can help you

You can try the following method:
<?php
foreach($get_hotels as $geth):
$images = explode(',', $geth->hotel_images);
foreach($images as $image): ?>
<td>
<img src="<?= base_url() . "uploads/hotels/$image"; ?>" width="73" height="53">
</td>
<?php
endforeach; ?>
<?php
endforeach; ?>
This uses explode to separate the values by comma and then, runs a loop over them and prints the image tags.

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>

Echos not working

I am trying to make a site using cakephp and twitter bootsrap. For the gallery I am making I am trying to display, using foreach, multiple images , but they simply don't show up. Everything is working, the foreach and the variable data, but the echos don't display anything. Could anyone help please?
Thanks in advance.
(code bellow from my show_images.ctp view)
<table >
<tr>
<?php
$i=0;
foreach( $gallery_images as $gallery_image ):?>
<td align=center>
<?php echo "<a id=\"single_1\" href=\"test/".$gallery_image['GalleryImage']['path']." ";?>
<?php echo "<img src=\"test/".$gallery_image['GalleryImage']['path'].", alt=\"".$gallery_image['GalleryImage']['path']."";?>
<?php echo "</a>";?>
</td>
<?php $i++;?>
<?php
if($i==4){
echo "</tr><tr>";
$i=0;
}
?>
<?php endforeach ?>
</tr>
you haven't close your tag inside echotry to change this:
<?php echo "<a id=\"single_1\" href=\"test/".$gallery_image['GalleryImage']['path']." ";?>
<?php echo "<img src=\"test/".$gallery_image['GalleryImage']['path'].", alt=\"".$gallery_image['GalleryImage']['path']."";?>
<?php echo "</a>";?>
to this:
<?php echo '<a id="single_1" href="test'.$gallery_image['GalleryImage']['path'].' >';?>
<?php echo '<img src="test'.$gallery_image['GalleryImage']['path'].'" alt="'.$gallery_image['GalleryImage']['path'].'" />';?>
<?php echo '</a>';?>

Getting images from content:encoded tag

I am trying to display RSS feed in my magento website and I'm having difficulties showing the image for the feed. Studying the feed, I see that the image is inside a content:encoded tag so I can't access it directly by using something like $item->image. Here's my current code:
<?php $channel = new Zend_Feed_Rss('newsfeedurl'); ?>
<?php foreach ($channel as $item): ?>
<?php if($i<2) { ?>
<img src="<?php echo $item->image; ?>" title="<?php echo $item->title; ?>" height="63" width="95" />
<?php echo "image:".$item->content; ?>
<?php } else {} ?>
<?php $i++; ?>
<?php endforeach; ?>
$?>
I tried also tried using $item->content but this returns the entire content of the newsfeed. So my question is, how can I access the image's source from content:encoded in order to display it on my feed?
UPDATE: After some more research, I tried using preg_match like so: preg_match('/<*img[^>]*src = ["\']?([^"\'])/i', $item->content, $matches); echo $matches[0]; I'm getting the correct image path but I've placed this inside a loop so each I should have at least 2 images but I'm only getting 1. why is this?
SOLVED: I've managed to solve my problem by changing $matches[0] to $matches[1]. I guess I was using 0 thinking it was the index of an array matches.
In order to get the image source from the content:encoded tag, I used regular expression (preg_match). Here's how my current code looks:
<?php $channel = new Zend_Feed_Rss('newsfeedurl'); ?>
<?php foreach ($channel as $item): ?>
<?php preg_match('/<*img[^>]*src *= *["\']?([^"\']*)/i', $item->content, $matches); ?>
<?php if($i<2) { ?>
<img src="<?php echo $matches[1]; ?>" title="<?php echo $item->title; ?>" height="63" width="95" /></a></div>
<?php } else {} ?>
<?php $i++; ?>
<?php endforeach; ?>
Hope this helps someone else.

Change foreach to show only first/last added image

I have following code construction. It gives out an image of the product, which was even added to the shopping card. Images appear in the adding order: the image on top is the image, which was added last.
How can i change this code so, that i get showing only one image, from recently added product (but not all images from all added products)? I guess, i need change something in foreach ($items as $item) so, that the image is showing not for each $item, but only for one $item, but don't know how to modify this code exactly...
Thanks for your help and advises!
Here is the code:
<table>
<?php $i=0; $k=0; $subtotal = 0;?>
<?php foreach ($items as $item) : ?>
<?php
$link = K2StoreItem::getK2Link($item->product_id);
$link = JRoute::_($link);
$image_path = K2StoreItem::getK2Image($item->product_id, $this->params);
?>
<tr class="row<?php echo $k; ?>">
<?php if($this->params->get('show_thumb_cart')) : ?>
<td class="warkorb2">
<?php if(!empty($image_path)) : ?>
<img src="<?php echo $image_path; ?>" class="itemImg<?php echo $this->params->get('cartimage_size','small') ?>" />
<?php endif;?>
</td>
<?php endif; ?>
</tr>
<?php ++$i; $k = (1 - $k); ?>
<?php endforeach; ?>
<table>
You can use a Query with a limit or do something like this
<?php $lastItem = end($items) ?> <!-- return the last item in the $items array -->
<?php
$link = K2StoreItem::getK2Link($lastItem->product_id);
$link = JRoute::_($link);
$image_path = K2StoreItem::getK2Image($lastItem->product_id, $this->params);
?>
in the query that you did not post you can do a LIMIT 0,1 and also add in a SORT BY .. be it id, time etc..

Categories