0

我知道我的问题是我在控制器 $name 和 $year 中调用变量的位置,我不确定如何将该数组格式化为控制器中的变量。

模型:

function getId($id)
{
    $this->db->distinct();
    $this->db->select('*');
    $this->db->where('id', $id);
    $result = $this->db->get('HWC');

    return $result->result();
}

function getVariations($name, $year)
{
    $this->db->distinct();
    $this->db->select('*');
    $this->db->where(array('ModelName' => $name, 'Year' => $year));
    $variations = $this->db->get('HWC');

    if(mysql_num_rows($variations) != 0)
    {
        return $variations->result();
    }
    else
    {

    }
}

控制器:

$this->load->model('testingsearch');
$data['records'] = $this->testingsearch->getId($id);

$name = $records->ModelName;
$year = $records->Year;

$data['variations'] = $this->testingsearch->getVariations($name, $year);

看法:

 foreach($variations as $row){
    echo $row['name'];
    echo $row['color'];
    echo $row['Tampo'];
 }
4

1 回答 1

0
function getId($id)
{
    $this->db->distinct();
    $this->db->select('*');
    $this->db->where('id', $id);
    $result = $this->db->get('HWC');
    return $result->result();
}

You are returning object with result() but displaying as array

foreach($variations as $row){
    echo $row['name'];
    echo $row['color'];
    echo $row['Tampo'];
}

改成这样

return $result->result_array();

现在视图将正常工作。在控制器中,您在这里犯了错误

$data['records'] = $this->testingsearch->getId($id);

将其更改为

$records    =   $this->testingsearch->getId($id);

现在这些说明应该可以正常工作但有条件

if($records){
    $name = $records->ModelName;
    $year = $records->Year;

    $data['variations'] = $this->testingsearch->getVariations($name, $year);
}
于 2013-04-22T09:12:23.893 回答