Looping through values in_array and checking multiple checkboxes using PHP - php

I have a series of Grades that are contained within a loop. I want to check if a database-driven (looped) checkbox's value matches with any of the values in the loop. If it does match a given item, then mark that input item as 'checked.'
For example, print_r($grades_array) outputs the following data:
Array ( [0] => Grade 5 [1] => Grade 8 [2] => Grade 9 )
And here's my loop through each of the input items (I'm using Laravel and Semantic UI, but the PHP concepts should still be the same):
#foreach($list_of_grades as $output_grade)
<div class="inline field">
<div class="ui checkbox">
<input type="checkbox" name="grade"
#if(in_array($output_grade->name, $grade_array))
checked="checked"
#endif
tabindex="0" class="hidden">
<label>{{ $output_grade->name }}</label>
</div>
</div>
#endforeach
Here's what I am getting as output:
However, this is what I would expect to get as output:
Any ideas as to what's wrong with the code?
And, just to clarify before it's asked, yes, the $output_grade->name values and the values within the array match. I can swap for ID as well and it produces the same single checked value result.

I face same case, Use just a trick if you find better solution please knock me
#php
$arrayValue = explode(',', $grade_array);
#endphp
#foreach($arrayValue as $data)
#php $arrayData[$data] = $data #endphp
#endforeach
#foreach($list_of_grades as $output_grade)
<div class="inline field">
<div class="ui checkbox">
<input type="checkbox" name="grade"
#if(in_array($output_grade->name, $arrayData))
checked="checked"
#endif
tabindex="0" class="hidden">
<label>{{ $output_grade->name }}</label>
</div>
</div>
#endforeach
But it working for me tested
here screenshot

Related

How to mark as checked the checkbox if the ids is match or exist

I have module where I need checked the list checkbox if the ids determine that i have same ids to the another table. I have two table, Table 1 and Table 2
Table 1 - List of Items and all items loop in the checkbox
Table 2 - This table 2 are the stored ids where the user check the
items on table and submit. and those ids where inserted to the table
2.
So now. If the user try to open again the checkbox list items and if one of those items is already set make the checkbox automatically checked.
I have here my loop items function:
#foreach($details_items as $details_items_data)
<div class="col-md-3">
<div class="form-check">
<input type="checkbox" value="{{$details_items_data->adi_id}}" class="form-check-input csdtc_chkbox" id="exampleCheck1">
<label class="form-check-label" for="exampleCheck1">{{$details_items_data->items}}</label>
</div>
</div>
#endforeach
Now I have here another loop where this loop is table 2
#foreach($details_set_check as $details_checker)
#endforeach
Currently have the output of this.
Try this out:
#foreach($details_items as $details_items_data)
<div class="col-md-3">
<div class="form-check">
<input type="checkbox" value="{{$details_items_data->adi_id}}" class="form-check-input csdtc_chkbox" id="exampleCheck1" #foreach($details_set_check as $details_checker) {{($details_items_data->adi_id == $details_checker->id)? 'checked':''}} #endforeach>
<label class="form-check-label" for="exampleCheck1">{{$details_items_data->items}}</label>
</div>
</div>
#endforeach
You have to make if condition.
<input #if(value in the lopp exists in your table) checked #endif />

Save multiple checkbox - Laravel 5.4

I need to save multiple checkbox to single field in the database.
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="1">Raíz
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="3">tronco
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="expresion_vegetal_id[]" value="4">corteza
</label>
</div>
Controller:
$ficha_tecnica = new Ficha_Tecnica();
$options = $request->get('expresion_vegetal_id');
$ficha_tecnica->expresion_vegetal_id = $options;
$ficha_tecnica->save();
this is trying to save, the values ​​in [""], I need only save the numbers
insert into `fichas_tecnicas` (`expresion_vegetal_id`) values (["1","3","4"])
When I try to save, show the next message
1366 Incorrect integer value: '["1","4"]' for column 'expresion_vegetal_id'
You'r cannot add that because
You can try to loop the expresion_vegetal_id still in array format.
I see in your question is you need to add that in string format.
You must loop that array first and save one by one or you can used created
I give you the sample using loop.
You code will looks like this
$ficha_tecnica = new Ficha_Tecnica();
foreach ($$request->expresion_vegetal_id as $expresion_vegetal_id) {
$ficha_tecnica->fresh();
$ficha_tecnica->expresion_vegetal_id = $expresion_vegetal_id;
$ficha_tecnica->save();
}
i hope you can find the other same way....

Laravel set checkboxes checked based on database value

I am trying to set checkboxes checked based on database value. One user can have have multiple checkbox values.
Checkboxes are generated from database table called child_age_groups:
#foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" name="age_group[]" id="age_group{{$age->id}}" "/>
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
#endforeach
So in my controller I get all the options that user has like this
$nanny_babysitting_ages = Nanny_babysitting_ages::where('user_id', user()->id)->get();
My nanny_babysitting_ages table is this
user_id | ages
and my child_age_groups table where I populate the checkboxes are this:
id | age_group
How can I set the checkboxes selcted based on the values from database?
This is how I resolved it, I added foreach and if statement in the input to check if it the same value as from database:
<div class="form-group" id="ageCheckboxes">
#foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" value="{{$age->id}}" name="age_group[]" id="age_group{{$age->id}}" #foreach ($nanny_babysitting_ages as $ages) #if($age->id == $ages->ages ) checked #endif #endforeach />
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
#endforeach
</div>
you can use this "#checked(true)" like :-
<input class="custom-control-input army" type="checkbox" id="customCheckbox1" #checked($emp_plus->army != 0)>

PHP: POST paired check box/text field in form

I have a form that is a list of check boxes. Next to each check box is a text field that goes with the check box. Below is an image of what I am talking about
The text in red is the name attribute. My question is based off of whatever the user check marks, how do I match the paired text field without doing a bunch of if statements?
I know how to just post the check boxes and just post the text fields..separately. I guess I am stuck at how to pair the results. This is what I am using to $_POST the check boxes
if (isset($_POST['family_medical_history']))
{
$_SESSION['patient_form']['family_medical_history'] = array();
foreach ($_POST['family_medical_history'] as $key => $value)
{
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST['family_medical_history'][$key];
}
}
else
{
$_SESSION['patient_form']['family_medical_history'] = "none";
}
Example HTML form
<div class="form-group col-sm-4">
<label><input type="checkbox" name="family_medical_history[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes">
</div>
You can pair them by naming the inputs similarly for each checkbox/relationship pair to produce a two-member array for each item.
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]">
</div>
In the PHP, they will become pairs with the 0 element equal to the value of the checkbox input, and the 1 element equal to the text input. You can create an array of conditions and loop through each of the named inputs. You'll have to change your if statement to something else, for example the submit button:
$conditions = array("rel_blindness" => "Blindness", "rel_cataracts" => "Cataracts", "rel_crossed_eyes" => "Crossed Eyes", "rel_glaucoma" => "Glaucoma");
if (isset($_POST['submit'])){
foreach ($conditions as $key => $value){
if ($_POST[$key][0] == $value){//if the checkbox is checked
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST[$key][1];
}
}
}

I'm trying to check all the checkboxes on my edit view fetching the data from a table

I'm trying to check the checkboxes on my edit view fetching the data from a table so the problem is all the checkboxes come unchecked. heres the code:
i don't know if this helps but in the table the data stores in an array there's not pivot table so i'm really trying to do it without using or making a pivot tablet.
<div class="form-group" style="padding-left: 1.4%">
{!!Form::label('estudio[]', 'Estudios Realizados* : ')!!}<br>
#foreach ($estudio as $key => $value)
#if(in_array($key, $my_estudio))
<input class="lista-estudio" name="estudio[]" type="checkbox" value="{{$key}}" id="estudio[]" checked> {{$value}}
#else
<input class="lista-estudio" name="estudio[]" type="checkbox" value="{{$key}}" id="estudio[]">{{$value}}
#endif
#endforeach
</div>

Categories