0

I have this problem that I can't resolve. In CMS Joomla I need to select data from table #__adsmanager_ads column ad_healine and from table #__modules column title.
Then check if ad_headline == title load module (that equal to ad_healine)

I use this code for single table select:

<?php
 Get a db connection.
$db = JFactory::getDbo();
 // Create a new query object.
$query = $db->getQuery(true);
$query
    ->select($db->quoteName(array('ad_headline')))
    ->from($db->quoteName('#__adsmanager_ads'));

    $db->setQuery($query);
    $results = $db->loadObjectList();
    $objectoarr = json_decode(json_encode($results),true); // Convert stdClass object to array
    print_r($objectoarr);     
  ?>

========================================================================= Next i write code below and problem is almost solver

<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query 
    ->select (array('a.ad_headline', 'b.title')) //Select columns from database
    ->from ($db->quoteName('#__adsmanager_ads','a'))
    ->join('LEFT', $db->quoteName('#__modules', 'b') . ' ON (' . $db->quoteName('a.ad_headline') . '=' . $db->quoteName('b.title').')');
$db->setQuery($query);
$result = $db->loadObjectList();
$objectoarr = json_decode(json_encode($result),true); // Convert stdClass object to array

foreach ($objectoarr as $adhealine){
        if ($adhealine[ad_headline] === $adhealine[title])
        {
            $document = &JFactory::getDocument();
            $renderer   = $document->loadRenderer('modules');
            $position   = 'inside-ad';
            $options   = array('style' => 'raw');
            echo $renderer->render($position, $options, null); 
        }
}
?>

But problem is that all modules set to position inside-ad loading there. But I need load only module which's name equivalent to ads name.

How can i solve this problem? Thank in advance

2
  • If you'd rather work with the result set as an array of arrays rather than an array of objects, then you shouldn't call loadObjectList() you should call loadAssocList(). this way you can avoid $objectoarr = json_decode(json_encode($result),true); which is obviously the long way around. I would be using an INNER JOIN to eliminate the possibility of joining to NULL rows. Since the INNER JOIN will ensure that you are only getting identical ad_headline and title values in each row, you can omit the php conditional: if ($adhealine[ad_headline] === $adhealine[title]). Jan 27, 2019 at 8:33
  • Did you manage to self-solve this issue or is this still a mystery to you? Are all of the modules being targeted in your query ALSO position = 'inside-ad'? Is it necessary to write into the query as a WHERE condition? Please progress all of your questions toward a system-recognized resolution. This is important to this this community for so many reasons. Please add your complete and educational answer or update your question. Thank you. Jan 27, 2019 at 8:35

1 Answer 1

1

You want to use a join in your query to pull back just the records where the module title is the same as the ad headline, so, start by reading this Joomla Docs page and the MySQL page. Then you will need to create a query something like:

$query->from('#__adsmanager_ads AS ads');
$query->select('ads.ad_headline')
      ->join(
             'LEFT',
             $db->quoteName('#__modules') . ' ON m.title = ads.ad_headline'
             )

N.B. _Please note I've just typed this into the browser, so it's probably got mistakes in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.