Note that there are some explanatory texts on larger screens.

plurals
  1. POJoomla 2.5 Trying to get property of non-object error
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/5891911/trying-to-get-property-of-non-object-in">Trying to get property of non-object in</a> </p> </blockquote> <p>I've come upon a problem when developing a custom component for joomla 2.5.</p> <p>I'm pulling data out of my database through a MVC structure. Everything is working fine, except when the query doesn't return any results.</p> <p>I'm getting a <code>Trying to get property of non-object</code> error in my view.</p> <p>Is there someway I can prevend this and show users a nice message that there aren't any results?</p> <p>Model part:</p> <pre><code>$this-&gt;_gallery = array(); // Load the data if (empty( $this-&gt;_gallery )) { $query = ' SELECT #__gallery.id, user_id, merk, type, name, bouwjaar, vermogen, #__gallery_kleur.kleur, transmissie, #__gallery_benzine.soort AS brandstof, cilinders, motorinhoud, gewicht, link FROM #__gallery LEFT JOIN #__gallery_merk ON #__gallery.merk_id = #__gallery_merk.merk_id LEFT JOIN #__gallery_type ON #__gallery.type_id = #__gallery_type.type_id LEFT JOIN #__gallery_autoinfo ON #__gallery.id = #__gallery_autoinfo.galleryid LEFT JOIN #__gallery_kleur ON #__gallery_autoinfo.kleur = #__gallery_kleur.kleurid LEFT JOIN #__gallery_benzine ON #__gallery_autoinfo.brandstof = #__gallery_benzine.benzineid LEFT JOIN #__users ON #__gallery.user_id = #__users.id LEFT JOIN (SELECT * FROM #__gallery_foto ORDER BY #__gallery_foto.hoofdfoto ASC) as #__gallery_foto ON #__gallery.id = #__gallery_foto.galleryid GROUP BY #__gallery.id '; $this-&gt;_db-&gt;setQuery( $query ); $this-&gt;_db-&gt;query(); $num_rows = $this-&gt;_db-&gt;getNumRows(); $this-&gt;_gallery = $this-&gt;_db-&gt;loadObjectList(); } if (!$this-&gt;_gallery) { $this-&gt;_gallery = new stdClass(); $this-&gt;_gallery-&gt;id = 0; } $this-&gt;_total = count($this-&gt;_gallery); if ($num_rows == 0) { if ($this-&gt;getState('limit') &gt; 0) { $this-&gt;_gallery = array_splice($this-&gt;_gallery, $this-&gt;getState('limitstart'), $this-&gt;getState('limit') ); } } return $this-&gt;_gallery; </code></pre> <p>View:</p> <pre><code> // No direct access to this file defined('_JEXEC') or die('Restricted access'); // import Joomla view library jimport('joomla.application.component.view'); jimport( 'joomla.html.pagination' ); /** * HTML View class for the HelloWorld Component */ class GalleryViewGallery extends JView { // Overwriting JView display method function display($tpl = null) { $gallery = $this-&gt;get( 'Gallery' ); $this-&gt;assignRef( 'gallery', $gallery ); $this-&gt;items = $this-&gt;get('Gallery'); $this-&gt;pagination = $this-&gt;get('Pagination'); // Check for errors. if (count($errors = $this-&gt;get('Errors'))) { JError::raiseError(500, implode('&lt;br /&gt;', $errors)); return false; } // Display the view parent::display($tpl); } } </code></pre> <p>default.php:</p> <pre><code>// No direct access to this file defined('_JEXEC') or die('Restricted access'); //$document = &amp;JFactory::getDocument(); $document-&gt;addStyleSheet('components'.DS.'com_gallery'.DS.'css'.DS.'gallery.css'); $document = &amp;JFactory::getDocument(); $document-&gt;addStyleSheet('components/com_gallery/css/gallery.css'); ?&gt; &lt;p&gt; In onderstaande overzicht wordt overzicht van galleries getoond, ben op je zoek naar iets specifieks ?. Gebruik dan het zoekscherm aan de linkerkant van deze pagina om een aangepast overzicht te tonen. &lt;/p&gt; &lt;?php $count = '1'; foreach($this-&gt;gallery as $gallery){ if($count == '0'){ echo 'Geen resultaat'; }else{ ?&gt; &lt;div class="gallery_wrapper"&gt; &lt;?php if($gallery-&gt;merk OR $gallery-&gt;type &gt; Null){ echo "&lt;div class=\"gallery_merktype\"&gt;"; echo "&lt;a href='gallerie?view=gallerydetail&amp;amp;gallery=" . $gallery-&gt;id . "' target='_self'&gt;"; echo "&lt;H1&gt;"; echo $gallery-&gt;merk; echo " "; echo $gallery-&gt;type; echo "&lt;/H1&gt;"; echo "&lt;/a&gt;"; echo "&lt;/div&gt;"; } //Weergeven afbeelding, bepaling maximale hoogte en breedte gebeurt in CSS. if($gallery-&gt;link &gt; Null){ echo "&lt;div class=\"gallery_foto\"&gt;"; echo "&lt;div class=\"gallery_foto_clip\"&gt;"; echo "&lt;a href='gallerie?view=gallerydetail&amp;amp;gallery=" . $gallery-&gt;id . "' target='_self'&gt;&lt;img src='../galleryimage/thumb/" . $gallery-&gt;link . "' width='200px' alt='Afbeelding'/&gt;&lt;/a&gt;"; echo "&lt;/div&gt;&lt;/div&gt;"; } ?&gt; &lt;div class="gallery_wrapper_info"&gt; &lt;?php //Weergeven gallery gegevens. if($gallery-&gt;bouwjaar &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Bouwjaar:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;bouwjaar; echo "&lt;/div&gt;"; } if($gallery-&gt;vermogen &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Vermogen:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;vermogen; echo "&lt;/div&gt;"; } if($gallery-&gt;kleur &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Kleur:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;kleur; echo "&lt;/div&gt;"; } if($gallery-&gt;transmissie &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Transmissie:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;transmissie; echo "&lt;/div&gt;"; } if($gallery-&gt;brandstof &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Brandstof:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;brandstof; echo "&lt;/div&gt;"; } if($gallery-&gt;cilinders &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Cilinders:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;cilinders; echo "&lt;/div&gt;"; } if($gallery-&gt;motorinhoud &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Motorinhoud:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;motorinhoud; echo "&lt;/div&gt;"; } if($gallery-&gt;gewicht &gt; Null){ echo "&lt;div class=\"gallery_info_title\"&gt;Gewicht:&lt;/div&gt;"; echo "&lt;div class=\"gallery_info_values\"&gt;"; echo $gallery-&gt;gewicht; echo "&lt;/div&gt;"; } ?&gt; &lt;div class="gallery_bekijken"&gt; &lt;H2&gt;&lt;a href="index.php/gallerie?view=gallerydetail&amp;amp;gallery=&lt;?php echo $gallery-&gt;id; ?&gt;" target="_self"&gt;Bekijk Gallery&lt;/a&gt;&lt;/H2&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;?php } } ?&gt; &lt;form method="post" name="limiet"&gt; &lt;?php echo $this-&gt;pagination-&gt;getPagesLinks(); ?&gt; &lt;center&gt; &lt;?php echo $this-&gt;pagination-&gt;getLimitbox(); ?&gt; &lt;/center&gt; &lt;/form&gt; </code></pre> <hr> <p>Great!, I've tried it in the way below and it worked for me:</p> <pre><code>if(is_object($this-&gt;gallery)){ echo "No result"; }else{ //DATA } </code></pre> <p>For the record:</p> <p>A print_r of a filled array:</p> <pre><code>Array ( [0] =&gt; stdClass Object ( [id] =&gt; 146 [user_id] =&gt; 861 [merk] =&gt; Seat [type] =&gt; Leon [name] =&gt; Danny [bouwjaar] =&gt; 2004 [vermogen] =&gt; 190 [kleur] =&gt; Geel [transmissie] =&gt; Handgeschakeld [brandstof] =&gt; Diesel [cilinders] =&gt; 4 [motorinhoud] =&gt; 1900 [gewicht] =&gt; 1210 [link] =&gt; p1793sjnq51u373r12qdobs1d2b2.jpg ) ) </code></pre> <p>And a empty one:</p> <pre><code>stdClass Object ( [id] =&gt; 0 ) </code></pre> <p>Although there was something I missed earlier, in the model I adjusted the limit variable to '0' instead of the default '5' for testing.</p> <p>If I put it back to '5' there is a array_splice error I'm getting when the query doesn't return any results. It works perfectly when there is data.</p> <p>Maybe I'm doin somethin wrong here also ?</p> <p>The piece of code in my model:</p> <pre><code>$this-&gt;_gallery = array(); // Load the data if (empty( $this-&gt;_gallery )) { $query = ' SELECT #__gallery.id, user_id, merk, type, name, bouwjaar, vermogen, #__gallery_kleur.kleur, transmissie, #__gallery_benzine.soort AS brandstof, cilinders, motorinhoud, gewicht, link FROM #__gallery LEFT JOIN #__gallery_merk ON #__gallery.merk_id = #__gallery_merk.merk_id LEFT JOIN #__gallery_type ON #__gallery.type_id = #__gallery_type.type_id LEFT JOIN #__gallery_autoinfo ON #__gallery.id = #__gallery_autoinfo.galleryid LEFT JOIN #__gallery_kleur ON #__gallery_autoinfo.kleur = #__gallery_kleur.kleurid LEFT JOIN #__gallery_benzine ON #__gallery_autoinfo.brandstof = #__gallery_benzine.benzineid LEFT JOIN #__users ON #__gallery.user_id = #__users.id LEFT JOIN (SELECT * FROM #__gallery_foto ORDER BY #__gallery_foto.hoofdfoto ASC) as #__gallery_foto ON #__gallery.id = #__gallery_foto.galleryid GROUP BY #__gallery.id '; $this-&gt;_db-&gt;setQuery( $query ); $this-&gt;_gallery = $this-&gt;_db-&gt;loadObjectList(); } if (!$this-&gt;_gallery) { $this-&gt;_gallery = new stdClass(); $this-&gt;_gallery-&gt;id = 0; } $this-&gt;_total = count($this-&gt;_gallery); if ($this-&gt;getState('limit') &gt; 0) { $this-&gt;_gallery = array_splice($this-&gt;_gallery, $this-&gt;getState('limitstart'), $this-&gt;getState('limit') ); } return $this-&gt;_gallery; } </code></pre> <p>The error in my view when there are no results and limit set to 5:</p> <pre><code>Warning: array_splice() expects parameter 1 to be array, object given in </code></pre> <p>Print_R isn't giving back results, but this is obvious I quess.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload