?
Solved

PHP list checkboxes - some checked some unchecked

Posted on 2009-03-30
10
Medium Priority
?
870 Views
Last Modified: 2013-12-12
I am writing a php function to list all categories stored in a mysql database as checkboxes. It is used as part of an update function so i need to display some of the checkboxes as "checked" and some as "unchecked" depending on values in the database. Below is the code i have come up with. It lists all the categories successfully but only ticks one text box even if the item belongs to multiple categories. The arrays hold the correct values I just cant figure out how to check the boxes.

In short i need to display all items in $data as checkboxes, checking all those items that are also present in $data2
function getAllCategoriesCheckedCheckbox($clientID)
{
	$data = mysql_query("SELECT * FROM Category")
    or die(mysql_error()); 
	$data2 = mysql_query("SELECT Category.categoryID FROM ClientCategory, Category WHERE ClientCategory.clientID = $clientID AND ClientCategory.categoryID = Category.categoryID")
    or die(mysql_error()); 
	
	$c = 1;
    
	//categories that should be checked array
    $clientCategories = mysql_fetch_array($data2);
	
	//all categories array
	while($categories = mysql_fetch_array($data))
    {
			if (in_array($categories['categoryID'], $clientCategories, true)) {
    			echo $c.'a<input name="category[]" id="category'.$c.'" value="'.$c.'" type="checkbox" checked="checked" /><span>'.$categories['categoryName']."</span><br />\n";
			}
			else {
				echo $c.'b<input name="category[]" id="category'.$c.'" value="'.$c.'" type="checkbox" /><span>'.$categories['categoryName']."</span><br />\n";
			}
			$c++;	
	}
}

Open in new window

0
Comment
Question by:somerider
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 5

Expert Comment

by:allmer
ID: 24026019
echo $c.'a<input name="category[]" id="category'.$c.'" value="'.$c.'" type="checkbox" checked /><span>'.$categories['categoryName']."</span><br />\n";

Shouldn't category[] contain an index? (e.g.: category[$i])
Otherwise there may be conflicts with checkboxes named alike. Which will then make them all checked/ unchecked depending on the last checkbox being created.
This could be your problem.
I usually use just checked instead of checked="checked" and it works fine in these situations.
HTH
Jens


0
 
LVL 1

Author Comment

by:somerider
ID: 24034866
Thanks for trying but giving each checkbox an index did not help. It is still ticking only one item even if more than one IDs are in the $clientCategory array.

Any help appreciated....im tearing my hair out.
0
 
LVL 5

Expert Comment

by:allmer
ID: 24036314
Which item is checked exactly?
Can you post the part ot the code including the index again?
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 1

Author Comment

by:somerider
ID: 24036373
It is ticking the first item listed in the $clientCategories array. Here is the code again. Sorry about the formatting. It looks far nicer in DW.

Heres an example....

$clientCategories contains:
2
3
4
12

$categories contains:
1 Web Sites
 2 Graphic Design
 3 Banking, Finance & Insurance
 4 Building, Property & Landscaping
 5 Retail & Services
 6 Technology, Engineering, Manufacturing & Automotive
 7 Education & Learning
 8 Medical & Scientific
 9 Sporting & Recreational
 10 Government, Community & Charities
 11 Flash Animation
 12 Logo Design

OUTPUT:
A checkbox is outputted for each of the $categories array items
Only item 2 checkbox is checked. I need item 2, 3 ,4 ,12 to be ticked.

Its almost like if (in_array($categories['categoryID'], $clientCategories, true)) is only returning true on the first pass.

Stumped! Im sure theres a very obvious solution

function getAllCategoriesCheckedCheckbox($clientID)
{
    
	$data = mysql_query("SELECT * FROM Category")or die("<p class=\"red\">An Error occurred.</p><p>".mysql_error()."</p>"); 
	$data2 = mysql_query("SELECT Category.categoryID FROM ClientCategory, Category WHERE ClientCategory.clientID = $clientID AND ClientCategory.categoryID = Category.categoryID")or die("<p class=\"red\">An Error occurred.</p><p>".mysql_error()."</p>"); 
	
	$c = 1;
    
	//categories that should be checked array
    $clientCategories = mysql_fetch_array($data2);
	
	//all categories array
	while($categories = mysql_fetch_array($data))
    {
			if (in_array($categories['categoryID'], $clientCategories, true)) {
    			echo $c.'a<input name="category['.$c.']" value="'.$c.'" type="checkbox" checked="checked" /><span>'.$categories['categoryName']."</span><br />\n";
			}
			else {
				echo $c.'b<input name="category['.$c.']" value="'.$c.'" type="checkbox" /><span>'.$categories['categoryName']."</span><br />\n";
			}
			$c++;	
	}
}

Open in new window

0
 
LVL 5

Expert Comment

by:allmer
ID: 24036521
Is there ever?
$clientCategories may not contain data in the format that you expect.

Could you replace the app. section of your code with the code below to find out about it?
echo"<pre>";
print_r($clientCategories);
print_r($categories);
echo"</pre>";
if (in_array($categories['categoryID'], $clientCategories)) {
    echo $c.'a<input name="category[$c]" value="$c" type="checkbox" checked="checked" /><span>'.$categories['categoryName']."</span><br />\n";
}
else {
    echo $c.'b<input name="category[$c]" value="$c" type="checkbox" /><span>'.$categories['categoryName']."</span><br />\n";
}
$c++;	

Open in new window

0
 
LVL 5

Expert Comment

by:allmer
ID: 24036617
Here is what I meant
function getAllCategoriesCheckedCheckbox($clientID)
{
    
	$data = mysql_query("SELECT * FROM Category")or die("<p class=\"red\">An Error occurred.</p><p>".mysql_error()."</p>"); 
	$data2 = mysql_query("SELECT Category.categoryID FROM ClientCategory, Category WHERE ClientCategory.clientID = $clientID AND ClientCategory.categoryID = Category.categoryID")or die("<p class=\"red\">An Error occurred.</p><p>".mysql_error()."</p>"); 
	
	$c = 1;
    
	//categories that should be checked array
    $clientCategories = mysql_fetch_array($data2);
echo"<pre>";
print_r($clientCategories);	
	//all categories array
	while($categories = mysql_fetch_array($data))
    {
print_r($categories);
echo"</pre>";
if (in_array($categories['categoryID'], $clientCategories)) {
    echo $c.'a<input name="category[$c]" value="$c" type="checkbox" checked="checked" /><span>'.$categories['categoryName']."</span><br />\n";
}
else {
    echo $c.'b<input name="category[$c]" value="$c" type="checkbox" /><span>'.$categories['categoryName']."</span><br />\n";
}
$c++;	
	}
}

Open in new window

0
 
LVL 5

Accepted Solution

by:
allmer earned 500 total points
ID: 24036951
Just came to my attention:
$clientCategories = mysql_fetch_array($data2);
it seems you are only retrieving the first row from the result set.
Try:
$clientCategories = array();
$i = 0;
while($tmp= mysql_fetch_array($data2)) {
    $clientCategories[$i++] = $tmp; // or $tmp[0];
}
instead of
$clientCategories = mysql_fetch_array($data2);
0
 
LVL 5

Expert Comment

by:allmer
ID: 24234771
I believe the question has been answered successfully
0
 
LVL 1

Author Closing Comment

by:somerider
ID: 31564697
Thanks for the help and sorry for the delayed response. you certainly helped but I ended up rewriting the whole section in a different way. Cheers
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question