Jump to content

php & checkboxes


Miko

Recommended Posts

Hello,

 

I'm trying to do the following:

 

I have a DB with a table with sub categories (let's say there a 10 sub categories in it) and another one that I keep the link between a product and the sub category (tbl name = LINKED).

My goal is to allow the user to have possibility to edit the product.

 

So when the user clicks on the 'edit' button next to the products name it will show a page (another page)where he has some input fields and a text area, these input fields and textarea are already completed with the data from the products that he had selected.

 

Now the product can have several sub categories like lets say it's a movie, the movie has as sub categories "romance, comedy, politic " , so I have in my table where a keep the link between product and sub category 3 rows with the title_id and then the sub category.

 

In the page with the input fields I should print out checkboxes with the sub category name, no problem here.

But how can I print out that same list but with the checkboxes romance,comedy and politic already checked? So in other words, I have 10 sub categories and the products is linked with 3 of them, I want to print out all 10 and check those 3 sub categories.

 

I was thinking using a loop where I first get the entire list of sub categories, then query the tbl "LINKED" but then I'm stuck :-\

Link to comment
Share on other sites

Here's how I would set up the database for this one (it seems complicated, but it sounds like the best way).

 

You have a table that contains:

- all products

- all categories

- all sub-categories

 

Tbl Products: Product Name, Description, etc... and unique product id

Tbl Cats: This one would be populated using another unique id and each row will link a category to a table (IE: the columns would be like "Movie","Book",etc.. which only allows boolean values, as well as a fk_pid column = the product id from the Products Tbl)

Tbl Sub-Cats: This one is populated (using yet another unique id) with a large number of columns (all the sub-categories for all your categories such as "Romance" or "Horror" which only allows boolean values) with a fk_catid (to link the Cats Tbl to the Sub-Cats Tbl).

 

A query should be pretty easy to set up such that you retrieve the product id, link it to the category id which links to the sub-categories and allows you to retrieve the boolean values of which subcategories have been checked off or not.

 

**Again, off the top of my head, I imagine the hardest part would really be getting this query (god, I hate queries like this).

Link to comment
Share on other sites

yeah it's a tricky request that my client demands  :o

 

I was just thinking of using some of my objects from another project, there I had something similar I think.

It was also a request which took a more on more relation between tables .. (there should be a good plan for this kind of things), Out of my head like that it was something like tbl_user, tbl_group, tbl_perm (DB was existed already with a few hundred members inside) and my client asked if I could create a page that allows him to edit users, and when he had select a user the exisiting groups and perms etc would be shown on the page (it wasn't checkboxes I think) and he could edit on the fly.

 

Stupid thing is, I don't remember where I've put the files  :cry:

Link to comment
Share on other sites

I truly believe that you have the exact same situation there honestly.

 

Group C is a member of Group B which is a member of Group A.

 

That is:

 

Group A (one) contains multiple members of Group B which contains multiple members of Group C.

 

I did try something like your whole user-group-perms thing using just text files, but it was utter failure. Good Luck!

Link to comment
Share on other sites

I've managed to get this done! :)

 

here is a sample of what I've done :)

 

<?php

	$sql = "SELECT * FROM sub_cat WHERE main_cat = '1' ORDER BY id";
	$query = mysql_query($sql);

	while($row = mysql_fetch_array($query)){

		$subcatid = $row['id'];
		$subcatname = $row['name_genre'];

		$sql2 = "SELECT * FROM linked WHERE title_id = '1' ";
		$query2 = mysql_query($sql2);

		while($row2 = mysql_fetch_array($query2)){

			$linkedsubcatid = $row2['sub_cat_id'];
			$explode = explode(",",$linkedsubcatid);

			if(in_array($subcatid,$explode)){

				echo "<input type=\"checkbox\" checked=\"checked\" />".$subcatname."<br />";

			}else{

				echo "<input type=\"checkbox\" />".$subcatname." <br />";

			}

		}

	}

?>

 

and it works like a charm :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.