COUNT with Calc element and php variable

mkainz

Member
Hi! I need to create something like this:

$data = 10;
$query = "SELECT COUNT(element_1)
FROM table_1
WHERE element_1 != '0'
AND element_2 == $data;
return $query;

how can i do it?
 
mkainz,

Did you ever get this working? I'm pulling my hair out on this and don't see how the link above is very helpful.
 
That link gives you some examples of how to use the Joomla database API. If you need more details on the J! API ...

https://docs.joomla.org/Selecting_data_using_JDatabase

The original post would be achieved by doing ...

Code:
$myData = 10;
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery->select('COUNT(element_1) AS mycount')->from('table_1')->where('element_1 != "0"')->where('element_2 = ' .  $myDb->quote($myData));
$myDb->setQuery($myQuery);
return $myDb->loadResult();

-- hugh
 
Last edited:
PHP:
$myQuery->select('COUNT(element_1) AS mycount')->from('table_1')->where('element_1 != "0"')->where('element_2 = ' .  $myDb->quote($myData));
 
Hi , could you tel me why this one doesnt work ?
I have tried many ways for that simple query.
Thanks for your answer
Code:
$cours_id = '{admin3_cours___id}';

$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);

$myQuery
->select('COUNT('scolarite_id')')
->from('admin3_cours_planning')
->where('admin3_cours_planning.cours_id  = '.$cours_id);
$myDb->setQuery($myQuery);
$nbeleve = $myDb->loadResult();
return $nbeleves;
 
What do you get, an error or 0?
Try $cours_id = '{admin3_cours___id_raw}';

For debugging:

Do you have jdump installed?
Then you can do
dump($cours_id,'some remark');
otherwise
var_dump($cours_id);exit;
to see what you get.
 
Finaly this worked
Code:
$cours_id = '{admin3_cours___id}';

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select('COUNT(*)');
$query->from($db->quoteName('admin3_cours_planning'));
$query->where($db->quoteName('admin3_cours_planning.cours_id')." = ".$db->quote($cours_id));
$db->setQuery($query);
$nbeleve = $db->loadResult();
return $nbeleve;
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top