balkan7 Posted December 24, 2012 Share Posted December 24, 2012 Hello guys I'm tried to save data from 2 checkbox into one column in database and retrieve. In this case i have this: <? $test = array(1 => "A", 2 => "B"); foreach ($test as $k => $v) { echo "<input type=\"checkbox\" name=\"c\" value=\"$k\"> $v <br>"; } ?> If those 2 checkbox selected i want save in base like array 1,2 After this check from database if column data is: 1 = show pic 1 2 = show pic 2 1,2 (array) = show pic 3 Quote Link to comment https://forums.phpfreaks.com/topic/272324-two-checkbox-save-like-array-into-one-column-in-database/ Share on other sites More sharing options...
Barand Posted December 24, 2012 Share Posted December 24, 2012 Change to <?php $test = array(1 => "A", 2 => "B"); foreach ($test as $k => $v) { echo "<input type=\"checkbox\" name=\"c[]\" value=\"$k\"> $v <br>"; // change name to post as array } ?> Then store the total (1,2 or 3) in the DB $total = array_sum($_POST['c']) Quote Link to comment https://forums.phpfreaks.com/topic/272324-two-checkbox-save-like-array-into-one-column-in-database/#findComment-1401070 Share on other sites More sharing options...
balkan7 Posted December 24, 2012 Author Share Posted December 24, 2012 Hi Barand Thanks for reply and for smart solution ! Quote Link to comment https://forums.phpfreaks.com/topic/272324-two-checkbox-save-like-array-into-one-column-in-database/#findComment-1401073 Share on other sites More sharing options...
balkan7 Posted December 27, 2012 Author Share Posted December 27, 2012 Hello Barand How can add for each checkbox (checked='checked') $id = $_GET['id']; $sql = mysql_query("SELECT * FROM table WHERE id=$id"); $data = mysql_fetch_array($sql); $checkbox = data['c']; $test = array(1 => "A", 2 => "B"); foreach ($test as $k => $v) { if ($checkbox == $k) { $checked = " checked='checked'"; echo "<input type=\"checkbox\" name=\"c[]\" value=\"$k\"$checked> $v <br>"; } How can i retrieve if column: c == 1 only checked A c == 2 only checked B c == 3 checked A and B ? Quote Link to comment https://forums.phpfreaks.com/topic/272324-two-checkbox-save-like-array-into-one-column-in-database/#findComment-1401565 Share on other sites More sharing options...
Barand Posted December 27, 2012 Share Posted December 27, 2012 Like this $test = array(1 => "A", 2 => "B"); foreach ($test as $k => $v) { $checked = $checkbox & $k ? " checked='checked'" : ""; echo "<input type=\"checkbox\" name=\"c[]\" value=\"$k\" $checked /> $v <br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/272324-two-checkbox-save-like-array-into-one-column-in-database/#findComment-1401594 Share on other sites More sharing options...
balkan7 Posted December 27, 2012 Author Share Posted December 27, 2012 Thanks working!! Quote Link to comment https://forums.phpfreaks.com/topic/272324-two-checkbox-save-like-array-into-one-column-in-database/#findComment-1401608 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.