Codeigniter模型foreach将数组传递给控制器​​ - >查看

时间:2012-09-27 06:20:24

标签: php codeigniter

这是我的模型 - 我想在视图页面上分配行和回显,例如<?php echo $product?>     

class Model_products extends CI_Model {

    function printer()
    {

    $id = $this->uri->segment(3, 0);

        $q = $this->db->get_where('printer', array('id' => $id));

        if ($q->num_rows == 1)
        { 
            foreach ($q->result() as $row)
            {

                echo $row->name; # THIS WORKS
                $data['product'] = $row->name;  # THIS DOES NOT WORK
                $data['desc'] = $row->description;
                $data['size'] = $row->size;
                $data['options'] = $row->options;
                $data['more'] = $row->more_info;
            }
        }


    }
}

这是我的控制器

public function products()
{

    $this->load->model('Model_products');

    $this->Model_products->printer();

    $this->load->view('products', $data);

    $this->load->view('pricing');
}

我的观点是

<h1>
<?php echo $product; ?>
</h1>

<p>
<?php echo $desc; ?>
</p>


<h3>
Size
</h3>
<p>
<?php echo $size; ?>
</p>

如何正确地将我的模型数组传递给控制器​​,以便能够回显我的视图?

4 个答案:

答案 0 :(得分:4)

只需将$ data返回控制器

class Model_products extends CI_Model {

    function printer()
    {
        $data   =   array();
        $id = $this->uri->segment(3, 0);

        $q = $this->db->get_where('printer', array('id' => $id));

        if ($q->num_rows == 1)
        { 
            foreach ($q->result() as $row)
            {

               // echo $row->name; # THIS WORKS
                $data['product'] = $row->name;  # THIS DOES NOT WORK
                $data['desc'] = $row->description;
                $data['size'] = $row->size;
                $data['options'] = $row->options;
                $data['more'] = $row->more_info;
            }
        }

    return $data;
    }
}

没有赢得控制器

public function products()
{

    $this->load->model('Model_products');

    $data['products']   =   $this->Model_products->printer();

    $this->load->view('products', $data);

    $this->load->view('pricing');
}

在视野中

<?   
   echo '<pre>';
   print_r($products);
?>

编辑同样更正您的型号代码。

        foreach ($q->result() as $row)
        {

            echo $row->name; # THIS WORKS
            $data[]['product'] = $row->name;  # THIS DOES NOT WORK
            $data[]['desc'] = $row->description;
            $data[]['size'] = $row->size;
            $data[]['options'] = $row->options;
            $data[]['more'] = $row->more_info;
        }

如果您不使用多维数组,它将覆盖最后一条记录。

答案 1 :(得分:0)

你的退货声明

if ($q->num_rows == 1)
    { 
        foreach ($q->result() as $row)
        {

            echo $row->name; # THIS WORKS
            $data['product'] = $row->name;  # THIS DOES NOT WORK
            $data['desc'] = $row->description;
            $data['size'] = $row->size;
            $data['options'] = $row->options;
            $data['more'] = $row->more_info;
        }
        return $data;
    }

和/或在您的控制器中

public function products()
{ 
   $this->load->model('Model_products');
   $data = $this->Model_products->printer();
   $this->load->view('products', $data);
   $this->load->view('pricing');
}

答案 2 :(得分:0)

您需要将模型中的数据返回给控制器,然后从那里将数据传递给视图。

在你的模型函数中,在最后放置一个return语句。

return $data;

然后在您的控制器中,将模型中返回的值分配给变量。

$data = $this->Model_products->printer();

这两个步骤可以解决您的问题。

答案 3 :(得分:0)

我使用了@ raheel的代码并且它有效。但我只想改变一下:

   $i=0;
   foreach ($q->result() as $row)
        {


            echo $row->name; # THIS WORKS
            $data[$i]['product'] = $row->name;  # THIS DOES NOT WORK
            $data[$i]['desc'] = $row->description;
            $data[$i]['size'] = $row->size;
            $data[$i]['options'] = $row->options;
            $data[$i]['more'] = $row->more_info;
        }

这将有助于提取数据