Jump to content

[SOLVED] Difficult Arrays and Check Boxes


phpretard

Recommended Posts

My objective is to get the comma seperated values from an individual row and display them as checkboxes.

 

$all_cats[] = value1, value2, value3, value4, value5, value6 <- all should be displayed as checkboxes

 

$type[]= value2, value4,  value6 <-- these should be "checked"

 


<?php
connect();

$get_current_categories = mysql_query("select * from bugs2 where id='".$_GET['id']."' ") or die(mysql_error());
while ($cat_check = mysql_fetch_assoc($get_current_categories))
{
$type[]=$cat_check['type'];	
}

$imp=implode(", ", $type);	
$exp=explode(", ", $imp);

$get_all_categories = mysql_query("select * from types order by name ") or die(mysql_error());
while ($all = mysql_fetch_assoc($get_all_categories))
{
$all_cats[] = $all['name'];
}


foreach($all_cats as $category)
{
	if(in_array($exp, $all_cats)){ // PROBLEM IS HERE I BELIEVE
		$selected = "<input type=\"checkbox\" name=\"categories[]\" value=\"$category\" checked />$category";
	}else{
		$selected = "<input type=\"checkbox\" name=\"categories[]\" value=\"$category\" />$category";
	}

echo $selected;
}		


free($get_current_categories);	
free($get_all_categories);	

?>

 

The problem is all the chackboxes show up but none are checked.

I think I have pinpointed where the problem is but am unable to fix it.

 

Thank you

Link to comment
Share on other sites

You are making this way more difficult than it needs to be. Simply do ONE query to get all the optins and add a parameter using JOIN to determine which ones should be checked. Give me a moment and I'll post some code based upon what you currently have.

 

However, based on what you have the 'type' field in the 'bugs2' table would need to match up with the 'name' field in the 'types' table? Is that correct?

Link to comment
Share on other sites

Hi

 

I agree with the above. Just been playing:-

 

<?php
connect();

$get_all_categories = mysql_query("select a.name, b.type from types a LEFT OUTER JOIN bugs2 b ON a.name = b.type AND id='".$_GET['id']."' order by a.name ") or die(mysql_error());
while ($all = mysql_fetch_assoc($get_all_categories))
{
echo "<input type=\"checkbox\" name=\"categories[]\" value='".$all['name']."' ".((is_null($all['type'])) ? '' : 'checked' )." />".$all['name'];
}

?>

 

However you should check that $_GET['id'] is legit before using it in SQL.

 

All the best

 

Keith

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Means that more than one of the tables has a column called id. So it doesn't know to check types.id or bugs2.id

 

Qualify the id field for this:-

 

<?php
connect();

$get_all_categories = mysql_query("select a.name, b.type from types a LEFT OUTER JOIN bugs2 b ON a.name = b.type AND b.id='".$_GET['id']."' order by a.name ") or die(mysql_error());
while ($all = mysql_fetch_assoc($get_all_categories))
{
echo "<input type=\"checkbox\" name=\"categories[]\" value='".$all['name']."' ".((is_null($all['type'])) ? '' : 'checked' )." />".$all['name'];
}

?>

 

All the best

 

Keith

Link to comment
Share on other sites

This should be all that you need. May need to check that field names are correct for your environment. It basically does a select all names on the 'types' table and then appends a new field to each record ('checked' with a value of 1 or 0) based upon whether there is a match in the 'bugs2' table with the type value matching the name.

 

$query = "SELECT name,
            IF (name IN (SELECT type FROM bugs2 WHERE id='{$_GET['id']}'),1,0) as checked
          FROM types
          ORDER BY name"
$result = mysql_query($query) or die(mysql_error());

$checkboxes = '';
while ($checkData = mysql_fetch_assoc($result))
{
    $checked = ($checkData['checked']==1) ? ' checked="checked"' : '';
   $checkboxes .= "<input type="checkbox" name="categories[]" value="{$checkData['name']}"{$checked} />$checkData['name']<br /> ";
}

echo $checkboxes;

 

EDIT: kickstart's method should work too. Just different ways to accomplish the same thing. I suspect his may be more efficient, but don't know for sure.

Link to comment
Share on other sites

Is this code to replace part or all of the existing?

 

I ask because it still gives me all blank checkboxes...although it does display all the checkboxes

 

The (comma seperated) value of b.type='value2, value4, value6'

 

Should I not comma seperate?

 

That is some great code by the way!

 

It all makes sense to me except for:

 


--- ".((is_null($all['type'])) ? '' : 'checked' )." ---

 

I don't quite understand why the question mark and the colon are there.

Link to comment
Share on other sites

Hi

 

The method is very similar to mine, just using a subselect while I have used a join.

 

It was to replace you whole fragment of code.

 

The bit  ".((is_null($all['type'])) ? '' : 'checked' )." is doing an IF within a statement. It checks if the returned type feield is null with is_null($all['type']) , if that is true it returns the bit after the ? (ie, nothing in this case), if false it returns the bit after the : ('checked').

 

Where is he comma seperated list of b.type coming from?

 

All the best

 

Keith

Link to comment
Share on other sites

Comma seperation comes from:

 


if (isset($_POST['submit'])){

$categories=implode(", ", $_POST['categories']);

connect();
$insert=mysql_unbuffered_query("update bugs2 set type = '$categories' where id='".intval($_POST['id'])."' ") or die(mysql_error());

}

 

 

So when I update it changes the values in the DB  (with the simplified code Hooray!)

 

it's the display that's killing me...it still won't show "checked" boxes

 

Link to comment
Share on other sites

Hi

 

Arrggh. That wasn't in the original post.

 

What is it that you want from that table? What are categories? Why are they stored in one field?

 

My guess is that you have bugs which have various catagories. If so what you should really have is a table of bugs and another table list all the categories for each bug.

 

All the best

 

Keith

Link to comment
Share on other sites

I apologize for that...

 

I am trying to fix someone elses mess and it seemed the easiest way to resolve was to comma seperate in the (field "type") in the table "bugs"), then reference the table I added called (categories) that has six entries (which are the categories).

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hi

 

If you can move the types off to a seperate table then that would be far easier, as you can then do most of the work in one SQL call.

 

While there are ways to extract the comma seperated field, they are not nice to read, would be a nightmare to maintain and I would perform badly. As well as giving you major problems if someone introduces a type with a comma in it.

 

To continue with what you already have:-

 

<?php
connect();

$TypeArray = array();
$get_current_categories = mysql_query("select type from bugs2 where id='".$_GET['id']."' ") or die(mysql_error());
while ($cat_check = mysql_fetch_assoc($get_current_categories))
{
$types = explode(',',$cat_check['type']);
foreach($types as $type)
{
	$type = trim($type);
	$TypeArray[$type] = $type;
}
}


$get_all_categories = mysql_query("select name from types order by name ") or die(mysql_error());
while ($all = mysql_fetch_assoc($get_all_categories))
{
echo "<input type='checkbox' name='categories[]' value='".$all['name']."' ".((isset($TypeArray[$all['name']])) ? "checked='checked'" : '' )." />".$all['name'];
}

free($get_current_categories);	
free($get_all_categories);	

?>

 

All the best

 

Keith

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.