[eluser]Unknown[/eluser]
Hey Guys,
New to CI, and PHP really
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/<?php echo $product->imagefiletiny; ?>"></td>
<td><?php echo $product->productid; ?></td>
<td><?php echo $product->prodcode; ?></td>
<td><?php echo $product->prodname; ?></td>
</tr>
<?php foreach ($variations as $variation) : ?>
<?php if($variation->pproductid == $product->productid) { ?>
<tr>
<td>Variation</td>
<td><?php echo $variation->pproductid; ?></td>
<td><?php echo $variation->pvcsku; ?></td>
<td><?php echo $variation->pprodname; ?></td>
<?php } endforeach; ?>
<?php endforeach; ?>