【发布时间】:2014-12-30 06:49:41
【问题描述】:
我一直在寻找一种创建协作翻译小部件的方法。所以我有一个 mysql 数据库和表(称为翻译),以及一个允许用户一次翻译一页的小脚本。
但我不太相信我的剧本。我认为它不够有效。首先,mysql 获取所有“en”列为空的行,然后在屏幕上显示其中的一个。有没有其他方法可以做到这一点?这是代码:
//Retrieve all the data from the "translations" table
$result = mysql_query("SELECT * FROM translations WHERE en IS NULL OR en=''") or die(mysql_error());
$Randnum=rand(0,mysql_num_rows($result)-1); //Gets a random number between 0 and the maximum number of rows
$i=0; //Start to 0
while($Col = mysql_fetch_array($result)) //While there are rows to evaluate
{
if ($i==$Randnum)
{
echo "\"".$Col['es']."\"<br><br>Translate it to English: <br>";
}
$i++;
}
我一直在寻找类似 "echo $Col[$Randnum]['es']" 或 "echo $Col.$Randnum['es']" 的东西,而不是使用整个 while 循环来打印单个随机行。我该如何实现?如果只是优化问题。如果您可以提供一个脚本或想法来分配给 $Col 仅一行带有随机数和空的“en”col,那就更好了! (我认为这最后一点是不可能的)。 'en' 行是文本,所以我不知道如何实现 other methods,因为他们使用数字和 ORDER BY。
【问题讨论】:
标签: php mysql optimization