MySql 根据表单输入选择多个并发布



我需要完成mysql_query选择多个选项并在网页上发布结果。表单(Breezingforms(拉取数据。

Joomla模块将出现在网页上

<div id="frmSrchResults"></div>

表单上的"搜索"按钮,用户可选择从数据库中提取数据

    function ff_Searchbutton_action(element, action)
{
    switch (action) {
        case 'click':
let var1 = ff_getElementByName('category').value;
let var2 = ff_getElementByName('subcategory').value;
let var3 = ff_getElementByName('CselCountry').value;
// call the .post
jQuery.ajaxSetup({async:false});
jQuery.post(
  '<?php return JUri::root(true); ?>/index.php', {
    option: 'com_breezingforms',
    ff_form: ff_processor.form,
    format: 'html',
    category: var1,
    subcategory: var2,
    country: var3
},
//  success: function(data) {
  function(data) {
    jQuery('#frmSrchResults').html(data);
);
            break;
        default:;
    } // switch
} // ff_Searchbutton_action

在表单片段中 - 在表单之前

$this->execPieceByName('ff_InitLib');
// fetch .post() parameters
$var1 = JRequest::getVar('par1');
$var2 = JRequest::getVar('par2');
if ($var1 && $var2 && $var1 !== '' && $var2 !== '') {
  $db = JFactory::getDBO();
  $db->setQuery("Query I need to complete");
  $result = $db->loadAssocList();
// clean output buffer
  while (@ob_get_level() > 0) { @ob_end_clean(); }
  echo $result;
  exit;
}

这是数据库结构的一个示例

id  title            name           value
4   Company Name     companyname    Microsoft
4   Company Address  companyaddress someaddress
4   Country          country        USA
4   Category         category       Computer
4   Sub-category     subcategory    Software
5   Company Name     companyname    Apple
5   Company Address  companyaddress someaddress2
5   Country          country        CANADA
5   Category         category       Business
5   Sub-category     subcategory    Executive
6   Company Name     companyname    Ollivetti
6   Company Address  companyaddress someaddress3
6   Country          country        CANADA
6   Category         category       Business
6   Sub-category     subcategory    Executive

例如,用户输入的表格:

Category=Business
Sub-category=Executive
Country=CANADA

现在我需要:选择值(根据用户在表单上的选择。每个表单元素都是一个选择列表(FROM 表等。 所以在我的例子中,结果应该是这样的:

Company Name        Company Address 
Apple               someaddress2    
Ollivetti           someaddress3    

我假设您正在运行过时的Joomla版本,因为JRequest在Joomla 3.x和Joomla 3.3中已被弃用,JRequest::getVar((因此,您应该尽快升级。

现代语法:

$jinput = JFactory::getApplication()->input;
$category = $jinput->get->post('par1', '', 'WORD');
$subcategory = $jinput->get->post('par2', '', 'WORD');
$country = $jinput->get->post('par3', '', 'WORD');

然后你可以像这样编写你的条件:

if ($category && $subcategory && $country) {

您的查询需要使用"透视"对关联的行进行分组;这是我在Joomla Stack Exchange上发布的一个解决方案,它实现了透视。

SQL搜索BusinessExecutive:(db-fiddle演示(

SELECT 
    MAX(CASE WHEN `name` = 'companyname' THEN `value` ELSE NULL END) AS `Company Name`,
    MAX(CASE WHEN `name` = 'companyaddress' THEN `value` ELSE NULL END) AS `Company Address`
FROM `ucm`
GROUP BY `id`
HAVING
    MAX(CASE WHEN `name` = 'category' THEN `value` ELSE NULL END) = 'Business'
    AND MAX(CASE WHEN `name` = 'subcategory' THEN `value` ELSE NULL END) = 'Executive'
    AND MAX(CASE WHEN `name` = 'country' THEN `value` ELSE NULL END) = 'CANADA'
ORDER BY `Company Name`;

使用输入变量将此原始SQL转换为Joomla方法语法,它看起来像这样:

$db = JFactory::getDbo();
$query = $db->getQuery(true)
    ->select([
        "MAX("
        . "CASE WHEN name = " . $db->q("companyname")
        . " THEN value ELSE NULL END"
        . ") AS " . $db->qn("Company Name"),
        "MAX("
        . "CASE WHEN name = " . $db->q("companyaddress")
        . " THEN value ELSE NULL END"
        . ") AS " . $db->qn("Company Address")
    ])
    ->from($db->qn("#__your_ucm_table"))
    ->group("id")
    ->having([
        "MAX("
        . "CASE WHEN name = " . $db->q("category")
        . " THEN value ELSE NULL END"
        . ") = " . $db->q($category),
        "MAX("
        . "CASE WHEN name = " . $db->q("subcategory")
        . " THEN value ELSE NULL END"
        . ") = " . $db->q($subcategory),
        "MAX("
        . "CASE WHEN name = " . $db->q("country")
        . " THEN value ELSE NULL END"
        . ") = " . $db->q($country)
    ])
    ->order($db->qn("Company Name"));
try
{
    $db->setQuery($query);
    if (!$results = $db->loadAssocList())
    {
        echo "No matches found";
    }
    else
    {
        echo "<table>";
            echo "<tr><th>", implode("</th><th>", array_keys($results[0])), "</th></tr>";
            foreach ($results as $row)
            {
                echo "<tr><td>", implode("</td><td>", $row), "</td></tr>";
            }
        echo "</table>";
    }
}
catch (Exception $e)
{
    JFactory::getApplication()->enqueueMessage("<div>Query Syntax Error, ask dev to run diagnostics</div>", 'error');
    // Don't show the following details to the public:
    //echo $query->dump();
    //echo $e->getMessage();
}

p.s. 请记住,您不能简单地echo您的loadAssocList数据。


至于你的jquery,我相信你错过了调用的success块。

success: function (data) {
    jQuery('#frmSrchResults').html(data);
},
error: function (xhr, status) {
    console.log("Bonk! Time to debug.");
}

以下是一些上下文: https://stackoverflow.com/a/20008285/2943403

相关内容

  • 没有找到相关文章

最新更新