Search
Other Sourcerer questions
Forum

Use PHP in article anywhere tag?

Thomas Venugopal's Avatar Thomas Venugopal
Hello,

Example url: /testpage/?cid=0344

I have this php script:

- It reads the cid value from the URL, which in this case is 0344.
- It searches the Joomla database for an article in category 301 that has a custom field with ID 41 whose value is 0344.
Since there is one article with the ID 1512 that meets these conditions, 1512 would be the result of the query.
{source}
<?php
// get 'cid'-parameter from url
$cid = JFactory::getApplication()->input->getString('cid');

// check it parameter is present
if ($cid) {
    // Verbindung zur Datenbank herstellen
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    // search article in categorie 301, where the custom field 'c-cid' has the value of the url parameter 'cid'
    $query->select($db->quoteName('a.id'))
          ->from($db->quoteName('#__content', 'a'))
          ->join('INNER', $db->quoteName('#__fields_values', 'fv') . ' ON ' . $db->quoteName('a.id') . ' = ' . $db->quoteName('fv.item_id'))
          ->where($db->quoteName('a.catid') . ' = 301')
          ->where($db->quoteName('fv.field_id') . ' = 41') // Custom Field ID
          ->where($db->quoteName('fv.value') . ' = ' . $db->quote($cid));

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

    // echo article id
    if ($result) {
        echo $result;
    } else {
        echo 'no article';
    }
} else {
    echo 'no cid';
}
?>
{/source}

The result with the url /testpage/?cid=0344 is 1512.

Now I'd like to change the following tag so that the result of the query is in the id field of the following tag.
{article id="1512"}{article-hl id="[c-hl:value]"}[text]{/article-hl}{/article}


I tried to put the php script with sourcerer into the id tag like this but that doesn't work:
{article id="{source}
<?php
// get 'cid'-parameter from url
$cid = JFactory::getApplication()->input->getString('cid');

// check it parameter is present
if ($cid) {
    // Verbindung zur Datenbank herstellen
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);

    // search article in categorie 301, where the custom field 'c-cid' has the value of the url parameter 'cid'
    $query->select($db->quoteName('a.id'))
          ->from($db->quoteName('#__content', 'a'))
          ->join('INNER', $db->quoteName('#__fields_values', 'fv') . ' ON ' . $db->quoteName('a.id') . ' = ' . $db->quoteName('fv.item_id'))
          ->where($db->quoteName('a.catid') . ' = 301')
          ->where($db->quoteName('fv.field_id') . ' = 41') // Custom Field ID
          ->where($db->quoteName('fv.value') . ' = ' . $db->quote($cid));

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

    // echo article id
    if ($result) {
        echo $result;
    } else {
        echo 'no article';
    }
} else {
    echo 'no cid';
}
?>
{/source}"}{article-hl id="[c-hl:value]"}[text]{/article-hl}{/article}

Is it possible to combine these two plugins?
Peter van Westen's Avatar Peter van Westen ADMIN
Yes, should work... but:

Make sure the Sourcerer system plugin is ordered before Articles Anywhere.
See: regularlabs.com/blog/242-plugin-order-is-important

Make sure Sourcerer is not outputting any comments around its output (see the system plugin settings).
You can check this by disabling Articles Anywhere and checking the HTML output in the browser.


Also, if you are going to look up the id of an article like that (with joins and all)... why not directly search for the 'c-hl' value, and skip the nested {article} tag.
Or even grab the intro+fulltext and skip Articles Anywhere all together!

And also, all the stuff you are doing with Sourcerer now, you should be able to do with filters in Articles Anywhere.
See: docs4.regularlabs.com/articlesanywhere/data-tags/input-values
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