0

我有多个复选框,所以我创建了一个二维数组来存储这些输入。

我应该用所有这些信息进行查询,但我不知道如何处理这个二维数组以便从数据库中获取数据。查询的条件可以是可变的,因为参数是可变的,所以我真的很困惑..

我应该如何处理复选框数据以将它们一起发送到查询?

这是视图:

<div class="control-group">
    <label class="control-label">Tipo de Socio</label>
    <div class="controls">
        <label class="checkbox line">
            <input type="checkbox" id="registered" value="2" name="type[]" /> Registrado
        </label>
        <label class="checkbox line">
            <input type="checkbox" value="1" name="type[]"/> Pre-Registrado
        </label>
    </div>
</div>
<div class="hide" id="content">
    <div class="control-group">
        <label class="control-label">Estado del Socio</label>
        <div class="controls">
            <label class="checkbox line">
                <input type="checkbox" value="1" name="act[]" checked /> Activo
            </label>
            <label class="checkbox line">
                <input type="checkbox" value="0" name="act[]" checked/> No Activo
            </label>
        </div>
    </div>
</div>

这是我的 admin.php:

public function formReport()
{
    // var_dump($_POST);die();
    $conditions    =    array();

    if(isset($_POST['type']))
    {
        ******should ask here about the fields that comes from the checkboxes and prepare data for the query*******
     }
}
4

3 回答 3

1

在不查看表结构的情况下,不可能知道如何将数据传递到 MySQL,但是您可以通过循环访问数组来访问传递的数据:

<?php

// Verify data was submitted and is an array
if ( isset( $_POST['type'] ) && is_array( $_POST['type'] ) ){

    $data_type = array();

    // Loop through all checkbox data submitted
    foreach ( $_POST['type'] as $key => $value ){
        // $key will contain numerical index, and 
        // $value will contain your HTML property value 
            // ("2" or "1" for `type`, "1" or "0" for `act` in your example)

        // Add the $value to a $data_type array
        $data_type[] = $value;
    }

    // Show Results:
    var_dump( $data_type );

    // If both checkboxes are selected, sample output will be:
    // array( 2, 1 );

    // If just the first checkbox:
    // array( 2 );

    // If just the second checkbox:
    // array( 1 );

}

对每个复选框数组重复(在您的示例中为 , 等typeact

于 2013-10-24T17:10:07.903 回答
1

我希望我已经理解了您的问题,您需要做的是以下内容。

首先,在我的本地代码中,我修改了复选框的值,并且我有以下代码:

<div class="control-group">
    <label class="control-label">Tipo de Socio</label>
    <div class="controls">
        <label class="checkbox line">
            <input type="checkbox" id="registered" value="registrado" name="type[]" /> Registrado
        </label>
        <label class="checkbox line">
            <input type="checkbox" value="pre_registrado" name="type[]"/> Pre-Registrado
        </label>
    </div>
</div>
<div class="hide" id="content">
    <div class="control-group">
        <label class="control-label">Estado del Socio</label>
        <div class="controls">
            <label class="checkbox line">
                <input type="checkbox" value="activo" name="act[]" checked /> Activo
            </label>
            <label class="checkbox line">
                <input type="checkbox" value="no_activo" name="act[]" checked/> No Activo
            </label>
        </div>
    </div>
</div>

首先,如果我检查所有复选框,然后提交表单,$_POST 变量包含以下代码:

Array
(
    [type] => Array
        (
            [0] => registrado
            [1] => pre_registrado
        )

    [act] => Array
        (
            [0] => activo
            [1] => no_activo
        )

)

如果我提交没有选中复选框的表单,结果如下:

Array
(
)

所以最好的解决方案是测试这些值是否在数组中。例如:

public function formReport()
{
    // var_dump($_POST);die();
    $conditions    =    array();

    // Do some validation first
    if(empty($_POST))
    {
        //    There is no check box selected
    }
    elseif(!isset($_POST['type'] || empty($_POST['type']))
    {
        //    User didn't checked any type
    }
    elseif(!isset($_POST['act'] || empty($_POST['act']))
    {
        //    User didn't checked any Estato
    }
    else
    {
        $registrado     = (isset($_POST['type']['registrado']) ? 1 : 0;
        $preRegistrado  = (isset($_POST['type']['pre_registrado']) ? 1 : 0;
        $activo         = (isset($_POST['act']['activo']) ? 1 : 0;
        $noActivo       = (isset($_POST['act']['no_activo']) ? 1 : 0;

        // In this state the variables above have the value 1, if the
        // end user has checked the appropriate checkbox, and the value 0
        // if the end user has not checked the appropriate checkbox

        // In this place now you can use the above variables to perform
        // any logical operation you like, and/or insert the values
        // in your database via a simple INSERT query.
    }
}
于 2013-10-24T17:20:22.967 回答
0

这就是我处理它的方式:

public function formReport(){
    $result = array();

    if(empty($_POST)){

        $result['message'] = "Ingrese Datos al Formulario"; 
        echo json_encode($result);

    }else{

$conditions = array();


        if(isset($_POST['type'])){
            //se ha seleccionado usuario reg o pre registrado o ambos
            if(isset($_POST['type'][0])){
                $conditions[] = ('u.status = 2');
            }
            if(isset($_POST['type'][1])){
                $conditions[] = ('u.status = 1');
            }

            //al seleccionar usuario registrado en la vista, se activan las casillas de activo y no activo:
            if(isset($_POST['act'])){
                if(isset($_POST['act'][0])){
                    $conditions[] = ('u.active = 1');
                }
                if(isset($_POST['act'][1])){
                    $conditions[] = ('u.active = 2');
                }
            }

            if(isset($_POST['gender'])){
                if(isset($_POST['gender'][0])){
                    $conditions[] = ('u.gender = m');
                }
                if(isset($_POST['gender'][1])){
                    $conditions[] = ('u.gender = f');
                }
            }   

    $this->data['queries'] = UserManager::getInstance()->customReport($conditions);

        }else{
            //no se seleccionó ningún tipo de usuario
            $result['message'] = "Seleccione algún tipo de Usuario para poder realizar el listado";
            echo json_encode($result);
        }


        $this->parser->parse('admin/tools/showReport.tpl',$this->data);
    }
}

所以我所做的是直接使用条件创建一个一维数组,因此我可以稍后进行查询,而无需执行任何其他逻辑,而不仅仅是添加条件并与数据库进行比较

于 2013-10-25T15:59:16.827 回答