简体   繁体   中英

Bug in joomla component

i'm trying to create a joomla component. The component is about generating a sentence. Basicly, here's the code/function that will help me generate it.

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();
}

the problem is when i getCategory() the return value is always different. I try to use if else but still it return different value because of $cat[array_rand($cat)] . Would appreciate if you guys could help me solve this bug

you have to use array_rend. This function Pick one or more random entries out of an array

See the url:-

http://php.net/manual/en/function.array-rand.php

<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM