0

複数のチェックボックスがあるので、それらの入力を格納する 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>

これは私の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 に答える
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);
    }
}

私がしたことは、条件を使用して 1D 配列を直接作成することでした。そのため、条件を追加して DB と比較するだけでなく、他のロジックを実行する必要なく、後でクエリを作成できます。

于 2013-10-25T15:59:16.827 に答える