问题
I'm using Joomla 1.5. I have created a custom component that pulls data out of the database perfectly. I initialize the database in my main component file like this:
$db =& JFactory::getDBO();
The problem is that I have a jQuery autocomplete plugin on my component page that makes calls to a PHP file from my component folder. That PHP file searches the database for matching results but I can't do the query because I can't call $db =& JFactory::getDBO();
because this is a custom PHP page and I can't access these functions without any references.
Can anybody help me to call Joomla functions in my custom file ?
Thanks.
回答1:
I would recommend not calling a separate PHP file and instead using the raw
view format in your component. To do this, create a view in your component for the data that jQuery will be pulling. For discussion, we'll call this view autocomplete
. Set up the autocomplete
view as you would any other, only using view.raw.php
in place of view.html.php
. Then in your JavaScript, call index.php?option=com_yourcomponent&view=autocomplete&format=raw
. This HTTP call will ONLY return what you output in your view.
If you absolutely must run an external PHP file, take a look at creating a stand-alone application using the Joomla! Framework: http://docs.joomla.org/How_to_create_a_stand-alone_application_using_the_Joomla%21_Framework This is enough code to pull in JFactory
and the database connection. But only use this if you really need to: otherwise, you are just creating another entry point into the Joomla! application that you will have to maintain and secure.
You may find Louis Landry's post on doing JSON helpful: http://groups.google.com/group/joomla-dev-cms/browse_thread/thread/5ac0b49c0f458b1a
I also blogged about this topic recently: http://www.designvsdevelop.com/the-way-not-to-do-javascript-in-joomla/
回答2:
I'd use the JLoader class to include external PHP files and classes to joomla.
JLoader::register( 'classname', dirname(__FILE__).DS.'classfoldername'.DS.'classfile.php');
This would include the class and make it ready for use:
$theClass = new classname();
来源:https://stackoverflow.com/questions/1249085/joomla-include-database-functions