![]() |
populating a checkbox from database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: populating a checkbox from database (/showthread.php?tid=14687) Pages:
1
2
|
populating a checkbox from database - El Forum - 01-11-2009 [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() Where the models are: Code: function getProducts() Ending up with the view 'shop_view' Code: <?php foreach ($query3->result() as $row):?> 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. populating a checkbox from database - El Forum - 01-11-2009 [eluser]nikefido[/eluser] what makes the checkbox relevant (Why would it be checked?). Any additional info will help us help you ![]() populating a checkbox from database - El Forum - 01-11-2009 [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. populating a checkbox from database - El Forum - 01-11-2009 [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 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 populating a checkbox from database - El Forum - 01-11-2009 [eluser]sore eyes[/eluser] Hi Nikfido, Thanks for responding in some detail. I will take a look at it. populating a checkbox from database - El Forum - 01-12-2009 [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 populating a checkbox from database - El Forum - 01-12-2009 [eluser]nikefido[/eluser] If obiron's DB query works with your database, I would definitely support that approach. populating a checkbox from database - El Forum - 01-15-2009 [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) 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? populating a checkbox from database - El Forum - 01-15-2009 [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...) populating a checkbox from database - El Forum - 01-16-2009 [eluser]sore eyes[/eluser] Hi Nikefido, thanks for responding. So what is the code that I need to use to get it working? |