Welcome Guest, Not a member yet? Register   Sign In
Show vairations for products in a table
#1

[eluser]Unknown[/eluser]
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.
Jason

Model
Code:
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
       c.combinationid
       ORDER BY o.vooptionsort, o.vovaluesort
       ");
    return $query->result();

}

Controller
Code:
public function products() {
  
  $this->load->model("get_db");
  
  $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);
  
  // $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)

Code:
<!-- table data -->
   <?php foreach ($products as $product) : ?>
    
    <tr>
     <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>
    </tr>
    
    &lt;?php foreach ($variations as $variation) : ?&gt;
     &lt;?php if($variation->pproductid == $product->productid) { ?&gt;
      <tr>
       <td>Variation</td>
       <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;




Theme © iAndrew 2016 - Forum software by © MyBB