Change foreach to show only first/last added image - php

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..

Related

echo array of images using codeigniter and mysql

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.

How to group posts by category using PHP & MySql?

I’ve tried using GROUP BY in my query but that only returns one post per category. This is an example of what is returned with my current setup. What I need is for all posts with the same category to be grouped together, can someone point me in the right direction? Thanks for your time.
Like suggested by Charlotte, you could group your posts in an associative array, with the categories as keys.
<?php
// Connecting to database
$categories = array(); // Create an empty array
while ($post = mysqli_fetch_array($result)) {
if(!array_key_exists($post['category'], $categories)) { // Check if the category is already present
$categories[$post['category']] = array(); // Create an array indexed with the category name
}
$categories[$post['category']][] = $post; // Add the post to the category
}
?>
Now you have to iterate twice : to display the categories and to display each post in the category.
When I have to deal with nested foreach to render html, I preferably use inline foreach.
The rendering of the posts should look like :
<?php foreach ($categories as $category => $posts) : ?>
<h2><?php echo $category; ?></h2>
<?php foreach ($posts as $post) : ?>
<a href='article.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a>
<p>Posted on <?php echo date('d-m-y h:i:s',strtotime( $post['posted'] ) );?> In <a href='category5.php?id=<?php echo $post['category_id']; ?>' ><?php echo $post['category']; ?></a>
</p>
<?php endforeach; ?>
<hr/>
<?php endforeach; ?>
Edit: I ometted the opening and closing php tags in the first block of code.
If you assemble the two blocks of code in your php file, it should work.

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>

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.

Show product attributes in list.phtml - Magento

Hello I have read many posts about this, and while it works its not complete.
For example; Attribute 1= shoesize and attribute 2 = shoe color.
Both are in a dropdown and I would like to list all of the possible attribute colors per product within the category pages.
Problem: When I test the code it will only display the first shoe color, instead of all posibilites. What am I doing wrong here?
Here are 3 examples of what I have. All code work, but only shows the first attribute color.
Example 1:
<!-- Find the following loop -->
<?php foreach ($_productCollection as $_product): ?>
<!-- Inside it insert one of the following codes as needed -->
<!-- Use this for regular text attributes -->
<?php echo $_product->getMyAttribute() ?>
<?php echo $_product->getAnotherCustomAttribute() ?>
<!-- Use this for dropdown attributes -->
<?php echo $_product->getAttributeText('shoecolor') ?>
<?php endforeach?>
<!-- ... -->
Example 2
<?php echo $_product->getResource()->getAttribute('shoecolor')->getFrontend()->getValue($_product) ?>
Example 3
<?php $type = "simple"; $p = "0" ?>
<?php foreach ($_productCollection as $_product): ?>
<?php $custom = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); ?>
<?php $col = $custom->getUsedProductCollection()->addAttributeToSelect('shoecolor')->addFilterByRequiredOptions(); ?>
<?php foreach($col as $simple_product) { $p=$simple_product->getId(); $type="configurable"; } ?>
<?php if($type == "configurable"): ?>
<h5><?php echo $_product->load($p)->getAttributeText('shoecolor'); ?><?php $type="simple" ?></h5>
<?php endif; ?>
you can just config it in attribute edit page
Used in Product Listing -> Yes
Another way
$_product = Mage::getModel('catalog/product')->load($this->getProduct()->getId());
If you create an attribute like yours "shoesize" then you can access by following code.
If your attribute type Text Field ( $_product should loaded ) :
<?php
echo $_product->getShoesize();
// if your arribute was shoe_size then
echo $_product->getShoeSize();
?>
If your attribute type Multiple Select or Dropdown, to get all attribute value :
<?php
echo $_product->getAttributeText('shoesize');
?>
Here is the code get attribute name and value that that doesn't belongs to any product
$attributeCode = 'YOUR_ATTRIBUTE_CODE';
$product = Mage::getModel('catalog/product');
$productCollection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', $attributeCode);
$attribute = $productCollection->getFirstItem()->setEntity($product->getResource());
print_r($attribute->getData()); // print out the available attributes
$options = $attribute->getSource()->getAllOptions(false);
print_r($options); // print out attribute options
Try this:
$_pp2 = Mage::getModel('catalog/product')->load( $_product->getId() );
echo $_pp2->getdm();
in:
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
Attribute Code:dm
Type: Text area
in view.phtml
echo $_product->get>getdm(); ?>

Categories