Jump to content


Photo

populating radio button form from database


  • Please log in to reply
13 replies to this topic

#1 kenjenn

kenjenn
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 October 2006 - 12:24 PM

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.

#2 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 01 October 2006 - 12:46 PM

You didn't put a $ infront of your "status", also, you don't need PHP tags in an echo, try this:

<?php
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" 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;
}
?>

~ Mutley.

#3 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 01 October 2006 - 12:48 PM

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

<?php
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>
    <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>";
?>


#4 kenjenn

kenjenn
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 October 2006 - 01:37 PM

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?

#5 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 01 October 2006 - 03:20 PM

Remove the single quote from this line
</tr>'";

Ronald  8)


RTFM is an almost extinct art form, it should be subsidized.

#6 kenjenn

kenjenn
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 October 2006 - 03:48 PM

Did that and same error.  Current code is:

<?php
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>
    <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>";


?>


#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 01 October 2006 - 04:39 PM

You need semi-colons at the end of the PHP code...

Your lines that end like this:
? "checked='checked'" : ''">

Need to have a semi colon after the php, before the last double quotes... like this:
? "checked='checked'" : '';">

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 kenjenn

kenjenn
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 October 2006 - 04:48 PM

no more errors, but all I get is a row of:

checked='checked'checked='checked'checked='checked'checked='checked' etc.

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 01 October 2006 - 04:56 PM

I'd say heredoc is what you need here...

Where you start to echo the content, change echo " to echo <<<HTML and then add HTML; above $i++.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 kenjenn

kenjenn
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 October 2006 - 05:00 PM

nope.  now I get 15 "; (double-quote semi-colon) above my empty table with 15 rows.

#11 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 01 October 2006 - 05:06 PM

Can you post your full code again now?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#12 kenjenn

kenjenn
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 October 2006 - 05:29 PM

thanks for helping.  full code:

<?php
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 <<<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>";


?>


#13 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 01 October 2006 - 07:02 PM

try
<?php
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>
    <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>";
?>


#14 kenjenn

kenjenn
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 01 October 2006 - 09:47 PM

That did the trick.  Thanks everybody.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users