I get images in a array then i read it with a foreach this are images, but how can i get also image text, my example is here:
$slideImages = array('image/path/image1', 'image/path/image2', 'image/path/image13');
$slideText = array('image text 1', 'image text 2', 'image text 3');
if($slideImages) :
echo '<ul>';
foreach($slideImages as $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
</li>
<?php
endforeach;
echo '</ul>';
endif;
I need to embed also image text from the true position, i know how to do it with for but so is not efficient... it's something like this possible?
Assuming the keys are the same, use the $key in the foreach:
foreach($slideImages as $key => $slideImage) :
?>
<li>
<div>IMAGE TEXT</div>
<img src="<?php echo $slideImage; ?>" />
<?php echo $slideText[$key]; ?>
</li>
<?php
endforeach;
You could also change the way you store the info and keep them in one multidimensional array.
$slides = array(
array( image => "image/path/image1", text => "image text 1"),
array( image => "image/path/image2", text => "image text 2"),
array( image => "image/path/image3", text => "image text 3")
);
Then you would iterate through the $slides array and just pull out the image and text for each:
foreach($slides as $slide)
{
echo "Text:".$slide["text"];
echo "Image Path:".$slide["image"];
}
This organizes your code a little better so you don't wind up with something crazy going on should your two arrays wind up out of sync.
simplified example:
foreach($slideImages as $key=>$var){
echo $var;
echo $slideText[$key];
}
Related
I add images to the page, to which I add some attributes
<?php
$data = [
[
'data-z-index' => 1,
'data-width' => 300,
]
];
?>
<?php foreach ($posts as $i => $item) { ?>
<div class="item">
<?php if ($item->img) { ?>
<?= Html::img($item->img->getUrl(), $data[$i]) ?>
<?php } ?>
</div>
<?php } ?>
As a result, on the page all this works for me and I get
<img src="//test.loc/storage/posts-image/1-2.jpg" alt="" data-z-index="1" data-width="300">
Now I also want to add an alt attribute that will come from the database
<?= Html::img($item->img->getUrl(), [$data[$i], 'alt' => $item->img_alt]) ?>
But now the attribute formatting is changing and 0 appears at the beginning
<img src="//test.loc/storage/posts-image/1-2.jpg" alt="post1" 0-data-z-index="1" 0-data-width="300">
What could be the problem?
It's because $data is an array. So you have a nested array as options.
Try to merge the arrays:
array_merge($data[$i], ['alt' => $item->img_alt]);
i am trying to display a list of image from some link within a array of hashes. beside some other image from my own folder. Thanks in advance
$config = [
'gallery_name' => 'my_heading',
'unsplash_categories' => ['array','of','category','keywords'],
'local_images' =>
['folder/jpg1.jpg','folder/jpg2.jpg','folder/jpg1.jpg','folder/jpg4.jpg']
];
in HTML, what tried.
<?php foreach($config as $my_config => $gallery_name): ?>
<h1><?php echo $gallery_name; ?></h1>
<?php endforeach ?>
<p>
<?php foreach($config as $img)
{
foreach($img['local_images'] as $local_img)
{
echo "<img src='".$local_img['']."' alt=''>.<br>";
}
foreach($img['unsplash_categories'] as $unsplash_img)
{
echo "<img src='".$unsplash_img['']."' alt=''>.<br>";
}
}
?>
</p>
Welcome on SO, Shaik.
Not clear on what you want to exactly achieve but if you just want to display the images using your $config as sample, you can simplify it a bit to something like this:
<h1><?= $config["gallery_name"]; ?></h1>
<p>
<?php
foreach($config['local_images'] as $local_img)
{
echo "<img src='" . $local_img . "' alt=''>.<br>";
}
//same for unsplash_categories...
?>
</p>
I'm getting list of image using foreach, but I don't want to display first image of array, but I don't know how to make it works. Here my code:
$portfolio_gallery_image = get_post_meta( get_the_ID(), "portfolio_gallery_image", true );
<div class="folio-gallery grid-masonry clearfix">
<?php
foreach ( $portfolio_gallery_image as $key => $image ){
?>
<a class="folio-item col-3 ndSvgFill grid-item" href="<?php echo esc_url($image) ?>">
<img src="<?php echo esc_attr($image) ?>" alt="portfolio">
</a>
<?php } ?>
</div>
This is array
Array
(
[2098] => http://dione.thememove.com/wp-content/uploads/2016/04/11.jpg
[2097] => http://dione.thememove.com/wp-content/uploads/2016/04/10.jpg
[2062] => http://dione.thememove.com/wp-content/uploads/2016/06/f_08.jpg
[2084] => http://dione.thememove.com/wp-content/uploads/2016/06/10.jpg
[2096] => http://dione.thememove.com/wp-content/uploads/2016/04/9.jpg
[2082] => http://dione.thememove.com/wp-content/uploads/2016/06/08.jpg
[2094] => http://dione.thememove.com/wp-content/uploads/2016/04/7.jpg
)
Really appreciate your help. Thank you.
You can do like this,
<?php
function custom_function(&$arr)
{
list($k) = array_keys($arr);
$r = array($k => $arr[$k]);
unset($arr[$k]);
return $r;
}
custom_function($portfolio_gallery_image);
foreach ($portfolio_gallery_image as $key => $image) {
?>
<a class="folio-item col-3 ndSvgFill grid-item" href="<?php echo esc_url($image) ?>">
<img src="<?php echo esc_attr($image) ?>" alt="portfolio">
</a>
<?php } ?>
array_shift Shift an element off the beginning of array.
Try below code to remove first key/value from array before for each in your code.
$new_image_array = array_shift($portfolio_gallery_image);
Hello I need some help getting the image URL of the itunes:image attribute from my XML rss feed. I can retrieve everything else in the array, but not 'mainimg'. The current code returns nothing for the image although it's there. Do you have any idea how I can do this, I'm looking for the HREF so I can place it inside 'img' tags.
<?php
$doc = new DOMDocument();
$doc->load('http://rss.acast.com/globalpillage');
$cnt=0;
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'maintitle' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'),
'mainimg' => $node->getElementsByTagName('image')->item(0)->getAttribute('url')/*This is what I'm trying to call the itunes:image attribute(HREF) with*/,
);
?>
<div class="showcontent">
<img src="<?php echo $itemRSS['image']; /*echo the itunes:image attribute(HREF)*/ ?>">
<h2><?php echo $itemRSS['title']; ?></h2>
<p><strong>Published</strong> <?php echo $itemRSS['date']; ?></p>
<audio controls>
<source src="<?php echo $itemRSS['enclosure']; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
<?php $cnt ++; } ?>
// Get namespace URI for prefix `itunes`
echo $ns = $s->lookupNamespaceURI('itunes');
// Get `image` tag from that namespace
echo $href = $node->getElementsByTagNameNS($ns, 'image')->item(0)->getAttribute('href');
If anyone stumbles upon this, I could not get the comment nor the answer to work. I think the comment should have $doc not $dom to mimic the example code, but that resulted in just the image for the podcast each time, not the "items". It looked like this:
echo $href = $doc->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href');
But when I changed to $node in the loop, I received the correct image:
echo $href = $node->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href');
Also, there are a few other typos I came across in the example code like storing $item['maintitle'] but echoing $item['title']
I could never get the accepted answer to work. In the end, this is what my code looked like this when I was scraping an Anchor podcast RSS feed:
<?php
$doc = new DOMDocument();
$doc->load('https://anchor.fm/s/xxxxxxx/podcast/rss');
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'enclosure' => $node->getElementsByTagName('enclosure')->item(0)->getAttribute('url'),
'mainimg' => $node->getElementsByTagNameNS('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image')->item(0)->getAttribute('href')
);
?>
<div class="showcontent">
<img src="<?php echo $itemRSS['mainimg']; ?>">
<h2><?php echo $itemRSS['title']; ?></h2>
<p><strong>Published</strong> <?php echo $itemRSS['date']; ?></p>
<audio controls>
<source src="<?php echo $itemRSS['enclosure']; ?>" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
<?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";
}