迭代fb:random

时间:2010-04-19 22:34:22

标签: php mysql iteration facebook

有谁知道我将如何从fb中的mysql数据库填充数据:随机并迭代它,选择一个随机引用?

FB:随机

$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数据:

$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>";
}

1 个答案:

答案 0 :(得分:0)

通常,当我想要获取随机引用或其他数据时,我所做的是选择数据库中的所有id,将它们提供给数组,然后从中随机选择数组。然后使用id挑选出您想要的那个。

例如:

$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);