Jump to content

random selection from database


burge124

Recommended Posts

hi, i have a basic query along the lines of...

 

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Question");

while($row = mysql_fetch_array($result))
  {
  echo $row['QuestionTitle'] . " " . $row['AnswerA'];
  echo "<br />";
  }

mysql_close($con);
?>

 

is there any possibility of out putting a random selection, so that every time the page is viewed another question appears from database

thanks

Link to comment
https://forums.phpfreaks.com/topic/100292-random-selection-from-database/
Share on other sites

thanks got that working, the query works fine but when i assign them to radio buttons they dont appear.

 

  <?php	

$result = mysql_query("SELECT * FROM question ORDER BY RAND() LIMIT 1");


while($row = mysql_fetch_array($result))
  {
   echo $row['QuestionTitle'];
   echo "<br />";
   }
   
   echo $row['CorrectAnswer'];
   echo "<br />";
   
?>

<INPUT TYPE="RADIO" NAME="question2"VALUE="answer2.1"><?php echo $row['AnswerA']; ?> <BR>
<INPUT TYPE="RADIO" NAME="question2"VALUE="answer2.2"><?php echo $row['AnswerB']; ?> <BR>
<INPUT TYPE="RADIO" NAME="question2" VALUE="answer2.3"><?php echo $row['AnswerC']; ?> <BR>

 

 

try this.......

if ($row['AnswerA']) { $check0 = " CHECKED"; }
if ($row['AnswerB']) { $check1 = "
CHECKED"; }
if ($row['AnswerC']){ $check2 = "
CHECKED"; }

<INPUT TYPE="RADIO" NAME="question2"VALUE="answer2.1"<?php echo $check0 ;?> ><BR>
<INPUT TYPE="RADIO" NAME="question2"VALUE="answer2.2"<?php echo $check1 ;?> ><BR>
<INPUT TYPE="RADIO" NAME="question2" VALUE="answer2.3"<?php echo $check2 ; ?> ><BR>

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.