Search
Other Sourcerer questions
Forum

Syntax translation needed

Herbert Kleinschmidt's Avatar Herbert Kleinschmidt
Sorry for this question, but in the sourcerer for J4 documentation there is a remark about "better (more stable)" code to use. I run out of php knowledge the recent years. How would a syntax I still understand like
$query = "SELECT COUNT(id) from #__content WHERE catid = 17 AND state = 1";

$database->setQuery($query);
$result = $database->loadResult();
look like in the proposed better way?
$query = $db->getQuery(true)
      ->select($db->quoteName('something'))
      ->from('#__mydatabasetable')
      ->where($db->quoteName('this').' = '.$db->quote('that'));

   $db->setQuery($query);
   $result = $database->loadResult();
I would manage if there weren't the "count()" and "AND"…
Thx for your hint.
Peter van Westen's Avatar Peter van Westen ADMIN
Try:
$query = $db->getQuery(true)
      ->select('COUNT(' . $db->quoteName('id') . ')')
      ->from('#__content')
      ->where($db->quoteName('catid') . ' = 17')
      ->where($db->quoteName('state') . ' = 1');

   $db->setQuery($query);
   $result = $database->loadResult();

Or use Articles Anywhere PRO ?
{articles category="17" limit="1"}[category] has [total-no-limit] article(s).{/articles}
Please post a rating at the Joomla! Extensions Directory
Herbert Kleinschmidt's Avatar Herbert Kleinschmidt
Thumbs up!
Herbert Kleinschmidt's Avatar Herbert Kleinschmidt
Yet sth went wrong:
syntax error, unexpected variable "$db", expecting ")"
Herbert Kleinschmidt's Avatar Herbert Kleinschmidt
I added that missing dot:
->select('COUNT(' . $db->quoteName('id') . ')')
Now it works.
Peter van Westen's Avatar Peter van Westen ADMIN
?
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription