2

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

Andre Bergmann
  • 59
  • 3
  • 10
  • what is your problem ? – echo_Me Feb 23 '13 at 16:02
  • 1
    I figured out that the field 'fulltext' from the query is not available - for this reason there's for sure an error. However maybe somebody can show a sample how to get the class for an article to avoid such errors in future ... – Andre Bergmann Feb 23 '13 at 16:11
  • 1
    i believe the problem is that you didn't escape name `fulltext`, try writing `\`fulltext\`` in your query – Marko D Feb 23 '13 at 16:41
  • Fetch content from `\`introtext\`` after checking if `\`fulltext\`` has returned null. If content is there it would be there in both or either of the two fields. – Viktor Tango Feb 23 '13 at 17:19

2 Answers2

3

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

Lodder
  • 19,758
  • 10
  • 59
  • 100
0

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.

Viktor Tango
  • 453
  • 9
  • 19