The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Fri Feb 17, 2012 2:58 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Feb 17, 2012 2:07 am
Posts: 4
I've looked High and Low for an Answer but nothing. So hope you can help.

I'm trying to place some php in my article. I need the articles title. I found this snippet which apparently works (according to others):

Code:
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
if ($option=="com_content" && $view=="article") {
    $ids = explode(':',JRequest::getString('id'));
    $article_id = $ids[0];
    $article =& JTable::getInstance("content");
    $article->load($article_id);
    echo $article->get("title");
}


I made one change already: the $ids... and $article_id... lines were causing trouble. So I replaced them with

Code:
article_id = JRequest::getString('id')


since that delivers the right id and I get no errors.


But as soon as I have this line in there the whole page goes blank!!!!

Code:
$article =& JTable::getInstance("content");


Weirdly enough typing print_r(JTable::getInstance("content")) gives me a nice listing of the contents of JTableContent object. But why can't I refer to it?

I'm using a 1.7.3 Installation on a local server. To write PHP in the article I'm using DirectPHP.

Thanks a lot in advance


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 5:46 am 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 07, 2011 5:25 am
Posts: 27
look at this code in JTable::getInstance

Code:
public static function getInstance($type, $prefix = 'JTable', $config = array())
{
        // Sanitize and prepare the table class name.
        $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
        $tableClass = $prefix.ucfirst($type);
 
        // Only try to load the class if it doesn't already exist.
        if (!class_exists($tableClass)) {
                // Search for the class file in the JTable include paths.
                jimport('joomla.filesystem.path');
                if ($path = JPath::find(JTable::addIncludePath(), strtolower($type).'.php')) {
                        // Import the class file.
                        require_once $path;
 
                        // If we were unable to load the proper class, raise a warning and return false.
                        if (!class_exists($tableClass)) {
                                JError::raiseWarning(0, 'Table class ' . $tableClass . ' not found in file.');
                                return false;
                        }
                } else {
                        // If we were unable to find the class file in the JTable include paths, raise a warning and return false.
                        JError::raiseWarning(0, 'Table ' . $type . ' not supported. File not found.');
                        return false;
                }
        }
 
        // If a database object was passed in the configuration array use it, otherwise get the global one from JFactory.
        if (array_key_exists('dbo', $config)) {
                $db = &$config['dbo'];
        } else {
                $db = & JFactory::getDbo();
        }
 
        // Instantiate a new table class and return it.
        return new $tableClass($db);
}


you get error "not supported. File not found." or "not found in file" ?

if you get error.
you mun used "JTable::addIncludePath()" before getInstance

example

JTable::addIncludePath("path_of_your_table");
$table=JTable::getInstance("table_name");


Top
 Profile  
 
PostPosted: Fri Feb 17, 2012 11:33 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Feb 17, 2012 2:07 am
Posts: 4
Great. Thanks a lot. Seems logical. Now all i have to do is figure out what my path is.
I'm feeling really stupid that I'm not getting this, but I have limited php experience.

error log says:
Code:
PHP Fatal error:  Call to a member function get() on a non-object in C:\xampp\htdocs\Joomla17\components\com_content\views\article\view.html.php


I tried the "JTable::addIncludePath()" with "getcwd()" and "C:\xampp\htdocs\Joomla17\components\com_content\views\article\" but all resulting in same error.

Thanks again.


Top
 Profile  
 
PostPosted: Sat Feb 18, 2012 12:13 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Mon Mar 07, 2011 5:25 am
Posts: 27
You not use "getcwd()".

Joomla has path constant.

JPATH_ADMINISTRATOR The path to the administrator folder.
JPATH_BASE The path to the installed Joomla! site.
JPATH_CACHE The path to the cache folder.
JPATH_COMPONENT The path to the current component being executed.
JPATH_COMPONENT_ADMINISTRATOR The path to the administration folder of the current component being executed.
JPATH_COMPONENT_SITE The path to the site folder of the current component being executed.
JPATH_CONFIGURATION The path to folder containing the configuration.php file.
JPATH_INSTALLATION The path to the installation folder.
JPATH_LIBRARIES The path to the libraries folder.
JPATH_PLUGINS The path to the plugins folder.
JPATH_ROOT The path to the installed Joomla! site.
JPATH_SITE The path to the installed Joomla! site.
JPATH_THEMES The path to the templates folder.
JPATH_XMLRPC The path to the XML-RPC Web service folder.(1.5 only)


if your write component in your backend use.

JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');


Top
 Profile  
 
PostPosted: Mon Feb 20, 2012 8:20 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Feb 17, 2012 2:07 am
Posts: 4
I had those tried everything that had made sense to me already, but I still get

"PHP Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\Joomla17\components\com_content\views\article\view.html.php"

Maybe this helps:
I am programming this from the administrator.

My installation is under:
"C:\xampp\htdocs\Joomla17"

"JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables')" returns:
"C:\xampp\htdocs\Joomla17/administrator/components/com_content\tables"
(seems weird that back and forward slashes are mixed like that)

It occured to me that maybe the Table object had not been created at the time the script is executed.


Top
 Profile  
 
 Post subject: Workaround!
PostPosted: Mon Feb 20, 2012 2:32 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Fri Feb 17, 2012 2:07 am
Posts: 4
Up until now I've written the script right into the article.

Now I tried writing the exact same code into a seperate Module. I loaded it in the article via loadposition.

It works perfectly right away!

This can work for me, but why doesn't it work the other way? Is there way of calling the JTable from the article itself?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 



Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group