Access Joomla 2.5 from external script to get article by id

狂风中的少年 提交于 2019-11-28 09:53:10

问题


I like to read an article by id from Joomla 2.5. As I'm not inside the framework I've to include it first. I found also some samples how to get an article by id than but it ever fails ... This is the sample I'm trying:

define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
echo JPATH_BASE .DS.'includes'.DS.'framework.php';
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
echo  $mainframe->getCfg('sitename');

$articleId = JRequest::getInt('Itemid');
$db =& JFactory::getDBO();

$sql = "SELECT fulltext FROM #__content WHERE id = 260"; //.intval($articleId);
$db->setQuery($sql);
$fullArticle = $db->loadResult();

Article ID 260 is available but it ever return null ...

When I trace it $cursor in loadResult() is every null:

public function loadResult()
{
    // Initialise variables.
    $ret = null;

    // Execute the query and get the result set cursor.
    if (!($cursor = $this->execute()))
    {
        return null;
    }
     ...

Can somebody help please?

Thanks Andre


回答1:


There are a few things you don't need in your script and I have changed it to Joomla 2.5 coding standards:

define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('introtext')
 ->from('#__content')
 ->where('id = 260');
$db->setQuery($query);

$fullArticle = $db->loadResult();

echo $fullArticle;

Hope this helps




回答2:


Try this SQL query, it should work now!

$sql  = 'SELECT `fulltext` FROM `#__content` WHERE `id` = 260';

What Lodder has said is exactly the same, only better structured than your query.

So you can code in that style if it suites you. In his way of coding it would become:

$sql = $db->getQuery(true);
$sql->select('fulltext')
 ->from('#__content')
 ->where('id = 260');
$db->setQuery($sql);

Easy to read and modify this way, isn't it? :)

Also, you can fetch the content from `introtext` as the `fulltext` is null at times.



来源:https://stackoverflow.com/questions/15042262/access-joomla-2-5-from-external-script-to-get-article-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!