here is my code:
<?php
foreach ($productsRecord['images'] as $upload):?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['urlPath'] ?>" alt="" /><br />
<?php endif ?>
<?php endforeach ?>
How would I go about limiting the results to only the first result, would I use a break; statement?
Cheers
Try to use current()
your code shall like that:
<?php reset($productsRecord['images']); ?>
<?php $upload = current($productsRecord['images']);?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['urlPath'] ?>" alt="" /><br />
<?php endif ?>
To more information about the current() check the manual:
http://php.net/manual/en/function.current.php
Just use the first element of the array directly
$productsRecord['images'][0]
No need to loop here.
Yes, use break
<?php break; endforeach; ?>
Related
Suddenly starting getting this error for the following code:
<?php foreach (get_the_terms(get_the_ID(), 'loan-club') as $cat) : ?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php endforeach; ?>
Sometimes the taxonomy 'loan-club' is empty. Could that be the problem? If so, could someone point me towards the correct code?
Add if condition before foreach:
<?php
$loan_club = get_the_terms(get_the_ID(), 'loan-club');
if(is_array($loan_club)) {
foreach ($loan_club as $cat) {
?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php
}
}
?>
Look at get_the_terms() function in the documentation: https://developer.wordpress.org/reference/functions/get_the_terms/
The function may also return WP_Error or false. If WP_Error or false is returned this will cause the error and the foreach loop breaks.
<?php foreach ((array)get_the_terms(get_the_ID(), 'loan-club') as $cat) : ?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php endforeach; ?>
Note: to all the people complaining about typecast, please note that the OP asked cleanest way to skip a foreach if array is empty (emphasis is mine). A value of true, false, numbers or strings is not considered empty.
This is the code that shows a Manufacturer Part Number on my product listing
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
what I would like to do is not show the MPN: Field on the product page if the $text_mpn field is blank, e.g if no part number is listed.
You can put an if statement around it. You can close the PHP code block after the if, put your code inbetween and open a new PHP code block to close it:
<?php if ($text_mpn != ''){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php };>
For blocks like this, it can be a bit messy and unclear to see where the block ends if you use normal curly braces, so you might consider the Alternative syntax for control structures for these cases:
<?php if ($text_mpn != ''):?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php endif;>
try this:
<?php if($mpn != ""){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php } ?>
try this
<?php if(strlen($text_mpn) > 0) echo "<span class='p-rewards'>MPN:".$text_mpn."</span>".$mpn."</br>"; ?>
Case 1:
You can use empty() for check is variable is empty or not.
<?php if(empty($mpn)){ ?>
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
<?php } ?>
Case :2
You can simply use display:none if you wants to hide control.you can simply add attribute of style.
<div <?php if(empty($mpn))
{ echo 'style="display:none"';}
else {echo 'style="display:block"';}
?> >
<span class="p-rewards">MPN:<?php echo $text_mpn; ?></span> <?php echo $mpn; ?><br />
</div>
<?php
$mpn = 'Ram Pukar';
$text_mpn = 'Hello ';
echo $text = !empty($mpn) ? '<span class="p-rewards">MPN: '.$text_mpn.'</span>'.$mpn.'<br />':null;
?>
Output:
MPN: Hello Ram Pukar
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>
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.
I need to change the output of <?php echo $EM_Category->name; ?> to lowercase only.
Output now is Entertainment. How would I get it to entertainment
Full section of code is:
<?php foreach(EM_Categories::get(array('orderby'=>'category_name')) as $EM_Category): ?>
<a href="<?php echo THEME_URL; ?>/events/category/<?php echo $EM_Category->name; ?>" class="browse-cat">
<span><?php echo $EM_Category->name; ?></span><img src="<?php echo THEME_URL; ?>/images/box_arrow.png" alt="arrow" height="15" width="15">
</a>
<?php endforeach; ?>
That would be the strtolower function.
<?php echo strtolower($EM_Category->name); ?>
<?php echo strtolower($EM_Category->name); ?>
http://us.php.net/strtolower
<?php echo strtolower($EM_Category->name); ?>
In PHP you use:
echo strtolower($EM_Category->name);