Reputation: 1793
I have multiple checkboxes so i created a 2D array to store those inputs.
I should make a query with all this information, but i can't figure out how to handle this 2D array in order to get the data from the Data Base. Conditions for the query can be variable as the parameters are variable, so i am really confused..
How should i handle the checkbox data to send it all together to the query?
this is the view:
<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>
and this is my 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*******
}
}
Upvotes: 0
Views: 1172
Reputation: 13511
I hope I have understood your question, and what you have to do is the following.
First of all in my local code I have modified the values of your checkboxes and I have the following code:
<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>
First of all If I check all the checkboes, and then submit the form, the $_POST variable contains the following code:
Array
(
[type] => Array
(
[0] => registrado
[1] => pre_registrado
)
[act] => Array
(
[0] => activo
[1] => no_activo
)
)
If I submit the form with no checkboxes checked the result is the following:
Array
(
)
So the best solution is to test if the values are into array. For example:
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.
}
}
Upvotes: 1
Reputation: 1793
this is how i handled it:
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);
}
}
so what i did was to create a 1D array directly with the conditions, so i can later on make queries without having to do any other logic more than just adding conditions and comparing to the DB
Upvotes: 0
Reputation: 13354
It's impossible to know how you want to pass the data into MySQL without seeing your table structure, but you can access the data passed by looping through an array:
<?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 );
}
Repeat for each checkbox array (type
, act
, etc. in your example)
Upvotes: 1