• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

beginner random question from table

I want to select 5 random questions and have a url for each, here is my code:

<?
            $resultID = mysql_query("SELECT subject FROM tickets ORDER BY RAND() LIMIT 5");

  $num_results = mysql_num_rows($resultID);



// Define your colors for the alternating rows

$color1 = "#d6ddef";
$color2 = "#ffffff";
$row_count = 1;

while ($row = mysql_fetch_row($resultID))
{
$row_color = ($row_count % 2) ? $color1 : $color2;


   
     foreach ($row as $field)
     {
           print "$row_count <a href='test.html'> $field</a>";
     }
     print "<br /><br />";
     
          $row_count++;
}


?>

The problem with this is the select statement, if i do SELECT subject,id FROM tickets ORDER BY RAND() LIMIT 5   it will screw up the table.  I want to be able to change test.html to view.php?id=$id , where id is coming from the select RAND() sql statement.  Thanks
0
hermanyang
Asked:
hermanyang
  • 3
  • 2
1 Solution
 
elfe69Commented:
Use the following code:

<?
$resultID = mysql_query("SELECT subject FROM tickets ORDER BY RAND() LIMIT 5");
// Define your colors for the alternating rows
$color1 = "#d6ddef";
$color2 = "#ffffff";
$row_count = 1;
while ($row = mysql_fetch_row($resultID)) {
     $row_color = ($row_count % 2) ? $color1 : $color2;
     print $row_count, " <a href='view.php?id=", $row['id'], "'> ", $row['subject'], "</a>";
     print "<br/><br/>";
     $row_count++;
}
?>
0
 
SpikinssonCommented:
The solution above is what you want, but one thing has to change:


<?
$resultID = mysql_query("SELECT id, subject FROM tickets ORDER BY RAND() LIMIT 5");
// Define your colors for the alternating rows
$color1 = "#d6ddef";
$color2 = "#ffffff";
$row_count = 1;
while ($row = mysql_fetch_row($resultID)) {
     $row_color = ($row_count % 2) ? $color1 : $color2;
     print $row_count, " <a href='view.php?id=", $row['id'], "'> ", $row['subject'], "</a>";
     print "<br/><br/>";
     $row_count++;
}
?>

Notice the added 'id' column in the query.

It should work fine now.

Greets,
Mike
0
 
hermanyangAuthor Commented:
    print $row_count, " <a href='view.php?id=", $row['id'], "'> ", $row['subject'], "</a>";  

returns a

Parse error: syntax error, unexpected ','
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
elfe69Commented:
You should replace print by echo:

<?
$resultID = mysql_query("SELECT id, subject FROM tickets ORDER BY RAND() LIMIT 5");
// Define your colors for the alternating rows
$color1 = "#d6ddef";
$color2 = "#ffffff";
$row_count = 1;
while ($row = mysql_fetch_row($resultID)) {
     $row_color = ($row_count % 2) ? $color1 : $color2;
     echo $row_count, " <a href='view.php?id=", $row['id'], "'> ", $row['subject'], "</a>";
     echo "<br/><br/>";
     $row_count++;
}
?>
0
 
hermanyangAuthor Commented:
hmm....so now just 1 through 5 displays...no data actually returns..

i know the sql used to work with just subject....and in the tables ID exists
0
 
elfe69Commented:
OK... use mysql_fetch_assoc() instead of mysql_fetch_row():

<?
$resultID = mysql_query("SELECT id, subject FROM tickets ORDER BY RAND() LIMIT 5");
// Define your colors for the alternating rows
$color1 = "#d6ddef";
$color2 = "#ffffff";
$row_count = 1;
while ($row = mysql_fetch_assoc($resultID)) {
     $row_color = ($row_count % 2) ? $color1 : $color2;
     echo $row_count, " <a href='view.php?id=", $row['id'], "'> ", $row['subject'], "</a>";
     echo "<br/><br/>";
     $row_count++;
}
?>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now