Display an article image from within a custom module

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
RyanMcGovern19
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 18, 2014 3:15 am

Display an article image from within a custom module

Post by RyanMcGovern19 » Tue Nov 18, 2014 3:31 am

Hello,

I have just started using Joomla and I really like what I've seen so far!

I have a question regarding module development. I have created a simple module that grabs posts in the news category from my site then displays them in a module on the home page. Everything is working fine except I don't know how to display a thumbnail from the post. For example, in my view file, I have some code like this:

Code: Select all

<?php defined('_JEXEC') or die('Restricted access'); ?>
<div class="grid-6">
	<h4>News about tea...</h4>
	<ul class="news-wrap">
	<?php foreach($rows as $row) { ?>
	  	<li class="clearfix">
		  	<h5><a href="<?php echo $row->link; ?>"><?php echo $row->title; ?></a></h5>
		  	<small class="post-date"><?php echo $row->created; ?></small>
		  	<a href="<?php echo $row->link; ?>"><img src="<?php echo $row->images; ?>" alt="Post image" /></a>
		  	<p><?php echo limit_words($row->introtext, 24); ?> <a href="<?php echo $row->link; ?>">Read more &raquo;</a></p>
	  	</li>
	<?php } ?>
	</ul>
</div> <!-- end grid-6 -->
As you can see, I am getting rows from my database with $rows->item_in_database. However, this doesn't work for images. In the database, in the content table, there is a row called images but calling "images" directly doesn't work. So what code should I use to display the image attached to the post?

Any help would be appreciated.

Thanks :D

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: Display an article image from within a custom module

Post by sovainfo » Tue Nov 18, 2014 4:05 am

Have a look at how images are processed in components/com_content/views/article/tmp/default.php.
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

RyanMcGovern19
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 18, 2014 3:15 am

Re: Display an article image from within a custom module

Post by RyanMcGovern19 » Tue Nov 18, 2014 4:46 am

sovainfo wrote:Have a look at how images are processed in components/com_content/views/article/tmp/default.php.
Thanks for your reply. I did have a look and try imitating that approach though it didnt work. I got an error message saying "trying to use $this when not in an object context". I've only been using PHP for about 1 year and my skills and understanding are quite low. I imagine I may have to modify my whole approach to make this work.

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: Display an article image from within a custom module

Post by sovainfo » Tue Nov 18, 2014 4:55 am

$this->item referres to the article, you shoud use the variable that contains the article in your module.
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

RyanMcGovern19
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 18, 2014 3:15 am

Re: Display an article image from within a custom module

Post by RyanMcGovern19 » Tue Nov 18, 2014 5:33 am

sovainfo wrote:$this->item referres to the article, you shoud use the variable that contains the article in your module.
Thanks. Again, my lack of experience here is holding me back. I really don't which variable is holding the article or how to rewrite it to work.

I tried:

Code: Select all

<?php defined('_JEXEC') or die('Restricted access'); ?>
<div class="grid-6">
	<h4>News about tea...</h4>
	<ul class="news-wrap">
	<?php foreach($rows as $row) { 
	$images  = json_decode($row->item->images);
	?>
	  	<li class="clearfix">
		  	<h5><a href="<?php echo $row->link; ?>"><?php echo $row->title; ?></a></h5>
		  	<small class="post-date"><?php echo $row->created; ?></small>
		  	<a href="<?php echo $row->link; ?>"><img src="<?php echo htmlspecialchars($images->image_fulltext); ?>" alt="Post image"/></a>
		  	<p><?php echo limit_words($row->introtext, 24); ?> <a href="<?php echo $row->link; ?>">Read more &raquo;</a></p>
	  	</li>
	<?php } ?>
	</ul>
</div> <!-- end grid-6 -->
But that just generates an error:
Notice: Undefined property: stdClass::$item in /Applications/MAMP/htdocs/tea-life/modules/mod_news/tmpl/default.php on line 6
So obviously I have to have created some additional code somewhere which declares the $item property or other such property.

I guess I'll work this out eventually. It's just something I'd like to clear up :D

Thanks

sovainfo
Joomla! Exemplar
Joomla! Exemplar
Posts: 8808
Joined: Sat Oct 01, 2011 7:06 pm

Re: Display an article image from within a custom module

Post by sovainfo » Tue Nov 18, 2014 6:22 am

The $row is your article, so you need to loose ->item
Issue with migrating? Include logs/joomla_update.php in your report!
Blank screen? Verify pagesource for HTML code (javascript error)
Installation failing on populating database? Install with set_time_limit(0)
Document your customizations!

RyanMcGovern19
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Tue Nov 18, 2014 3:15 am

Re: Display an article image from within a custom module

Post by RyanMcGovern19 » Tue Nov 18, 2014 7:24 am

sovainfo wrote:The $row is your article, so you need to loose ->item
Great! Thanks. So I got it to display the image using the following code:

Code: Select all


	<?php foreach($rows as $row) { 
	$images  = json_decode($row->images);
	?>
	  	<li class="clearfix">
		  	<h5><a href="<?php echo $row->link; ?>"><?php echo $row->title; ?></a></h5>
		  	<small class="post-date"><?php echo $row->created; ?></small>
		  	<a href="<?php echo $row->link; ?>"><img src="<?php echo htmlspecialchars($images->image_intro); ?>" alt="Post image"/></a>
		  	<p><?php echo limit_words($row->introtext, 24); ?> <a href="<?php echo $row->link; ?>">Read more &raquo;</a></p>
	  	</li>
	<?php } ?>

Appreciate the help. I have a few more questions but I should probably start a new thread for those.

Cheers :D


Locked

Return to “Joomla! 3.x Coding”