我正在尝试创建一个 joomla 组件。该组件是关于生成一个句子。基本上,这是帮助我生成它的代码/函数。
function getCategory()
{
$cat = array("walk","reside","eat");
return $b = $cat[array_rand($cat)];
}
function getS1() //function 1.1
{
$db = JFactory::getDBO();
$query = "select words from #__wordbank where function = 1.1 order by rand() LIMIT 1";
$db->setQuery($query);
return $db->loadResult();
}
function getV11() //function 2.11
{
$db = JFactory::getDBO();
$b = getCategory();
$query = "select words from #__wordbank where function = 2.11 AND category = '$b' order by rand() LIMIT 1";
$db->setQuery($query);
return $db->loadResult();
}
function getP1() //function p1.1
{
//load preposition
$db = JFactory::getDBO();
$b = getCategory();
$query = "select words from #__wordbank where function = 'p1.1' AND category = '$b' order by rand() LIMIT 1";
$db->setQuery($query);
return $db->loadResult();
}
function getP2() //function p1.2
{
//load noun
$db = JFactory::getDBO();
$b = getCategory();
$query = "select words from #__wordbank where function = 'p1.2' AND category = '$b' order by RAND() limit 1";
$db->setQuery($query);
return $db->loadResult();
}
问题是当我getCategory()
返回值总是不同时。我尝试使用 if else 但它仍然返回不同的值,因为$cat[array_rand($cat)]
. 如果你们能帮我解决这个错误,将不胜感激