English 中文(简体)
形成从多个检查箱铺设的询问
原标题:Forming a query string from multiple checkboxes

我试图从多个检查箱中形成一个插座,用于查询我的数据库。

我有以下表格:

            <fieldset data-role="controlgroup">

            <input type="checkbox" name="wheat" id="checkbox-1a" class="custom" />
            <label for="checkbox-1a">Wheat Allergy</label>

            <input type="checkbox" name="yeast" id="checkbox-2a" class="custom" />
            <label for="checkbox-2a">Yeast Allergy</label>

            <input type="checkbox" name="sugar" id="checkbox-3a" class="custom" />
            <label for="checkbox-3a">Sugar Allergy</label>

            <input type="checkbox" name="dairy" id="checkbox-4a" class="custom" />
            <label for="checkbox-4a">Dairy Allergy</label>

我的购买力平价编码如下:

        if(isset($_POST[ wheat ]))
        {
            $str1 =  wheatfree = 1 ;
        }

        if(isset($_POST[ yeast ]))
        {
            $str2 =  yeastfree = 1 ;
        }

        if(isset($_POST[ sugar ]))
        {
            $str3 =  sugarfree = 1 ;
        }

        if(isset($_POST[ dairy ]))
        {
            $str4 =  dairyfree = 1 ;
        }

        $fullsearch = $str1.$str2.$str3.$str4;

        $str_SQL = "SELECT * FROM recipes WHERE ".$fullsearch;

        echo $str_SQL;

This is sort of doing what I require, but it s not very graceful.

单单单单单单单是这样:

SlectT * fromrecipes WHERE IXfree = 1dairyfree = 1

并且如果用户选择不选择一个I,则选择斜体时会出现不确定的变差。

不能真正确定如何解决这一问题或下一步工作。 我在此赞同一些逻辑,即根据当时表格的核实,刚刚修改了扼杀,我可以与我的行竞争。 但一米损失:

帮助?

最佳回答

如何做到这一点:

$options = array();
if(isset($_POST[ wheat ]))
{
    $options[] =  wheatfree = 1 ;
}

if(isset($_POST[ yeast ]))
{
    $options[] =  yeastfree = 1 ;
}

if(isset($_POST[ sugar ]))
{
    $options[] =  sugarfree = 1 ;
}

if(isset($_POST[ dairy ]))
{
    $options[] =  dairyfree = 1 ;
}

$fullsearch = implode(  AND  , $options);

$str_SQL = "SELECT * FROM recipes";
if ($fullsearch <>   ) {
    $str_SQL .= " WHERE " . $fullsearch;
}

echo $str_SQL;
问题回答

Further to Dave s answer:

$options = Array();
$ingredients = Array( wheat ,  yeast ,  sugar ,  dairy );

foreach ($ingredients as $i)
   if (isset($_POST[$i]))
      $options[] = $i .  free = 1 ;

$sql = "SELECT * FROM recipes";
if (count($options))
   $sql .= " WHERE " . implode(  AND  , $options);

echo $sql;

但是,为什么有人用<条码>价值使用检查箱的财产?

<input type="checkbox" name="ingredients[]" value="wheat" />
<input type="checkbox" name="ingredients[]" value="sugar" />

等等。

然后:

$options = Array();
foreach ($_POST[ ingredients ] as $i)
   $options[] = $i .  free = 1 ; // don t forget to escape $i somehow!

$sql = "SELECT * FROM recipes";
if (count($options))
   $sql .= " WHERE " . implode(  AND  , $options);

echo $sql;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签