NewcastleFan Posted April 4, 2013 Share Posted April 4, 2013 (edited) 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? Edited April 4, 2013 by NewcastleFan Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/ Share on other sites More sharing options...
Technocrat Posted April 4, 2013 Share Posted April 4, 2013 It's probably $postag that the issue, $string is always going to be set to the same thing. What does $postag = when it doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1422885 Share on other sites More sharing options...
NewcastleFan Posted April 4, 2013 Author Share Posted April 4, 2013 Post tag comes through as: tag1, tag2 tag1, tag3, tag4 I think it may be an issue on how do I separate the values to then check? Not 100%. Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1422887 Share on other sites More sharing options...
Technocrat Posted April 4, 2013 Share Posted April 4, 2013 Is it an array? You could use in_array or a loop Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1422889 Share on other sites More sharing options...
NewcastleFan Posted April 4, 2013 Author Share Posted April 4, 2013 The posttag is opened up in the array here? $string = $posttag; $array = explode(',', $string); if ($string == $array) { $checked = "checked"; } else { $checked = ""; } I've used explode to separate them but then I'm not sure how to get from that point to checking to check the box? Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1422890 Share on other sites More sharing options...
Technocrat Posted April 4, 2013 Share Posted April 4, 2013 $sqlCommand = "SELECT tag FROM tags"; $query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); $tagoptions=""; while ($row = mysqli_fetch_array($query2)) { $tag = $row["tag"]; if (in_array($tag, $postag)) { $checked = "checked"; } else { $checked = ""; } $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1422891 Share on other sites More sharing options...
NewcastleFan Posted April 4, 2013 Author Share Posted April 4, 2013 $sqlCommand = "SELECT tag FROM tags"; $query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); $tagoptions=""; while ($row = mysqli_fetch_array($query2)) { $tag = $row["tag"]; if (in_array($tag, $postag)) { $checked = "checked"; } else { $checked = ""; } $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>'; } That doesn't seem to do anything, no boxes are checked no matter the value of $posttag. Not sure if I've explained it correctly I'm not the greatest at explaining sorry. $posttag would come through with a value of "tag1, tag2" $tag would come through in an array with my while loop "tag1" "tag2" "tag3" Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1422895 Share on other sites More sharing options...
Technocrat Posted April 4, 2013 Share Posted April 4, 2013 $sqlCommand = "SELECT tag FROM tags"; $query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); $tagoptions=""; $postag = explode(',', str_replace(' ', '', $postag)); while ($row = mysqli_fetch_array($query2)) { $tag = $row["tag"]; if (in_array($tag, $postag)) { $checked = "checked"; } else { $checked = ""; } $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1422907 Share on other sites More sharing options...
NewcastleFan Posted April 5, 2013 Author Share Posted April 5, 2013 $sqlCommand = "SELECT tag FROM tags"; $query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); $tagoptions=""; $postag = explode(',', str_replace(' ', '', $postag)); while ($row = mysqli_fetch_array($query2)) { $tag = $row["tag"]; if (in_array($tag, $postag)) { $checked = "checked"; } else { $checked = ""; } $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>'; } Thanks for the reply, this still unfortunately does nothing I've tried to take it back to basics and remove the database calls just to figure out how to get this to work: <?php $posttag = "tag1, tag2"; $posttag = explode(',', str_replace(' ', '', $posttag)); $tag = "tag1, tag2, tag3"; if (in_array($tag, $posttag)) { $checked = "checked"; } else { $checked = ""; } } echo $posttag; ?> <br> <input type='checkbox' name='checkboxname[1]' value='tag1' <?php echo $checked ?>>Tag1<br> <input type='checkbox' name='checkboxname[2]' value='tag2' <?php echo $checked ?>>Tag2<br> <input type='checkbox' name='checkboxname[3]' value='tag3' <?php echo $checked ?>>Tag3<br> However this now isn't even working ha, just returns a blank screen Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1423028 Share on other sites More sharing options...
Barand Posted April 5, 2013 Share Posted April 5, 2013 $sqlCommand = "SELECT tag FROM tags"; $query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); $tagoptions=""; $postag = explode(',', str_replace(' ', '', $postag)); while ($row = mysqli_fetch_array($query2)) { $tag = $row["tag"]; if (in_array($tag, $postag)) { $checked = "checked"; } else { $checked = ""; } $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>'; } What was in $postag when you ran the above code? Did you echo $tagoptions at the end? Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1423067 Share on other sites More sharing options...
NewcastleFan Posted April 5, 2013 Author Share Posted April 5, 2013 $tagoptions was echoed yes, it output the correct list of check boxes but none were selected. $posttag was pulled from the database column posttag which is a text field, and had the value of "tag1, tag2" so the check boxes for tag1 and tag2 should be selected. Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1423074 Share on other sites More sharing options...
Barand Posted April 5, 2013 Share Posted April 5, 2013 Is it a problem with spelling? $posttag vs $postag Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1423108 Share on other sites More sharing options...
NewcastleFan Posted April 8, 2013 Author Share Posted April 8, 2013 Yes, unfortunately all of them are $posttag in my script. No idea where this is going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1423490 Share on other sites More sharing options...
Solution NewcastleFan Posted April 8, 2013 Author Solution Share Posted April 8, 2013 Not sure how its working now but this is what worked no idea why if anyone else is stuck: $sqlCommand = "SELECT tag FROM tags"; $query2 = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); $tagoptions=""; $posttag = explode(',', str_replace(' ', '', $posttag)); while ($row = mysqli_fetch_array($query2)) { $tag = $row["tag"]; if (in_array($tag, $posttag)) { $checked = "checked"; } else { $checked = ""; } $tagoptions.="<input type='checkbox' name='checkboxname[]' value='$tag' $checked>".$tag.'<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/276529-php-checkboxes-auto-select/#findComment-1423496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.