Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

PHP Checkboxes Auto Select

$
0
0

Hey guys, 

 

Not sure if anyone is able to help me out I'm trying to split up some data from a table and select check boxes depending on if the tag exists:

 

Database:

 

Table: Tags

Fields: id, tag

 

PHP so far:

 

$sqlCommand = "SELECT tag FROM tags";
$query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
$tagoptions="";
while ($row = mysqli_fetch_array($query2)) { 
    $tag = $row["tag"];
$string = $posttag;
$array = explode(',', $string);
if ($string == $tag) {
$checked = "checked";
}
else {
$checked = "";
}
$tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>';
}

 

 


The data in the tag's field:

ID - TAG

1 tag1

2 tag2

3 tag3

 

The tag table is used to generate the tag check boxes. Once they are generated (Which works).

 

I then have the data been requested from a different table which could be multiple options e.g.:

 

tag1, tag3

 

However if only one tag is selected then the box is checked, if 2 tags or more are selected then neither work.

 

Anyone have any ideas where I am going wrong here?


Viewing all articles
Browse latest Browse all 13200

Trending Articles