问题描述
我使用的是 Joomla 1.5.我创建了一个自定义组件,可以完美地从数据库中提取数据.我在我的主要组件文件中初始化数据库,如下所示:
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();
问题是我的组件页面上有一个 jQuery 自动完成插件,它从我的组件文件夹调用 PHP 文件.该 PHP 文件在数据库中搜索匹配结果,但我无法进行查询,因为我无法调用 $db =&JFactory::getDBO();
因为这是一个自定义的 PHP 页面,我无法在没有任何引用的情况下访问这些函数.
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.
谁能帮我在我的自定义文件中调用 Joomla 函数?
Can anybody help me to call Joomla functions in my custom file ?
谢谢.
推荐答案
我建议不要调用单独的 PHP 文件,而是在您的组件中使用 raw
视图格式.为此,请在您的组件中为 jQuery 将提取的数据创建一个视图.为了便于讨论,我们将此视图称为 autocomplete
.像设置其他视图一样设置 autocomplete
视图,仅使用 view.raw.php
代替 view.html.php
.然后在您的 JavaScript 中,调用 index.php?option=com_yourcomponent&view=autocomplete&format=raw
.此 HTTP 调用只会返回您在视图中输出的内容.
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.
如果您绝对必须运行外部 PHP 文件,请查看使用 Joomla! 创建独立应用程序!框架:http://docs.joomla.org/How_to_create_a_stand-alone_application_using_the_Joomla%21_Framework足够的代码来拉入 JFactory
和数据库连接.但只有在您确实需要时才使用它:否则,您只是在创建另一个进入 Joomla! 的入口点!您必须维护和保护的应用程序.
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.
您可能会发现 Louis Landry 关于执行 JSON 的帖子很有帮助:http://groups.google.com/group/joomla-dev-cms/browse_thread/thread/5ac0b49c0f458b1a
You may find Louis Landry's post on doing JSON helpful: http://groups.google.com/group/joomla-dev-cms/browse_thread/thread/5ac0b49c0f458b1a
我最近也写了关于这个话题的博客:http://www.designvsdevelop.com/the-way-not-to-do-javascript-in-joomla/
I also blogged about this topic recently: http://www.designvsdevelop.com/the-way-not-to-do-javascript-in-joomla/
这篇关于Joomla 包含数据库功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!