【问题标题】:Chosen select, how to get the multiple values into a $bind array选择选择,如何将多个值放入 $bind 数组
【发布时间】:2016-07-08 08:10:08
【问题描述】:

我正在使用带有扩展 Pdo 的 aura sql。 我有一个带有过滤器的表单,您可以在其中使用所选 jquery 选择多个选项,因为您可以看到我从数据库中填充选项。我不知道如何在提交后从表单中获取多个值,因为我使用 $bind 和 $pdo。 我想为我选择的每个选项添加一个“and pr.idproduttori = :produttore”。对不起我的英语不好:D

模板:

<select name="filter[produttore][]"  multiple class="chosen-select form-element" data-placeholder="Seleziona Produttore/i">
    <option value="">Tutti</option>
    <?php foreach ($produttori as $row): ?> 
                    <option value="<?= $row['id'] ?>" <?= @$filter['produttore'] == $row['id'] ? 'selected' : '' ?>><?= $row['prodname'] ?></option>
    <?php endforeach ?> 
</select>

控制器:

if(!empty($filter['produttore'])){
    foreach($filter['produttore'] as $row){
        $q_where .= ' and pr.idproduttori = :produttore';
        $bind['produttore'] = $row;
    }
}

if(!empty($filter['ean'])){
        $q_where .= ' and p.ean_code = :ean';
        $bind['ean'] = $filter['ean'];
}

// Query predefinita
$q = 'select p.idprodotti as id, pr.nome as produttore, p.modello, p.ean_code as ean, t.descrizione as tipo, count(o.idoggetti) as quanto
    from prodotti p join produttori pr on p.idproduttori = pr.idproduttori
    join tipologia t on p.idtipologia = t.idtipologia
    left join oggetti o on p.idprodotti = o.idprodotti';

$q_attribs = ' group by p.idprodotti order by '.$filter['orderby'].' '.$filter['verso'];

$data = $pdo->fetchAll($q.$q_where.$q_attribs, $bind);

【问题讨论】:

    标签: jquery mysql arrays pdo bind


    【解决方案1】:

    我不确定,但我知道你的意思是这样的:

    if(!empty($filter['produttore'])){
        $q_where .= ' and (';
        foreach($filter['produttore'] as $row){
            $q_where .= 'pr.idproduttori = :produttore or ';
            $bind['produttore'] = $row;
        }
        $q_where = substr($q_where,0,-4).')';
    }
    

    【讨论】:

    • $bind['produttore'] 有问题,因为如果我执行 var_dump($bind['produttore']);它说只有第二个值我选择作为过滤器进入 $bind['produttore'],我需要 $bind['produttore'] 是一个数组
    • 当你写$bind['produttore'] 覆盖一个可能存在的值是正常的。你不是说$bind['produttore'][]吗?
    • $bind['produttore'][] 不起作用,因为您每次都必须更改字符串中的:produttore
    【解决方案2】:

    现在可以了:

    if(!empty($filter['produttore'])){
        $q_where .= ' and (';
        $i = 'a';
        foreach($filter['produttore'] as $row){
            $q_where .= 'pr.idproduttori = :'.$i.' or ';
            $bind[$i] = $row;
            $i++;
        }
        // d($bind[]);
        $q_where = substr($q_where,0,-4).')';
    }
    

    虽然不是很好看,但它确实有效

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 2020-03-02
      • 2014-09-27
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多