将复选框数据传递给php,然后传递给查询

将复选框数据传递给php,然后传递给查询,php,arrays,checkbox,Php,Arrays,Checkbox,我有多个复选框,所以我创建了一个2D数组来存储这些输入 我应该用所有这些信息进行查询,但我不知道如何处理这个2D数组以便从数据库中获取数据。查询的条件可以是可变的,因为参数是可变的,所以我真的很困惑 如何处理复选框数据以将其全部发送到查询 这是一种观点: <div class="control-group"> <label class="control-label">Tipo de Socio</label> <div class="co

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

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

如何处理复选框数据以将其全部发送到查询

这是一种观点:

<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>

在不查看表结构的情况下,不可能知道要如何将数据传递到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 );

}

我希望我已经理解了您的问题,您需要做的是以下几点

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

<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>
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.
    }
}

我就是这样处理的:

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);
    }
}

所以我所做的就是直接用条件创建一个1D数组,这样我以后就可以进行查询,而不必做任何其他逻辑,只需添加条件并与DB进行比较。。。否则一切都只是猜测而已。。。事情不是这样的。你能提供一些标记吗?@Lix我不是假装让你给我写代码,只是让我稍微了解一下从2D数组处理复选框信息的逻辑,这样我就可以将它发送到query@MerianosNikos我刚刚用一些代码更新了这个想法,@jim是为了向你表明你在发布之前已经投入了研究工作来调查这个问题。thax感谢你出色的教学方法,它非常简单明了。我所做的就是这样,现在我有了解决问题的办法。在这些行中,有一件事:(isset($_POST['type']['registrado'])?1:0;将数组的子键命名为'registrado'(正如您在表单中输入的值),我认为正确的是(isset($_POST['type'][0])?1:0;:)你可以随意给它命名。但是描述性名称将在功能方面帮助你,当你将再次尝试维护代码时。当然,但我的意思是,我试图做你所做的事情,但出现了一个错误。如果我从表单中输入值,(“registrado”)它不会将其识别为我的数组子键,它只能使用索引访问,并使用当前代码更新您的问题,以便为您提供正确答案。
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);
    }
}