1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Query result in an article

Discussion in 'Community' started by enrb, Jun 29, 2020.

  1. enrb

    enrb Member

    Level: Community
    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)
     
  2. juuser

    juuser Active Member

    Level: Community
    You can use "sourcerer" Joomla extension to add code to your articles.
     
  3. enrb

    enrb Member

    Level: Community
    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?
     
  4. juuser

    juuser Active Member

    Level: Community
    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.
     
    troester likes this.
  5. hominid4

    hominid4 Member

    Level: Community
    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;

    ?>
     
    troester likes this.
  6. juuser

    juuser Active Member

    Level: Community
    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).
     
  7. hominid4

    hominid4 Member

    Level: Community
    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.
     
  8. juuser

    juuser Active Member

    Level: Community
    @hominid4, sorry, didn't notice the replay was from you, not the original poster :)
     
    hominid4 likes this.
  9. enrb

    enrb Member

    Level: Community

    Thanks so much!
    It works perfectly!
     
    hominid4 likes this.
  10. hominid4

    hominid4 Member

    Level: Community
    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
     

Share This Page