CodeIgniter Forums

Full Version: Show vairations for products in a table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hey Guys,
New to CI, and PHP really Smile

I'm trying to grasp the MVC methods. I can do this with Spahgetti code, but trying to work out the best methods for creating code i can reuse, and to keep the view free from lots of php.

So this is what i have so far.
I know the view is way off, but have tried so many different ways, but just can't get it.
Any help would be so good!
Thanks in advance.

public function getProducts() {
  // get products

  $query = $this->db->query("SELECT *
     FROM isc_products AS p
     LEFT JOIN isc_product_images AS pi
     ON pi.imageprodid = p.productid
     LIMIT 20 ");

  return $query->result();

public function getVariations($productid) {    
    // get variations for each product
    $query = $this->db->query("
       SELECT p.prodname AS pprodname, c.vcsku AS pvcsku,
       GROUP_CONCAT(o.vovalue ORDER BY o.voname SEPARATOR ' | '),
       c.vcprice, c.vcpricediff, p.prodprice, c.vcstock, p.productid AS pproductid
       FROM isc_product_variation_combinations AS c
       LEFT JOIN isc_product_variation_options AS o
       ON FIND_IN_SET(o.voptionid, c.vcoptionids)
       LEFT JOIN isc_products AS p
       ON p.productid = c.vcproductid
       WHERE c.vcproductid = $productid
       GROUP BY
       ORDER BY o.vooptionsort, o.vovaluesort
    return $query->result();


public function products() {
  $data['products'] = $this->get_db->getProducts();
  $productid = $this->get_db->getProducts('productid');

  $data['variations'] = $this->get_db->getVariations($productid);

  $data['title'] = "Products";

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

And the view (i know this is wrong, but just can't work out how to pass the parent productid to get the variations of the product)

<!-- table data -->
   <?php foreach ($products as $product) : ?>
     <td><img src="siteurl/&lt;?php echo $product-&gt;imagefiletiny; ?&gt;"></td>
     <td>&lt;?php echo $product->productid; ?&gt;</td>
     <td>&lt;?php echo $product->prodcode; ?&gt;</td>
     <td>&lt;?php echo $product->prodname; ?&gt;</td>
    &lt;?php foreach ($variations as $variation) : ?&gt;
     &lt;?php if($variation->pproductid == $product->productid) { ?&gt;
       <td>&lt;?php echo $variation->pproductid; ?&gt;</td>
       <td>&lt;?php echo $variation->pvcsku; ?&gt;</td>
       <td>&lt;?php echo $variation->pprodname; ?&gt;</td>

    &lt;?php } endforeach; ?&gt;
   &lt;?php endforeach; ?&gt;