0

这是我的模型-我想分配行并在视图页面上回显,例如<?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

4 回答 4

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;
        }

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

于 2012-09-27T06:23:20.100 回答
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');
}
于 2012-09-27T06:22:11.207 回答
0

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

在您的模型函数中,在末尾放置一个 return 语句。

return $data;

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

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

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

于 2012-09-27T06:23:54.503 回答
0

I used @raheel's code and it works. But I would just change a bit:

   $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;
        }

This will help extracting the data

于 2013-04-05T18:40:41.900 回答