Calling a model function in the codeigniter field of view

I am new to MVC, I am transferring a project written in a non-MVC style to MVC, but I am stuck in a problem when it is necessary to call a model function in a view.

Scenario:

Table 1 - Products:
contains product_id, product_nameetc., and for each product there may be several versions.

Table2 - Versions:
contains version_id, version_name..., product_idetc.

Now, in the view, I show the products, and under each heading of the product I have to display a list of versions of this product, in a style different from MVC, it was quite simple, I can use the following code fragment in the view:

foreach ($product as $row) 
{
    echo $row['product_name'];
    if ($main->getVersionList($vresult,$row["product_id"]))
    {
        foreach ($vresult as $vrow)
        {
          echo $vrow['version_name'];
        }
    }
}

Product , Version, , ?

Update:

( ), :

        $this->load->model ( 'product_mod' );
        $data ['products'] = $this->product_mod->getProductList ();
        $data ['versions'] = array ();
        foreach ( $data ['products'] as $product )
        {
            $data ['versions'] [$product['product_id']] =   $this->product_mod->getVersionList ( $product['product_id'] );
        }
+4
3

MVC MVC

, , , MVC PHP. MVC- PHP, CodeIgniter Yii, MVP, :

  • () ,

tereΕ‘ko

CodeIgniter

, CodeIgniter, 3 :

  • ( ).
  • , ( )
  • PHP, , HTML.

, :

//product.php

class Product extends CI_Model
{
    public function get_product($product_id)
    {
        $this->db->select('*')->from('products');
        $this->db->where('product_id', $product_id);
        $this->db->join('versions', 'versions.product_id = products.product_id');
        $query=$this->db->get();
        return $query->first_row('array');
    }
}

:

//products.php

class Products extends CI_Controller
{
    public function view($product_id)
    {
        $this->load->model('product');
        // Fetch the result from the database
        $data['product'] = $this->product->get_product($product_id);
        // Pass the result to the view
        $this->load->view('product_view', $data);
    }
}

, , :

//product_view.php

// Use $product to display the product.
print_r($product);
+7

mysql , , product_model ( )

: *

public function All_Products()
{
$this->db->select('');
$result = $this->db->get('products')->result_array();
return $result;
}

, , , , .

$products .

$products = $this->products_model->All_Products();

, . getVersionList .

$newArray = array ();
foreach ( $products as $values ) {
$version = $this->products_model->getVersionList($values['product_id']);
$tmp = array (
'product_id' => $values ['product_id'], 
'product_name' => $values ['product_name'], 
'version' => $version
);
array_push ( $newArray, $tmp );
}

, $data, .

$data ['products'] = $newArray;

, , , , .

+2

, . .

$CI =& get_instance();
$CI->model_name->method_name();
0

All Articles