dongtao5055 2013-04-30 19:29
浏览 44
已采纳

从Joomla内容数据库中提取别名

So I am trying to pull the alias of the current article (the article that the code is placed in) from the database.

I have a PHP snippet to pull the alias of the category of the current article:

<?php // Get category alias
$db = &JFactory::getDBO();            

$option   = JRequest::getCmd('option');
$view   = JRequest::getCmd('view');

$temp   = JRequest::getString('id');
$temp   = explode(':', $temp);
$id   = $temp[0];

/* Checking if we are making up an article page */
if ($option == 'com_content' && $view == 'article' && $id)
{            
   /* Trying to get CATEGORY alias from DB */
   $db->setQuery('SELECT cat.alias FROM #__categories cat RIGHT JOIN #__content cont ON cat.id = cont.catid WHERE cont.id='.$id);   
   $category_alias = $db->loadResult();           
}
?>

I then use the variable $category_alias as a PHP argument for an iframe. All of this is embedded into the article using the plugin DirectPHP.

My problem is to rewrite this code to pull the alias of the current ARTICLE instead of the current CATEGORY.

I have read the Joomla documentation but I am completely stumped on what table/columns I need to use to get the alias of the articles.

Any help is very welcome!

  • 写回答

1条回答 默认 最新

  • dongyulian5801 2013-05-01 10:49
    关注

    You are using a lot of legacy code:

    • JRequest should not be used to access request parameters.
    • Objects are assigned per reference, so don't use the reference operator (&) for objects.
    • Use the query interface.
    • Pretect your code against SQL injection.

    The alias value you're looking for is stored in the content table together with the article.

    Your code - together with the suggested corrections - will then look like this:

    <?php
    $input  = JFactory::getApplication()->input;
    $option = $input->getCmd('option');
    $view   = $input->getCmd('view');
    $id     = array_shift(explode(':', $input->getString('id', 0)));
    
    if ($option == 'com_content' && $view == 'article' && !empty($id))
    {
        $db    = JFactory::getDBO();
        $query = $db->getQuery(true)
            ->select('alias')
            ->from('#__content')
            ->where('id=' . (int)$id);
        $db->setQuery($query);   
        $article_alias = $db->loadResult();           
    }
    

    Your article's alias will then be accessible through $article_alias (but only within the if block!).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要