Query result in an article

enrb

Member
Is it possible to create a query that gives me a result and then show the result in an article?
For example, I would like to write in an article: the total of invoices to be collected is (result_of_query)
 
I tried, but it doesn't work ...

I wrote the following code:
{source} {SELECT Sum (invoice.price) FROM invoice WHERE invoice.data> 0} {/ source}

Am I wrong?
 
No idea :) There seems to be an extra space in your closing tag: {/_source}. That might make a difference. As this is pure Joomla/Sourcerer thing, not Fabrik, you could probably get answers faster e.g. in stackoverflow, Joomla or RegularLabs forums.
 
If you copy/paste the below code into your Sourcerer window, including the opening and closing PHP tags, does it point you in the right direction?

PHP:
<?php

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

$myQuery = "SELECT SUM(invoice.price) FROM invoice WHERE invoice.data > 0";
$myDb->setQuery($myQuery);
$invoicePriceSum = $myDb->loadResult();

echo $invoicePriceSum;

?>
 
Your code seems fine, make sure your editor does not strip the code. Check if it works without wysiwyg editor (switch in Joomla global settings).
 
Hi juuser, my code was in reference to them using that code within Sourcerer. I'd recommend them not entering PHP directly within the editor.
 
If you copy/paste the below code into your Sourcerer window, including the opening and closing PHP tags, does it point you in the right direction?

PHP:
<?php

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

$myQuery = "SELECT SUM(invoice.price) FROM invoice WHERE invoice.data > 0";
$myDb->setQuery($myQuery);
$invoicePriceSum = $myDb->loadResult();

echo $invoicePriceSum;

?>


Thanks so much!
It works perfectly!
 
Thanks so much!
It works perfectly!

Great to hear. When using Sourcerer, you still have to build the query, such as with the example tasks below, just wrap your PHP in <php> tags and use 'echo' instead of 'return' if you need to output your result(s).

http://fabrikar.com/forums/index.php?wiki/php-common-tasks

Regarding Sourcerer's built-in variables, they do have "ready-to-use" variables listed below that creates a connection to your Joomla Db, creates your query object, etc, but it's recommended to use your own variable names such as $myDb and $myQuery instead of the core $db and $query variables so not to potentially conflict with Joomla and Fabrik core variables, so I'll usually enter my own Db connection and query object lines within Sourcerer (as I did above) instead of using their built-in variables just to keep my code consistent with my Fabrik PHP code. But either the case, it's nice to know Sourcerer's built-in variables and such just in case.

https://www.regularlabs.com/extensi...g-php-code-ready-to-use-php-variables-objects
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top