split63 Posted June 3, 2010 Share Posted June 3, 2010 I have an html form on which a user selects numbers from 0x00 to 0xFF. Each of the 256 numbers has a radio button or check box. They can select as many numbers that apply to them. The results will be stored in a mysql database. Ideally, only a true or false is needed for each number. I could store each number selected, or I could store a true/false for all 256. Also, it seems like an array might be best for handling this data, but as I understand Mysql does not directly store arrays. Later, I will pull this data from the database and dependent on which numbers were selected, I will print a unique message for each number. Any suggestions with regard to the overal approach to get me pointed in the right direction would be appreciated. Its been 12 years since I used server side scripting so its like starting over. Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/ Share on other sites More sharing options...
ignace Posted June 3, 2010 Share Posted June 3, 2010 I thought it was fun to do: function createHexColorList($name, $callback) { if (!function_exists($callback)) { trigger_error("Function '$callback' does not exist.", E_USER_ERROR); return; } $return = ''; for ($i = 0; $i < 256; ++$i) { $return = $return . $callback($name, dechex($i)); } return $return; } function htmlInput($name, $value) { $value = strtoupper($value); return '<li><label><input type="radio" name="' . $name . '" value="' . $value . '"> 0x' . $value . '</label></li>'; } echo '<form action="#" method="POST">', '<ul id="red">', createHexColorList('red', 'htmlInput'), '</ul>', '<ul id="green">', createHexColorList('green', 'htmlInput'), '</ul>', '<ul id="blue">', createHexColorList('blue', 'htmlInput'), '</ul>', '</form>'; if (!empty($_POST)) { $red = $_POST['red']; $green = $_POST['green']; $blue = $_POST['blue']; echo 'You selected: <span style="background-color: #' . $red . $green . $blue . '">' . $red . $green . $blue . '</span>'; } To allow for multiple colors to be selected: function createHexColorList($name, $callback) { if (!function_exists($callback)) { trigger_error("Function '$callback' does not exist.", E_USER_ERROR); return; } $return = ''; for ($i = 0; $i < 256; ++$i) { $return = $return . $callback($name, dechex($i)); } return $return; } function htmlInputCheckbox($name, $value) { $value = strtoupper($value); return '<li><label><input type="checkbox" name="' . $name . '[]" value="' . $value . '"> 0x' . $value . '</label></li>'; } echo '<form action="#" method="POST">', '<ul id="red">', createHexColorList('red', 'htmlInputCheckbox'), '</ul>', '<ul id="green">', createHexColorList('green', 'htmlInputCheckbox'), '</ul>', '<ul id="blue">', createHexColorList('blue', 'htmlInputCheckbox'), '</ul>', '</form>'; if (!empty($_POST)) { $red = $_POST['red']; $green = $_POST['green']; $blue = $_POST['blue']; foreach ($red as $key => $colorCode) { print 'You selected: <span style="background-color: #' . $red[$key] . $green[$key] . $blue[$key] . '">' . $red[$key] . $green[$key] . $blue[$key] . '</span><br>'; } } Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/#findComment-1067305 Share on other sites More sharing options...
split63 Posted June 3, 2010 Author Share Posted June 3, 2010 ignace, I ran your code and it provided one long column repeated three times with radio buttons. A user can select one or more numbers so I don't think a radio approach works unless each is its separate entity. Currently only one button can be checked in your code. My plan was to display 16 rows of 16 check boxes. I think I can swing that. What I was having trouble with was dealing with the results once the user submitted the form. I need to determine how to construct the form so that it provides the data in a format best suited for storing to Mysql. Once I know the best format, I can create the table in MySQL using phpmyadmin. So my fundamental issue is whats the best way to store this info in MySQL? Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/#findComment-1067370 Share on other sites More sharing options...
split63 Posted June 3, 2010 Author Share Posted June 3, 2010 It seems that using a checkbox and name array is one options like this: <form action="checkbox-form.php" method="post"> Select your numbers<br /> <table> <tr> <td><input type="checkbox" name="numbers[]" value="0" />0 </td> <td><input type="checkbox" name="numbers[]" value="1" />1 </td> <td><input type="checkbox" name="numbers[]" value="2" />2 </td> <td><input type="checkbox" name="numbers[]" value="4" />4 </td> </tr> <tr> <td><input type="checkbox" name="numbers[]" value="10" />A</td> <td><input type="checkbox" name="numbers[]" value="11" />B</td> <td><input type="checkbox" name="numbers[]" value="12" />C</td> <td><input type="checkbox" name="numbers[]" value="13" />D</td> </tr> </table> <input type="submit" name="formSubmit" value="Submit" /> </form> This is currently showing a brute force manual method, which would be replaced with a php loop to produce the same thing. The question then is, what do I do with the numbers[] array in checkbox-form.php? Meaning, what is the best Mysql type to store this in and what would the query look like? Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/#findComment-1067414 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 A user can select one or more numbers so I don't think a radio approach works unless each is its separate entity. Currently only one button can be checked in your code. 256 radio buttons - best way to handle it? Plus I provided a checkbox solution, maybe read my reply in it's entirety? If you want to limit it to 16 side-by-side you can do that by using CSS: float all <li>'s and put a height on the <ul>. Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/#findComment-1067550 Share on other sites More sharing options...
djbuddhi Posted June 4, 2010 Share Posted June 4, 2010 use a php for loop in side the radio button Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/#findComment-1067566 Share on other sites More sharing options...
split63 Posted June 4, 2010 Author Share Posted June 4, 2010 Plus I provided a checkbox solution, maybe read my reply in it's entirety? If you want to limit it to 16 side-by-side you can do that by using CSS: float all <li>'s and put a height on the <ul>. Your code helped quite a bit. I have a version of it running and it creates the form in 16 rows of 16 columns. Thanks Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/#findComment-1067855 Share on other sites More sharing options...
split63 Posted June 4, 2010 Author Share Posted June 4, 2010 ignace, I did stumble upon what seems to be a slight improvement. Instead of the dechex and strtoupper, I used sprintf("%02X", $i). The "X" says print in upper case Hex. This also allowed the number to always be two digits. So "5" is displayed as "05" Thanks, Link to comment https://forums.phpfreaks.com/topic/203777-256-radio-buttons-best-way-to-handle-it/#findComment-1067875 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.