views:

33

answers:

1

does anyone know how i would fill data from mysql database in fb:random and iterate through it, to pick a random quote?

fb:random

$facebook->api_client->fbml_setRefHandle('quotes',
'<fb:random>
<fb:random-option>Quote 1</fb:random-option>
<fb:random-option>Quote 2</fb:random-option>
</fb:random>');

mysql data:

$rowcount = mysql_result($result, 0, 0);
$rand = rand(0,$rowcount-1);

$result = mysql_query("SELECT cQuotes, vAuthor, cArabic, vReference FROM thquotes LIMIT $rand, 1", $conn)
  or die ('Error: '.mysql_error());


$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
  echo "Empty";
}
else{

$fb_box = "<p>" . h($row['cArabic']) . "</p>";
$fb_box .= "<p>" . h($row['cQuotes']) . "</p>";
$fb_box .= "<p>" . h($row['vAuthor']) . "</p>";
$fb_box .= "<p>" . h($row['vReference']) . "</p>";
}
A: 

Generally, what I do when I want to pick up a random quote or other piece of data is I select all of the ids in the database, feed them into an array, and then choose at random from the array. Then use the id to pick out the one you want.

For example:

$sql = "SELECT `id` FROM `quotes`;";
$result = mysql_query($sql);
$count = mysql_num_rows($result);

$id = rand(1, $count);

$sql = "SELECT `text`,`author` FROM `quotes` WHERE `id`='$id' LIMIT 1;";
$result = mysql_query($sql);
$quote = mysql_fetch_assoc($result);
Kaji
i'm already doing that. but i would like to know how would i do this using the fb:random tag?
fuz3d