0

私のカスタム コンポーネントのサイト ビューには、国のリストがあります (ビュー: 国)。国をクリックすると、その国に住んでいるすべての人を示す別のビュー (ビュー: 人) が表示されます。

さて、人のビューで、国名と国旗を表示したいと思います。

だから私はに追加しfunction getCountry()たい...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; 
}

次に、に追加しました.../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
        (...)

---/site/views/persons/tmpl/default.php結果:次のようなことができると思いました...

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

...しかし、出力が得られません...何が間違っていましたか?

4

1 に答える 1

0

$db->loadResult();単一の値の結果をロードするために使用されます (たとえば、国名などのみを選択する場合) が、クエリでは行全体を選択しているため、次のいずれかを使用する必要があります。

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

詳細については、こちらをご覧ください(これは joomla 1.5 のドキュメントですが、2.5 と 3 でも同じです)。

于 2013-10-16T05:26:43.200 に答える