Pergunta

In my custom component, in site view, I have a list of countries (view: countries). Clicking on a country, another view is displayed (view: persons) showing all persons living in that country.

Now, in persons view, I want to display the country's name and flag.

So I want to add a function getCountry() in ...site/models/persons.php:

public function getCountry() {
    $db = $this->getDBO(); 
    $id = $this->state->get("filter.country");
    $sql = "SELECT * FROM #__cnlp_persons_country WHERE id = $id";
    $db->setQuery($sql); 
    $country = $db->loadResult(); 
    // var_dump($country);
    return $country; 
}

Then, I added to .../site/views/persons/view.html.php:

class Cnlp_personsViewPersons extends JView
{
    protected $items;
    protected $state;
    protected $params;
    protected $country; // <--- I added this
    ...

    public function display($tpl = null)
{
        $app                = JFactory::getApplication();
        $this->state        = $this->get('State');
        $this->items        = $this->get('Items');
        $this->params               = $app->getParams('com_cnlp_trainers');
        $this->country              = $this->get('Country'); // <--- I added this
        (...)

Result: I thought I could then in ---/site/views/persons/tmpl/default.php something like...

<h1><?php echo $this->country->name; ?></h1>
<img src="<?php echo $this->country->flag; ?>" />

...but I get no output... What did I do wrong?

Foi útil?

Solução

$db->loadResult(); is used to load a single value results (for example if you would only want to select the country name or something like this), but in your query you're selecting an entire row, so you should use one of theese:

$db->loadRow(); // returns an indexed array from a single record in the table
$db->loadAssoc(); // returns an associated array from a single record in the table
$db->loadObject(); // returns a PHP object from a single record in the table:

You can read more about this here (it's joomla 1.5 documentation, but it's same for 2.5 and 3)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top