kenjenn Posted October 1, 2006 Share Posted October 1, 2006 I'm trying to populate a radio-button form from MySQL but can't seem to get the right php worked out to display. Here's what I've got:echo "<form name=attendance action=$_SERVER[PHP_SELF] method=POST>";echo '<input type=submit value="Submit Changes">';$i=0;echo '<table>';while ($row=mysql_fetch_row($result1)) { $IDstudent=$row[0];$name = $row[1];$status = $row[2];echo '<tr><input name=studentid['.$i.'] type=hidden value=' . $IDstudent . '><td>' . $name . '</td><td>' . $status . '</td><td><input name=status['.$i.'] type=radio value="P" <?php if($status=="P"){echo "checked"};?>>Present</td><td><input name=status['.$i.'] type=radio value="T" <?php if($status=="T"){echo "checked"};?>>>Tardy</td><td><input name=status['.$i.'] type=radio value="U" <?php if($status=="U"){echo "checked"};?>>Unexcused Absence</td><td><input name=status['.$i.'] type=radio value="E" <?php if($status=="E"){echo "checked"};?>>>Excused Absence</td></tr>';++$i;}Any help on getting the IF statement to work to "Check" the appropriate radio button?Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/ Share on other sites More sharing options...
Mutley Posted October 1, 2006 Share Posted October 1, 2006 You didn't put a $ infront of your "status", also, you don't need PHP tags in an echo, try this:[code]<?phpecho "<form name=attendance action=$_SERVER[PHP_SELF] method=POST>";echo '<input type=submit value="Submit Changes">';$i=0;echo '<table>';while ($row=mysql_fetch_row($result1)) {$IDstudent=$row[0];$name = $row[1];$status = $row[2];echo '<tr><input name=studentid['.$i.'] type=hidden value=' . $IDstudent . '><td>' . $name . '</td><td>' . $status . '</td><td><input name=$status['.$i.'] type=radio value="P" if($status=="P"){echo "checked"};>Present</td><td><input name=$status['.$i.'] type=radio value="T" if($status=="T"){echo "checked"};>Tardy</td><td><input name=<?=$status['.$i.'] type=radio value="U" if($status=="U"){echo "checked"};>Unexcused Absence</td><td><input name=status['.$i.'] type=radio value="E" if($status=="E"){echo "checked"};>Excused Absence</td></tr>';++$i;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101788 Share on other sites More sharing options...
ignace Posted October 1, 2006 Share Posted October 1, 2006 First of all before posting make sure your text is between the code tags so the syntax is colored,second of all add some whitespace for readability... thx ;) ;D[code]<?phpecho "<form name='attendance' action='{$_SERVER[PHP_SELF]}' method='POST'>";echo "<input type='submit' value='Submit Changes'>";$i=0;echo '<table>';while ($row=mysql_fetch_row($result1)) { $IDstudent=$row[0]; $name = $row[1]; $status = $row[2]; echo " <tr> <td> <input name='studentid[{$i}]' type='hidden' value='{$IDstudent}'> </td> <td> {$name} </td> <td> {$status} </td> <td> <input name='status[{$i}]' type='radio' value='P'" . (strtolower($status) == 'p') ? "checked='checked'" : ''"> Present </td> <td> <input name='status[{$i}]' type='radio' value='T'" . (strtolower($status) == 't') ? "checked='checked'" : ''"> Tardy </td> <td> <input name='status[{$i}]' type='radio' value='U'" . (strtolower($status) == 'u') ? "checked='checked'" : ''"> Unexcused Absence </td> <td> <input name='status[{$i}]' type='radio' value='E'" . (strtolower($status) == 'e') ? "checked='checked'" : ''"> Excused Absence </td> </tr>'";$i++;}echo "</table>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101790 Share on other sites More sharing options...
kenjenn Posted October 1, 2006 Author Share Posted October 1, 2006 ignace, thanks for the tips. I cut & pasted your code, but when I ran it I received the following parse error:Parse error: parse error, unexpected '"', expecting ',' or ';' in ....Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101807 Share on other sites More sharing options...
ronverdonk Posted October 1, 2006 Share Posted October 1, 2006 Remove the single quote from this line [code] </tr>'";[/code]Ronald 8) Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101825 Share on other sites More sharing options...
kenjenn Posted October 1, 2006 Author Share Posted October 1, 2006 Did that and same error. Current code is:[code]<?phpecho "<form name='attendance' action='{$_SERVER[PHP_SELF]}' method='POST'>";echo "<input type='submit' value='Submit Changes'>";$i=0;echo '<table>';while ($row=mysql_fetch_row($result1)) { $IDstudent=$row[0]; $name = $row[1]; $status = $row[2]; echo " <tr> <td> <input name='studentid[{$i}]' type='hidden' value='{$IDstudent}'> </td> <td> {$name} </td> <td> {$status} </td> <td> <input name='status[{$i}]' type='radio' value='P'" . (strtolower($status) == 'p') ? "checked='checked'" : ''"> Present </td> <td> <input name='status[{$i}]' type='radio' value='T'" . (strtolower($status) == 't') ? "checked='checked'" : ''"> Tardy </td> <td> <input name='status[{$i}]' type='radio' value='U'" . (strtolower($status) == 'u') ? "checked='checked'" : ''"> Unexcused Absence </td> <td> <input name='status[{$i}]' type='radio' value='E'" . (strtolower($status) == 'e') ? "checked='checked'" : ''"> Excused Absence </td> </tr>";$i++;}echo "</table>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101838 Share on other sites More sharing options...
HuggieBear Posted October 1, 2006 Share Posted October 1, 2006 You need semi-colons at the end of the PHP code...Your lines that end like this:[code=php:0]? "checked='checked'" : ''">[/code]Need to have a semi colon after the php, before the last double quotes... like this:[code=php:0]? "checked='checked'" : '';">[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101870 Share on other sites More sharing options...
kenjenn Posted October 1, 2006 Author Share Posted October 1, 2006 no more errors, but all I get is a row of:checked='checked'checked='checked'checked='checked'checked='checked' etc. Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101875 Share on other sites More sharing options...
HuggieBear Posted October 1, 2006 Share Posted October 1, 2006 I'd say heredoc is what you need here...Where you start to echo the content, change [b]echo "[/b] to [b]echo <<<HTML[/b] and then add [b]HTML;[/b] above [b]$i++[/b].RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101885 Share on other sites More sharing options...
kenjenn Posted October 1, 2006 Author Share Posted October 1, 2006 nope. now I get 15 "; (double-quote semi-colon) above my empty table with 15 rows. Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101889 Share on other sites More sharing options...
HuggieBear Posted October 1, 2006 Share Posted October 1, 2006 Can you post your full code again now?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101896 Share on other sites More sharing options...
kenjenn Posted October 1, 2006 Author Share Posted October 1, 2006 thanks for helping. full code:[code]<?phpecho "<form name='attendance' action='$_SERVER[PHP_SELF]' method='POST'>";echo "<input type='submit' value='Submit Changes'>";$i=0;echo '<table>';while ($row=mysql_fetch_row($result1)) { $IDstudent=$row[0]; $name = $row[1]; $status = $row[2]; echo <<<HTML <tr> <td> <input name='studentid[{$i}]' type='hidden' value='{$IDstudent}'> </td> <td> {$name} </td> <td> {$status} </td> <td> <input name='status[{$i}]' type='radio' value='P'" . (strtolower($status) == 'p') ? "checked='checked'" : '';"> Present </td> <td> <input name='status[{$i}]' type='radio' value='T'" . (strtolower($status) == 't') ? "checked='checked'" : '';"> Tardy </td> <td> <input name='status[{$i}]' type='radio' value='U'" . (strtolower($status) == 'u') ? "checked='checked'" : '';"> Unexcused Absence </td> <td> <input name='status[{$i}]' type='radio' value='E'" . (strtolower($status) == 'e') ? "checked='checked'" : '';"> Excused Absence </td> </tr>";HTML;$i++;} echo "</table>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101920 Share on other sites More sharing options...
sasa Posted October 1, 2006 Share Posted October 1, 2006 try[code]<?phpecho "<form name='attendance' action='$_SERVER[PHP_SELF]' method='POST'>";echo "<input type='submit' value='Submit Changes'>";$i=0;echo '<table>';while ($row=mysql_fetch_row($result1)) { $IDstudent=$row[0]; $name = $row[1]; $status = $row[2]; echo " <tr> <td> <input name='studentid[{$i}]' type='hidden' value='{$IDstudent}'> </td> <td> {$name} </td> <td> {$status} </td> <td> <input name='status[{$i}]' type='radio' value='P'";echo $x =(strtolower($status) == 'p') ? "checked='checked'" : ''; echo "> Present </td> <td> <input name='status[{$i}]' type='radio' value='T'"; echo $x =(strtolower($status) == 't') ? "checked='checked'" : ''; echo "> Tardy </td> <td> <input name='status[{$i}]' type='radio' value='U'"; echo $x = (strtolower($status) == 'u') ? "checked='checked'" : ''; echo "> Unexcused Absence </td> <td> <input name='status[{$i}]' type='radio' value='E'"; echo $x = (strtolower($status) == 'e') ? "checked='checked'" : ''; echo "> Excused Absence </td> </tr>";$i++;} echo "</table>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-101955 Share on other sites More sharing options...
kenjenn Posted October 1, 2006 Author Share Posted October 1, 2006 That did the trick. Thanks everybody. Quote Link to comment https://forums.phpfreaks.com/topic/22654-populating-radio-button-form-from-database/#findComment-102004 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.