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.
Related
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
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.
I have a view that is outputting a list of nodes that contain a link, image and text.
I want to use the link field to wrap the output (rather than link to the node), but I can't figure out how to get the raw url/title etc to create create that link in the template.
The field is configured to output the url as plain text, but is wrapped in div/spans regardless of the style settings for the view field.
views-view-fields--my-view.php:
<a href="<?php echo $fields['field_link']->content ?>">
<?php foreach ($fields as $id => $field):
if ($id == 'field_link') continue;
?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<?php print $field->wrapper_prefix; ?>
<?php print $field->label_html; ?>
<?php print $field->content; ?>
<?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>
</a>
This produces:
<a href="<div class=" data-thmr="thmr_72"><div class="field-items"><div class="field-item even"><span data-thmr='thmr_24' class='devel-themer-wrapper'>/drupal/%237digital-buy</span></div></div></div>">
[...]
</a>
Which is obviously not what I need.
Found the solution. You need to rewrite the output. See below.
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 want to make the joomla articles intro image to behave like the read more, and the title link. So the user clicks the image, and the article loads.
I'm not an PHP expert but maybe this is the readmore links code:
<a href="<?php echo $this->item->readmore_link; ?>" class="button<?php echo $this->item->params->get('pageclass_sfx'); ?>">
<?php if ($this->item->readmore_register) :
echo JText::_('Register to read more...');
elseif ($readmore = $this->item->params->get('readmore')) :
echo $readmore;
else :
echo JText::_("Read Article");
endif; ?></a>
This is what i want to do with every intro image on my joomla site.
Thanks !
Just resolved it!
your way of thinking helped me. Thank you!
here's my code:
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>">
<?php
$images = json_decode($item->images);
if (isset($images->image_intro) and !empty($images->image_intro)) {
$imgfloat = (empty($images->float_intro)) ? $params->get('float_intro') : $images->float_intro;
$class = (htmlspecialchars($imgfloat) != 'none') ? ' class="size-auto align-'.htmlspecialchars($imgfloat).'"' : ' class="size-auto"';
$title = ($images->image_intro_caption) ? ' title="'.htmlspecialchars($images->image_intro_caption).'"' : '';
echo '<img'.$class.$title.' src="'.htmlspecialchars($images->image_intro).'" alt="'.htmlspecialchars($images->image_intro_alt).'" />';
}
echo $this->item->introtext;
?>
</a>
for Joomla 2.5:
in your override for _item.php (Location: yourtemplate\html\mod_articles_news\item.php)
place the following line:
<?php if ($params->get('image')) : ?>
<?php $images = json_decode($item->images); ?>
<?php if (isset($images->image_intro) and !empty($images->image_intro)) : ?>
<img src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
<?php endif; ?>
<?php endif; ?>
Place it there where you would like it to show up
For example after:
<?php echo $item->beforeDisplayContent; ?>
Your intro image has become a link now.
the isset part, makes sure that if a viewer uses Internet Explorer, it doesn't show up a small red cross box.
Just for the information:
in blog_item.php you can find an example code how it's shown up in an article. Here you can also find the code for imagefloat etc.
<?php if (isset($images->image_intro) and !empty($images->image_intro)) : ?>
<?php $imgfloat = (empty($images->float_intro)) ? $params->get('float_intro') : $images->float_intro; ?>
<div class="img-intro-<?php echo htmlspecialchars($imgfloat); ?>">
<img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
</div>
<?php endif; ?>
So let me start by explaining what the code that you've posted above does. The entire block of code generates one link: there are a bunch of if statements that are determined based off some settings. For example, if you have set that people need to register in order to read more, the link will say "Register to read more..."
The part that we're interested in here, however, since we want to turn images into links, is the URL that we want the images to link to. This is right in the first line:
<a href="<?php echo $this->item->readmore_link; ?>"
so we know that the URL is provided dynamically thanks to $item->item->readmore_link and all this code is doing is echoing it into the HTML.
All that's left is to edit your Joomla template of the page on which you have your images (probably the same file you took this code from). It looks like this should be part of a greater PHP loop, which loops through all the posts. Somewhere above where you found this code, should be code for the intro image that goes along with that post.
I'm not sure what it'll look like, it could be a <img src="<? stuff here; ?> /> or it could be dynamically generated. Keep reading. If you're still not sure where to find it at the end, edit your post with the full code of the template where you got the above snipping from. Regardless of what it looks like, it is referred to as <WHATEVER IMAGE CODE YOU FOUND ABOVE> in the following step:
You have to wrap that image with "a" tags so that it looks like the following:
<WHATEVER IMAGE CODE YOU FOUND ABOVE>
That should do it. Let me know if you have any trouble, I'll be more than happy to make my post more specific if you can provide more detailed information, but I've tried to explain it well enough that you should be able to figure it out with a couple tries.
As you stated you are not a PHP expert, it sounds like your best bet will be to use a Joomla extension that has similar functionality to what you want.
I believe mod_minifrontpage will work for what you need. It allows you to display a list of articles, and it generates thumbnails for those articles based on the first image to be referenced.
There are article intro images in J, since 1.7.5 and now in latest 2.5.3
what you need is change the defaults for component_content,
you can do it 2 ways, editing views in yourinstall/components/com_content/views/
or use template overrides , you first need to know if your template IS using overrides otherwise if you edit component views in the component itself you will not see changes.
to verify this , go to
site_name/templates/template_name/html folder and check if there is folder name
com_content ,
if that is the case than your template is using overrides and any edits should be done trough there not through component
now to the actual code
this is in
components\com_content\views\featured\tmpl\default_item.php ( THIS I DEFAULT FRONTPAGE ARTICLE VIEW)
<?php if (isset($images->image_intro) and !empty($images->image_intro)) : ?>
<?php $imgfloat = (empty($images->float_intro)) ? $params->get('float_intro') : $images->float_intro; ?>
<div class="img-intro-<?php echo htmlspecialchars($imgfloat); ?>">
<img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
</div>
<?php endif; ?>
all you would need to do is wrap a element around IMG tag with readmore link like this
<a href="<?php echo $this->item->readmore_link; ?>">
<img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/>
</a>
DO NOT forget that if there is template override for com_content you wold need to edit the featured/default_item.php inside it
In Joomla 3.1, the intro_image layout has been moved to the layouts/joomla/content folder. In my situation, it is called from com_content/views/category/tmpl/blog_item.php like so:
<?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
I moved that file to my template/html/com_content/category/blog_item.php and then wrapped the call to JLayoutHelper like so:
<?php $link = JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>
<a href="<?php echo $link; ?>">
<?php echo JLayoutHelper::render('joomla.content.intro_image', $this->item); ?>
</a>
If you have Gantry installed on Joomla 3.1, the overrides are in a different location. You will want to navigate to plugins/system/gantry/overrides/3.0/2.5/com_content/category/blog_item.php and wrap the intro image with the read more link code.
<?php $link = JRoute::_(ContentHelperRoute::getArticleRoute($this->item->slug, $this->item->catid)); ?>
<a href="<?php echo $link; ?>"><img
<?php if ($images->image_intro_caption):
echo 'class="caption"'.' title="' .htmlspecialchars($images->image_intro_caption) .'"';
endif; ?>
src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>"/></a>