Jump to content

Simple MVC problem using Joomla


CaptainChainsaw

Recommended Posts

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.