php - 如何从 Joomla 中的组件菜单项获取 MySQL 查询结果并使用 foreach where 子句进行优化?

标签 php mysql joomla components where-clause

我有自定义组件 J 2.5。在此组件的菜单中,我也有 sql 字段:

<field
    name="title"
    type="sql"
    default="10"
    label="აირჩიეთ კატეგორია"
    multiple="multiple"
    query="SELECT id AS value, title FROM #__deals_categories"
/>

我从函数中获取数据并将其存储在 $category 数组中:

$dealsparam = JFactory::getApplication()->getParams();
$category = $dealsparam->get(title, 10);

$category 的输出是数组:

Array ( [0] => 20 [1] => 23 [2] => 33 [3] => 41 [4] => 49 )

这些是来自数据库的类别 ID。 另外,我有一个功能可以从这些类别中获取内容。当我有一个类别 id 时一切正常,但是当我有许多类别 id 时,例如:2、4、5 等,我无法从 mysql 获取数据。我尝试 foreach 一个 where 子句,但出现了一些我无法理解的错误。

在下文中,我试图 foreach 一个 where 子句并从许多不同的类别中获取数据:

if ($category) {
    print_r ($category);
    foreach ($category as $cat){
        $query->where('d.category_id = ' . (int) $cat);
        echo $cat.'<br>';
    }
}

最佳答案

您应该使用 IN 而不是 = 条件 -

$category = (array) $category;
jimport('joomla.utilities.arrayhelper');
JArrayHelper::toInteger($category);
if(!empty($category)){
    $query->where('d.category_id IN(' . implode(',', $category) . ')');
}

关于php - 如何从 Joomla 中的组件菜单项获取 MySQL 查询结果并使用 foreach where 子句进行优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41242944/

相关文章:

javascript - 导航移动下拉菜单,制作屏幕尺寸

mysql - MySQL 中输入计数最高的子查询

php - 检索模板 joomla 2.5 中的模块参数设置

joomla - 如何在 joomla 文章中插入 HTML/javascript/css 代码

video - Joomla YouTube下载按钮或Dropbox集成

php - QUickbooks PHP SDK 添加发票付款

php - LARAVEL 在 View 中获取 withErrors 的结果

PHP MySQL 存储距离计算器 - 邮政编码

通过 SSH 隧道查询 MYSQL 数据库的 MySQL-Python 代码

php - 使用 mysql 在 php 中制作问答游戏