简体   繁体   English

试图找到在Joomla 2.5组件中提取数据库项的最佳方法

[英]Trying to Find the Best way to pull DB item in Joomla 2.5 Component

I have a database which has 2 columns id and title. 我有一个具有2列idtitle.的数据库title. What is the best way for me to filter through these table values to display the matching title results when a user submits the id value in a form for a Joomla 2.5 component (admin view)? 当用户以Joomla 2.5组件的形式(管理视图)提交id值时,对这些表值进行过滤以显示匹配title结果的最佳方法是什么?

When I look at the documentation here: 当我在这里查看文档时:

http://docs.joomla.org/Selecting_data_using_JDatabase

I see that by using Joomla's API I can use a shorthand to create this connection. 我看到通过使用Joomla的API,我可以使用简写方式来创建此连接。 I have tried: 我努力了:

$userSubmittedIDValue = $_GET["userSubmittedIDValue"];

// Get a db connection.
$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

$query
->select($db->quoteName(array('title')))
->from($db->quoteName('#__mycomponent_table'))
->where($db->quoteName('id') . ' = '. $db->quote('$userSubmittedIDValue'))

$results = $db->loadObjectList();

Unfortunately, this gives the following error: 不幸的是,这给出了以下错误:

Parse error: syntax error, unexpected T_VARIABLE

Then the error points to $results = $db->loadObjectList(); 然后错误指向$results = $db->loadObjectList();

I just need to pull that value and apply it to a php variable so that I can use it as required. 我只需要提取该值并将其应用于php变量,即可按需使用它。 Any ideas? 有任何想法吗?


Okay thanks to the first answer I have realized that I was missing a semi-colon. 好吧,感谢第一个答案,我意识到我缺少分号。 The trouble I am having now is still in pulling the value from the array using foreach . 我现在遇到的麻烦仍然是使用foreach从数组中提取值。 I have tried adding the following: 我尝试添加以下内容:

foreach (array($results) as $userSubmittedIDValue) {

echo $results;

}

But this just prints the word Array . 但这只是打印单词Array I am getting close, but something is still off. 我越来越近了,但是仍然有事。

You're getting that error due to the fact you have a missing semi colon after your query. 由于出现查询后缺少分号的事实,导致出现此错误。

So, change this: 因此,更改此:

->where($db->quoteName('id') . ' = '. $db->quote('$userSubmittedIDValue'))

to this: 对此:

->where($db->quoteName('id') . ' = '. $db->quote('$userSubmittedIDValue'));

Simply note the ; 只需注意; on the end 最后

Once done, you can use a foreach loops to display your results. 完成后,您可以使用foreach循环显示结果。

So your final code to use would be this: 因此,您要使用的最终代码是:

$db = JFactory::getDBO();
$query = $db->getQuery(true);

$query->select('*')
->from('#__mycomponent_table')
->where($db->quoteName('id') . ' = '. $db->quote($userSubmittedIDValue));

$db->setQuery($query);    
$rows = $db->loadObjectList();

foreach ($rows as $row) {
    echo $row->title;
}

I completely forgot to mention before that you also have to remove the single quote wrapped around $userSubmittedIDValue in the query as shown in my code above. 我之前完全忘记提及,您还必须删除查询中$userSubmittedIDValue周围的单引号,如上面的代码所示。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM