Mysql SELECT根据表单输入多个并发布

时间:2019-02-09 00:23:47

标签: javascript jquery mysql joomla breezingforms

我需要完成mysql_query SELECT多个选项并在网页上发布结果。表单(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

现在,我需要:从表等中选择SELECT值(根据用户在表单上的选择。每个表单元素都是一个选择列表)。因此,在我的示例中,结果应该是这样的:

Company Name        Company Address 
Apple               someaddress2    
Ollivetti           someaddress3    

1 个答案:

答案 0 :(得分:0)

我将假设您正在运行Joomla的过时版本,因为JRequest has been deprecated as of Joomla 3.xJoomla 3.3 deprecated function for 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) {

您的查询将需要使用“数据透视表”对关联的行进行分组; here is a solution that I posted on Joomla Stack Exchange that implements a pivot

SQL搜索BusinessExecutive:(db-fiddle demo

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