0

I'm trying to select data from content table in Joomla (3.4) filtered by language field. When I pass a value, it doesn't change in the SQL statement

Here is my code:

public function articlesar()
{
    $language = JRequest::getString('language', 'en-GB');
    $return_arr = array();

    $query= "SELECT id, title, introtext, created, images,language FROM `#__content` WHERE  language='$language' ";

    $db = &JFactory::getDBO(); 
    $db->setQuery($query);
    $row = $db->loadRowList();

    foreach($row as $val) 
    {
        $row_array['article_id'] = $val[0];
        $row_array['title'] = $val[1];
        $row_array['introtext'] = $val[2];
        $row_array['created'] = $val[3];
        $row_array['image'] = $val[4];
        $row_array['language'] = $val[5];

        array_push($return_arr,$row_array);
    }

    header('Content-Type: application/json');
    echo json_encode($return_arr);
    jexit();
}
  • JRequest is deprecated get the API17:JInput object from the application instead – pl71 Jun 3 '15 at 18:17
  • How you are passing value to this function? through request parameter? what is the url parameter you are passing? – Nagarjun Jun 3 '15 at 19:18
  • actually, I'm new to joomla and I'm just want to get the data to used in android app. I'm using component, which is using JRequest to get data ,so I kept using it. My url : localhost/press/index.php/en/… – zorba Jun 3 '15 at 19:41
  • I think the problem in the JRequest::getString because the field in database is Char, and it works with String field, is there any alternative function to get Char field – zorba Jun 3 '15 at 19:58
  • @zorba if at least one of the answers below has solved your issue, please award the most helpful answer with the green tick so that this page is deemed resolved by the system and the more supportive volunteer is awarded rep points for their excellent contribution. – mickmackusa Jan 27 '19 at 11:31
1

As already mentioned in the comments, you use use JInput instead of JRequest:

$jinput = JFactory::getApplication()->input;
$language = $jinput->get('language, 'en-GB');

However that isn't the issue you see. The issue is more that you don't pass the language parameter in the URL (using &language=de-DE for example). Now if you instead want to have the language that is currently active on the site, you could use $language = JFactory::getLanguage()->getTag();.

You may also want to look into $db->loadAssocList() instead of $db->loadRowList(), which should give you directly an associative array. See https://docs.joomla.org/Selecting_data_using_JDatabase for more details

| improve this answer | |
1

I've run into a few issues with queries not working when values aren't escaped, so I'd recommend using the up to date coding standards for your database query. In addition to this, ensure you don't use JRequest as it's deprecated and will soon be removed.

Try using the following:

public function articlesar()
{
    $language = JFactory::getLanguage()->getTag();
    $db       = JFactory::getDbo();

    $query = $db->getQuery(true);    
    $query->select($db->quoteName(array('id', 'title', 'introtext', 'created', 'images', 'language')))
          ->from($db->quoteName('#__content'))
          ->where($db->quoteName('language') . ' = '. $db->quote($language));    
    $db->setQuery($query);

    $row = $db->loadAssocList();

    $return_arr = array();

    foreach($row as $val) 
    {
        $row_array['article_id']    = $val[0];
        $row_array['title']         = $val[1];
        $row_array['introtext']     = $val[2];
        $row_array['created']       = $val[3];
        $row_array['image']         = $val[4];
        $row_array['language']      = $val[5];

        array_push($return_arr, $row_array);
    }

    header('Content-Type: application/json');
    echo json_encode($return_arr);
    jexit();
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.