fraser5002 Posted June 30, 2010 Share Posted June 30, 2010 Hi my question involves a bit of php and a bit of HTMl so i thought i would ask it anyway. I have an array stored in my MQSQL database as a serialized string, i have unserealized the string after the query which works fine so the data is now stored as a php variable as an array The array is 300 elements long and each element is either a 1 or a 0 What i would like to do is know a fast way of displaying this information on the page I have an html table of 300 rows each with 2 radio buttons. I would like each row in the table to refer to the corresponding position in the array and select either radio button 1 if the value is yes or radio button 2 if the answer is 0. How would i code this in PHP? Hope i have explained it enough . If not just let me know Many Thanks Fraser Link to comment https://forums.phpfreaks.com/topic/206322-my-sql-query-help/ Share on other sites More sharing options...
ChemicalBliss Posted June 30, 2010 Share Posted June 30, 2010 Displaying PHP Data is very easy with Loops: <?php // - use mysql etc and grab your data - unserialize it into this array: $unserialized_array; echo("<form action='' method='POST'><table width='500'>"); // Loop each item in the array, save the "Key" and "Value" of each array element to their respective variables. foreach($unserialized_array as $key => $value){ // Make sure these variables start with nothing in them. $yes = NULL; $number0 = NULL; // varaibe names cannot start with a number // Find which radio should be checked by default if($value == " checked"){ $yes = "yes"; }else{ $no = " checked"; } // The \n at the end is a New Line (only visible in the source code). echo("<tr><td width='80%'>".$key."</td><td width='20%'>yes: <input type='radio' name='".$key."' value='yes' ".$yes."/><br>0: <input type='radio' name='".$key."' value='0' ".$no."/></td></tr>\n"); } echo("</table>"); echo("<input type='submit' value='Submit Form' /></form>"); ?> Hope this helps, -cb- Link to comment https://forums.phpfreaks.com/topic/206322-my-sql-query-help/#findComment-1079323 Share on other sites More sharing options...
marcus Posted June 30, 2010 Share Posted June 30, 2010 What I interpreted <?php $array = array(1,1,0,1,0,0,0,1,1,1,0,1); echo '<table cellspacing="3" cellpadding="3">'; function a($n,$x){ if($n == 1) return '<input type="radio" name="'.$x.'" value="1" CHECKED /> Yes • <input type="radio" name="'.$x.'" value="0" /> No'; else return '<input type="radio" name="'.$x.'" value="1" /> Yes • <input type="radio" name="'.$x.'" value="0" CHECKED /> No'; } $x = 1; foreach($array AS $set){ echo "<tr><td>Answer:</td><td>".a($set,$x)."</td></tr>"; $x++; } echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/206322-my-sql-query-help/#findComment-1079326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.