Jump to content

Create radio group from MySQL table


ssailer

Recommended Posts

I am trying to create a simple radio group based on the content of a MySQL table and it's not working.  Can anyone help?

 

<form action="" method="POST" name="form1" id="form1">

<?php do { ?>

<input name="fruitID" type="radio" value="<?php echo $row_getFruit['fruitID']; ?>" />

<?php echo $row_getFruit['fruitName']; ?><br />

<?php } while ($row_getFruit = mysql_fetch_assoc($getFruit)); ?>

</form>

Link to comment
https://forums.phpfreaks.com/topic/205192-create-radio-group-from-mysql-table/
Share on other sites

Use a "while" loop, instead of a "do while" loop:

<form action="" method="POST" name="form1" id="form1">
<?php
     while ($row_getFruit = mysql_fetch_assoc($getFruit)) {
          echo '<input name="fruitID" type="radio" value="' . $row_getFruit['fruitID'] . '" />';
          echo $row_getFruit['fruitName'] . "<br />\n";
     }
?> 
</form>

 

Ken

Okay - I found some code that I have been able to get working, but where do I add the form tags within this code?  I'm fairly new to PHP and it's not all making sense to me yet.  Plus, there will probably be TONS of other form fields, not all of them dynamic, and I'm completely lost as to where to put them...

 

<?php

$query = "SELECT * FROM fruit ORDER BY fruitName ASC";

$result = mysql_query($query) or die('Error : ' . mysql_error());

$sid = (isset($_POST['role']) ? $_POST['role'] : '');

while($row = mysql_fetch_assoc($result))

{

echo '<input type="radio" name="fruitID" value="' . $row['fruitid']

. '"' . ($sid == $row['fruitid']) . '>'

. $row['fruitName'] . "<br>\n";

}

?>

I got it working  :D

 

<form action="sresults.php" method="post" name="sform" id="sform">

  <div align="center">

  <?php do { ?> 

    <input name="fruitID" type="radio" value="<?php echo $row_getFruit['fruitID']?>" />

<?php echo $row_getFruit['fruitName']; ?><br />

<?php

} while ($row_getFruit = mysql_fetch_assoc($getFruit));

  $rows = mysql_num_rows($getFruit);

  if($rows > 0) {

      mysql_data_seek($getFruit, 0);

  $row_getFruit = mysql_fetch_assoc($getFruit);

  }

?>

    <input name="submit" type="submit" value="submit" />

  </div>

</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.