Match *_content "alias" (id-XXX)

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Froman
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Apr 25, 2018 1:27 pm

Match *_content "alias" (id-XXX)

Postby Froman » Wed Apr 25, 2018 2:32 pm

Hello,

I've built a very simple Module that connects to an external database.

The function is to match a column in the external database with the alias(column) in *_content.
Everything works when I use id from *_content, however that will not scale for my project.

Code: Select all

   $db = JDatabaseDriver::getInstance( $option );

             // Retreive the Data                        
                $value = JFactory::getApplication()->input->get('ALIAS'); <-- I need the alias here instead of id.
                $query = $db->getQuery(true)
                            ->select('*')
                            ->from($db->quoteName('test'))
                            ->where($db->quoteName('FIELD FROM TABLE')."=".$db->quote($value));
             // Prepare the Query
                $db->setQuery($query);
             // Load the Row
                $result = $db->loadAssoc();
             // Return the Data
                return $result;


Code: Select all

URL: index.php/Categories/Sub-Categories/id-XXX

How do I isolate the XXX part of the url in Joomla?

User avatar
toivo
Joomla! Exemplar
Joomla! Exemplar
Posts: 9532
Joined: Thu Feb 15, 2007 5:48 am
Location: Nottingham, UK
Contact:

Re: Match *_content "alias" (id-XXX)

Postby toivo » Sun Apr 29, 2018 5:02 pm

The last part of the URL is usually the alias column from the menu item. If you need the alias from the content table instead, analyse the column 'link' of the menu entry, containing the non-SEF link, and extract the article id, then look up the article from #__content and retrieve the alias column.

Code: Select all

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Log\Log;
$app   = Factory::getApplication();
$db   = Factory::getDbo();
// menu item
$alias   = $app->getMenu()->getActive()->alias;
$msg    = 'menu alias = ' . $alias;
$app->enqueueMessage($msg, 'info');
// get menu item
$query = $db->getQuery(true)
         ->select('*')
         ->from($db->quoteName('#__menu'))
          ->where($db->quoteName('alias') . ' = ' . $db->quote($alias));   
$db->setQuery($query);
$result = $db->loadAssoc();
$title   = $result['title'];
$msg   = 'title from #__menu = ' . $title;
$app->enqueueMessage($msg, 'info');
// in case URL is needed
$url   = Uri::getInstance()->toString();
$msg   = 'URL = ' . $url;
$app->enqueueMessage($msg, 'info');
Toivo Talikka, Global Moderator
my first programs were assembled and run in 16KB :)
troubleshooting smtp and other articles https://talikka.com/joomla


Return to “Joomla! 3.x Coding”

Who is online

Users browsing this forum: No registered users and 3 guests