Sam
Sam

Reputation: 29

PHP: How to get multiple checkbox values from database for same ID?

I am new to php & need little help here. I've following code to INSERT multiple checkbox values into a table for same ID:

on my form I have:

<input type="checkbox" name="subcat[]" value="Mech">Mech
<input type="checkbox" name="subcat[]" value="Ele">Ele
<input type="checkbox" name="subcat[]" value="Civ">Civ
<input type="checkbox" name="subcat[]" value="Air">Air
<input type="checkbox" name="subcat[]" value="BSL">BSL

I've checked "Mech", "Ele" and "BSL"

and PHP to insert:

$subcat = $_POST['subcat'];
    for($i=0; $i<sizeof($subcat);$i++){
    $sql1="INSERT INTO mst_scatadd (party_code,scid)
    VALUES ('$pcode',$subcat[$i])";
    mysql_query($sql1,$con);
    }

db looks something like...

sctid  | party_code  |   scid
---------------------------------
  1    |   01S001    |    Mech
  2    |   01S001    |    Ele
  3    |   01S001    |    BSL
  4    |   01K207    |    Main

Now, how can I retrieve these values from db to my form for the same ID and make them as checked if I want to edit the checkboxes?

Little help would be appreciated!!

Thanks in adv.

EDIT 1:

Hello again guys!

I've done with inserting and retrieving checkbox values using implode and explode and its working fine. Thanks to @Antoniossss for good suggession to keep checkboxes values as numbers(make sure the column in db should be VARCHAR) Thanks to @Barmar to explain use of explode & thanks to @MarkTWebsite to share idea of if-else

Code for inserting multiple checkbox values:

foreach ($_POST['subcat'] as $_subcat)
    {
        $checksub[] = $_subcat;
    }   $finalsub = implode(',', $checksub);

$sql="INSERT INTO vendor_mst(party_code, subcat) VALUES ('$pcode','$finalsub')";

My final code to retrieve checkbox values from db...

sql="SELECT * FROM vendor_mst WHERE party_code like '".$searchname."'";
$result=mysql_query($sql,$con);
while ($row=mysql_fetch_array($result, MYSQL_BOTH)){
$checksub  = explode(',',$row['subcat']);
}
if(in_array("1",$checksub))echo '<input type="checkbox" name="subcat[]" value="1" checked >Mech'; else echo '<input type="checkbox" name="subcat[]" value="1">Mech';
if(in_array("2",$checksub))echo '<input type="checkbox" name="subcat[]" value="2" checked >Ele';  else echo '<input type="checkbox" name="subcat[]" value="2">Ele';
if(in_array("3",$checksub))echo '<input type="checkbox" name="subcat[]" value="3" checked >Civ';  else echo '<input type="checkbox" name="subcat[]" value="3">Civ';
if(in_array("4",$checksub))echo '<input type="checkbox" name="subcat[]" value="4" checked >Air';  else echo '<input type="checkbox" name="subcat[]" value="4">Air';
if(in_array("5",$checksub))echo '<input type="checkbox" name="subcat[]" value="5" checked >BSL';  else echo '<input type="checkbox" name="subcat[]" value="5">BSL';

Thanks to stackoverflow!

Upvotes: 1

Views: 31774

Answers (4)

user777388
user777388

Reputation: 311

<?php
 $checksub = array('1','2','3');

?>

<form action="<?php echo site_url('index/do_upload') ?>" method="post" enctype="multipart/form-data">
    <label>Select file : <input type="file" name="userfile"></label>
    <input type="submit" name="action">
    <input type="checkbox" name="subcat[]" value="1" <?php if (in_array(1,$checksub)){ echo "checked"; }?>/> 

</form>

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32507

I would rather go in direction of threating checkboxes as binary flags and store their binary OR operation into DB as integer. It is really easy to implement, and will simplify record relations in your database. To do that you need to change values of checkboxes into 2^n values. For example

<input type="checkbox" name="subcat[]" value="1">Mech
<input type="checkbox" name="subcat[]" value="2">Ele
<input type="checkbox" name="subcat[]" value="4">Civ
<input type="checkbox" name="subcat[]" value="8">Air
<input type="checkbox" name="subcat[]" value="16">BSL

selecting Mech and Civ should be storen in DB as 5 (binary 00101), Air+BSL would be 24 (11000b) and so on. To revert this process you would just simply test every bit of stored value and check/uncheck corresponding boxes in loop. I hope that you will get my point.

EDIT: example code as requested. Be aware that I AM NOT a PHP programmer so there could (and definetly will be) syntax errors but I will do my best To get user input you have to simply add values of checked boxes

$selected=0;
foreach($POST['subcat'] as $checkedBox){
    $selected=$selected+$checkedBox
    }

So now variable $selected will have encoded status of all checkboxes To check which boxes (I never have troubles with that word) should be check or not you have to test every bit of stored value in DB. I dont know how are you generating your view, so i will provide only a template, you have to figure it out how to apply it to your project

$valueFromDb= ///  some value fetched from DB
$maxValue= //// the highest value of your checkboxes, in my eg. 16
for($testValue=1;$testValue<=$limit;$testValue=$testValue << 1){
    $result=($valueFromDb & $testValue) > 0;
    // do something with checked result for current checkbox
}

variable result above will have checked status (boolean value) of coresponding checkboxes. Each loop iteration coresponds to different checkbox in order from Mech to BSL one by one. Alternatively you can print checkboxes directly and set them as checked with if inscruction $valueFromDb= // fetched value

<input type="checkbox" name="subcat[]" value="1" <?php if($valueFromDb & 1 >0) echo('checked') ?php>Mech
<input type="checkbox" name="subcat[]" value="2" <?php if($valueFromDb & 2 >0) echo('checked') ?php>>Ele
<input type="checkbox" name="subcat[]" value="4" <?php if($valueFromDb & 4 >0) echo('checked') ?php>>Civ
 .... and so on and on and on

I have just noticed that combining those 2 methods would result in generating checboxes dynamicly in loop so the code will be much more readable. All you would need is to provide map of checbox value labels:) I hope that eg. above will definetlly clearify how to apply such solution.

Upvotes: 1

Barmar
Barmar

Reputation: 780852

You can retrieve the subcats with:

SELECT party_code, GROUP_CONCAT(scid) scids
FROM mst_scadd
GROUP BY party_code

Then when you're fetching the rows from the database, you can do:

$scids = array_flip(explode(',', $row['scids']));

That will create an associative array whose keys are the subcategories. Then your code for displaying the checkboxes can be something like:

foreach ($all_subcats as $sc) {
    echo '<input type="checkbox" name="subcat[]" value="' . $sc . '" ' .
        (isset($scids[$sc]) ? 'checked' : '') . '>' . $sc;
}

Upvotes: 0

MarkTWebsite
MarkTWebsite

Reputation: 21

You will need to use the PHP if statement; I don't really understand your db structure, else I would provide code however, if you use PHP if, for each checkbox, check in the database to see if it's selected then; if it is selected

echo '<input type="checkbox" name="subcat[]" value="Mech" selected>Mech';

else;

echo '<input type="checkbox" name="subcat[]" value="Mech">Mech';

Do you understand what I mean?

Source; HTML/CSS/PHP Developer for 3 years.

Upvotes: 0

Related Questions