CaptainChainsaw Posted September 27, 2008 Share Posted September 27, 2008 Hi all, I'm trying to display three database records which when using print_r(); produces: Array ( [0] => Array ( [0] => Hello, World! ) [1] => Array ( [0] => Bonjour, Monde! ) [2] => Array ( [0] => Ciao, Mondo! ) ) What do I need to do to my code to get these values displayed on the screen? Where does $this->items come from? This explained in the API somewhere? View code: <?php jimport( 'joomla.application.component.view'); class HelloViewHello extends JView { function display($tpl = null) { $model =& $this->getModel(); $greetings = $model->getGreeting(); //$this->assignRef( 'greeting', $greetings[0]); print_r($greetings); $i=0; foreach($greetings as $greeting){ $this->assignRef( 'greeting', $greeting[0]); $i++; } parent::display($tpl); } } ?> Template code: <?php $n = count( $this->items ); // faster when outside the loop! echo $n; for( $i = 0; $i < $n; $i++ ) { $row =& $this->items[$i] ?> <div class="_what_you_want_"> <div class="_what_you_want_2_"> <?php echo $row->id; ?> </div> <div class="_what_you_want_3_"> <?php echo $row->greeting; ?> </div> </div> <?php $k = 1 - $k; } ?> Thnx in advance. CaptainChainsaw Quote Link to comment https://forums.phpfreaks.com/topic/126060-simple-mvc-problem-using-joomla/ Share on other sites More sharing options...
CaptainChainsaw Posted September 27, 2008 Author Share Posted September 27, 2008 Hi all, I got this working, I'm not sure how elegant my solution is. I can't seem to use $row->greeting, instead have to use $this->greeting[$i][greeting]. Is this how it's meant to be done or is the $row->greeting way better? I just couldn't get it to work. The data structure way doesn't look too neat. I also changed it to use an associative array when retrieving from the DB. the model: <?php // Check to ensure this file is included in Joomla! defined('_JEXEC') or die(); jimport( 'joomla.application.component.model' ); //jimport( 'joomla.application.component.helper' ); class HelloModelHello extends JModel { /** * Gets the greeting * @return string The greeting to be displayed to the user */ function getGreeting() { $db =& JFactory::getDBO(); $query = 'SELECT greeting FROM #__hello'; $db->setQuery( $query ); //$greeting = $db->loadResult(); //$greeting = $db->loadRowList(); $greeting=$db->loadAssocList(); return $greeting; } } ?> the view: <?php jimport( 'joomla.application.component.view'); class HelloViewHello extends JView { function display($tpl = null) { $model =& $this->getModel(); $greetings = $model->getGreeting(); $this->assignRef( 'greeting', $greetings); parent::display($tpl); } } ?> The template: <?php // no direct access defined('_JEXEC') or die('Restricted access'); ?> <?php $n = count( $this->greeting ); // faster when outside the loop! for( $i = 0; $i < $n; $i++ ) { $row =& $this->greeting[$i][greeting]; ?> <div class="_what_you_want_"> <div class="_what_you_want_2_"> <?php echo $this->greeting[$i][greeting] ?> </div> <div class="_what_you_want_3_"> <?php //echo $row->greeting; ?> </div> </div> <?php $k = 1 - $k; } ?> The above outputs: Hello, World! Bonjour, Monde! Ciao, Mondo! Thank you, n00ber Quote Link to comment https://forums.phpfreaks.com/topic/126060-simple-mvc-problem-using-joomla/#findComment-651951 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.