Welcome Guest, Not a member yet? Register   Sign In
populating a checkbox from database
#1

[eluser]sore eyes[/eluser]
Hi, I am newbie to codeigniter, so please bear with me. I have a database of shops and products they sell. I have set up codeignter so that I can cycle through each shop, one at a time and list the products that each shop can sell. The list of products is in checkbox form and needs to show a value of y/n depending on whether or not the shop sells the product. And that is the tricky part and I am stuck. Your help would be much appreciated.

The controller index is:
Code:
function enterShop()
    {
        $data['main'] = 'shop_view';
        $data['title'] = "Shop";
        $shop_id = $this->uri->segment(3);
        $data['query'] = $this->MShops->getOneShop($shop_id);
        $data['query2'] = $this->MProducts->getProducts();
        $data['query3'] = $this->MProductsbyshop->getProductsbyshop($shop_id);
        $this->load->vars($data);
        $this->load->view('template');  
    }

Where the models are:

Code:
function getProducts()
    {
    
    $this->db->select('product_id, product');
    $this->db->where('type_of_shop_id', 2);
    $this->db->order_by('product');
    $data = $this->db->get('products');
    return $data;
    }

function getProductsbyshop($shop_id)
     {
    $this->db->select('product_id');
    $this->db->where('shop_id',$shop_id);
    $data = $this->db->get('productsbyshop');
    return $data;


    }

Ending up with the view 'shop_view'
Code:
<?php  foreach ($query3->result() as $row):?>
&lt;?php $row->product_id; ?&gt;<br/>
&lt;?php $new_product_id=$row->product_id; ?&gt;
&lt;?php echo $new_product_id; ?&gt;
&lt;?php endforeach ?&gt;

<div id="floatright">

&lt;?php  foreach ($query2->result() as $row):?&gt;
<p>&lt;?php echo form_checkbox('product', true, set_checkbox());?&gt;
&lt;?php //$row->product_id; ?&gt;
&lt;?php echo $row->product; ?&gt;</p>
&lt;/form&gt;
&lt;?php endforeach ?&gt;


The view ends up showing the details for each shop, a list of all products, and a list of products that each shop sells (for show purposes). I need to combine the latter two, showing instead the check boxes with relevant ones ticked.

I hope I've explained this okay. Again your help would be much appreciated.
#2

[eluser]nikefido[/eluser]
what makes the checkbox relevant (Why would it be checked?). Any additional info will help us help you Tongue (I'm not sure I understand you're question)
#3

[eluser]sore eyes[/eluser]
Thanks for responding. The checkbox is ticked for each product, if sold by that shop. If not sold then not ticked.Regards.
#4

[eluser]nikefido[/eluser]
Hmmm the answer to this seems to be specific to your business logic.

I would create a function within the model you use to call the database query (assuming you handle database calls from a model class) that checks if a product would require a check.

Code:
//possible implementation
&lt;?php  foreach ($query2->result() as $row):?&gt;
&lt;?php if($check = $this->yourmodel->shouldCheckBox($row->product_id)) : ?&gt;
    <p>&lt;?php echo form_checkbox('product', true, set_checkbox());?&gt;
    &lt;?php //$row->product_id; ?&gt;
    &lt;?php echo $row->product; ?&gt;</p>
&lt;?php endif; ?&gt;
&lt;/form&gt;
&lt;?php endforeach ?&gt;

Upon seeing your code, you might be already doing this with your "set_checkbox()" function. However, I'm not sure that your syntax is correct for the form_checkbox();
According to docs, it goes like this:
Code:
//from the docs
$js = 'onClick="some_function()"';

echo form_checkbox('newsletter', 'accept', TRUE, $js)
where you're implementation skips the argument where "accept" goes. You should pass it a default parameter.
#5

[eluser]sore eyes[/eluser]
Hi Nikfido, Thanks for responding in some detail. I will take a look at it.
#6

[eluser]obiron2[/eluser]
If I have this right, you want to list all of the products with a checkbox ,but only tick the box if the shop sells that product.

rewite your db query.

select p.productID,count(s.productid) from products p left join shopproducts s on p.productid = s.productid
where s.shopid = $shopid
order by productid


What you will get is a list of all products (left join) and a 1 if the shop sells that or 0 if it does not.

You can then use the 1 and 0 to decide whether to activate the checkbox.

Obiron
#7

[eluser]nikefido[/eluser]
If obiron's DB query works with your database, I would definitely support that approach.
#8

[eluser]sore eyes[/eluser]
Many thanks for this Obiron, it was just what I wanted. I'm still struggling a bit, as this is my first join. I have made some modifications. The query now reads

Code:
$query = $this->db->query("SELECT products.product_id,count(productsbyshop.product_id)
AS counting FROM products LEFT JOIN productsbyshop ON
products.product_id = productsbyshop.product_id
WHERE productsbyshop.shop_id = '$shop_id'
GROUP BY products.product_id ORDER by products.product_id");

The problem is that it does not list all the products, only those that are selected. I thought that having the table 'products' on the left of the 'left join' would make it show all of the products, but it does not. I did not understand what the single 'p' and 's' were in the query, so I removed them and this may be the source of the error.

Can you help please?
#9

[eluser]nikefido[/eluser]
The P and S were aliases to the tables "products" and "shopproducts" so if you just removed them and refered to every field with its tablename (tablename.fieldname), then that should not be the cause of the "error"

You are probably not getting all of your products because this query will (I believe) only give results where you're IDs match up

Hope that helps (and is correct...)
#10

[eluser]sore eyes[/eluser]
Hi Nikefido, thanks for responding. So what is the code that I need to use to get it working?




Theme © iAndrew 2016 - Forum software by © MyBB