I need to use nested foreach for dependent checkboxes.
<input type="checkbox" name="all[]" value="<?php echo $row_bus_details['busid'];?>" >
<?php
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
?>
<input type="checkbox" name="bookingside[]" value="<?php echo $book_side_row['advt_side_id']; ?>" id="<?php echo $book_side_row['advt_side']; ?><?php echo $row_bus_details['busid'];?>" > <?php echo $book_side_row['advt_side']; ?><br/>
<?php } ?>
I need to loop the selected values of second checkbox if the first checkbox is selected.
I wrote the code like
$i = 0;
$busid = isset($_POST['all']) ? $_POST['all'] : array();
foreach ((array)$busid as $item) {
if(!empty($_POST['bookingside'])) {
foreach($_POST['bookingside'] as $side) {
$sql_book_side=mysqli_query($db,"INSERT INTO `advt_book_side`(bus_id,sides_id) VALUES ('$item','$side')");
$i++;
}
}
}
The result I need is just like the image below
You need to save data in serialize array from in data base like:
$sql_book_side=mysqli_query($db,"INSERT INTO advt_book_side(bus_id,sides_id) VALUES ('$item',serialize(array('left'=>1,'right'=>1,'back'=>0)))");
Print check box with check uncheck using below code
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
$array = unserialize($book_side_row['sides_id']);
foreach($array[0] as $side){
?>
<input type="checkbox" name="bookingside[]" value="<?php echo ($side)? $side:0; ?>">
<?php }
} ?>
Related
Currently I have multiple checkboxes that the user can check or uncheck. The checked values should make a new entry in the database and the unchecked values that were checked before should be deleted from the database. As of now my code displays as checked for all the entries that are currently there in my database, but if I check new values and submit, those values dont get added to my database and same with deletion. This is my current code:
<h4 class="text-uppercase w-100 px-4">Featured Amenities</h4>
<?php $checked = array();
foreach($checked_facility as $key => $value){
$checked[]=$value['facilities_id'];
}
if($facilities) foreach($facilities as $facility): ?>
<fieldset id="availablenetworked">
<input type="checkbox" <?php echo in_array($facility['id'], $checked) ? 'checked' : ''; ?> name="facility[]" id="<?php echo $facility['id'] ?>" value="<?php echo $facility['id'] ?>">
<label for="<?php echo $facility['id'] ?>"><span id="<?php echo $facility['id'] ?>-am-desc"><?php echo $facility['title'] ?></span></label>
</fieldset>
<?php endforeach; ?>
Controller Class:
if ($this->security->xss_clean($this->input->post('facility')) != '') {
$facilityPost = $this->security->xss_clean($this->input->post('facility'));
$facilityArray = array();
$c = 0;
foreach($facilityPost as $fp){
if ($fp != ''){
$facilityArray[$c]['listings_id']= $id;
$facilityArray[$c]['facilities_id']= $fp;
$c++;
}
}
if(count($facilityArray) > 0){
$this->listings_model->save_facility($facilityArray);
}
}
Model class:
function save_facility($maindata){
$this->db->insert_batch("crm_property_facilities",$maindata);
return $this->db->insert_id();
}
Now I did add a var_dump($this->input->post('facility')); die(); after the submit to check the data and it return NULL. So not quiet sure where I'm going wrong here.
The foreach loop at the end with $_POST['clients'] only returns one value. Whats wrong?
<?php $clients_to_display = Client::find_all(); ?>
<p><?php foreach ($clients_to_display as $key) {
echo $key->name; ?>:<input type='checkbox' name='clients[]' value=<?php $key->name; ?></><br/>
<?php } ?></p>
if(isset($_POST['submit'])){
$job->name = $_POST["job_name"];
$job->description = $_POST["job_description"];
$job->type = $_POST["job_type"];
$job->age = $_POST["job_age"];
foreach ($_POST['clients'] as $key) {
echo $key;
}
}
This code does nothing:
<?php $key->name; ?>
I think you want:
<?php echo($key->name); ?>
Also, it has to be enclosed in quotes:
value="<?php echo($key->name); ?>"
Also the markup is invalid. So, the full line should be:
echo($key->name); ?>:<input type="checkbox" name="clients[]" value="<?php echo($key->name); ?>" /><br/>
I use foreach to show data came from database.
This is for Plan_data
<?php foreach ($veddingPlanData as $row) { ?>
<input box with value containing "echo row->value_name" >
<?php } ?>
This is for task data as check box [here I want to show the checkbox list in which the task is given to plan_id they appear as cheeked checkbox and remaining list with not checked status)
<?php foreach ($veddingPlanTaskMappingData as $row) { ?>
<input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" checked><?php echo $row->task_name?><br>
<?php } ?>
Here I show the whole task list in check box.
<?php foreach ($allVedingTasks as $row) { ?>
<input type="checkbox" name="task_id" value="<?php echo $row->task_id ;?>" ><?php echo $row->task_name?><br>
<?php } ?>
I want to foreach the task_name list with selected some task as there mapped plan_id.
Finally I found It
<?php
$arrSelVedTask = array();
foreach ($veddingPlanTaskMappingData as $row) {
$arrSelVedTask[$row->task_id] = '';
}
?>
<div class="form-group">
<lable for="task_id" class="control-label col-sm-12">Plan Task LIST:</lable>
<div class="col-sm-10">
<div class="checkbox" style="margin-left:40px;">
<?php foreach ($allVedingTasks as $row) {
if(isset($arrSelVedTask[$row->task_id])) {
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" checked ><?php echo $row->task_name; ?><br><?php
}
else{
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" ><?php echo $row->task_name; ?><br><?php
}
}
?>
Simply add a check:
<?php
foreach ($veddingPlanTaskMappingData as $row) {
$checked = ($row->task_id == YOUR_CONDITION) ? 'checked="checked"' : '';
?>
<input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" <?php echo $checked?>><?php echo $row->task_name?><br>
<?php } ?>
Use set_checkbox Of CI
<input type="checkbox" name="task_id" value="<?php echo $row->task_id ;?>" <?php echo set_checkbox('task_id', $row->task_id); ?> ><?php echo $row->task_name?>
at first in your model fetch the value of checkbox which is in your database then call that function in controller and pass the value to view page. then use if statement with every checkbox item to check whether the value is same or not. and write echo "checked";command if the condition match.
First of all we will take all the categories than we are taking an Id and using it for showing the checked list from the database. First we select the table values based on Id than we will fetch the record. We are having the data in the database column using , comma like 1,2,3,5,9. Than we save it in array and at last we just show that array to the page.
$table="select * from categorytable";
$this->result=mysqli_query($this->con,$table);
$this->count=mysqli_num_rows($this->result);
if($this->count < 1)
{
return "<div class='form-group'>".$messages->getResponseMessage("No category is added.", "Warning")."</div>";
}
else
{
$alert='';
$select="select Category from table where Id='$id'";
$result= mysqli_query($this->con, $select);
$amrow= mysqli_fetch_array($result);
$getAminity=$amrow["Category"];
$getArray= explode(",",$getAminity);
while($row= mysqli_fetch_array($this->result))
{
if(in_array($row[Id],$getArray)){
$alert.="<div class=col-md-2><label class='checkbox-inline'><input type=checkbox id='catval[]' checked name='catval[]' value='$row[Id]'/> $row[Name]</label></div>";
}
else
{
$alert.="<div class=col-md-2><label class='checkbox-inline'><input type=checkbox id='catval[]' name='catval[]' value='$row[Id]'/> $row[Name]</label></div>";
}
}
I am having no luck trying to compare values in an array passed as a GET to values in a database recordset do/while loop. I'm attempting to make a checkbox checked if any value in the GET array matches a recordset id. It works if you use only one interest id in the URL without a comma. Thank you.
My URLs are:
interestSearch.php?interests=3 (this works)
interestSearch.php?interests=3,8 (doesn't work)
<? require('db.php');
mysql_select_db($database_data);
$query_allInterests = "SELECT * FROM interests";
$allInterests = mysql_query($query_allInterests, $data) or die(mysql_error());
$row_allInterests = mysql_fetch_assoc($allInterests);
$totalRows_allInterests = mysql_num_rows($allInterests);
?>
<form method="get">
<? do { ?>
<input <?
if(isset($_GET['interests']) && $_GET['interests'] != "") {
$theCounter = 0;
$theArray = array($_GET['interests']);
foreach ($theArray as $value) {
// if ($value == $row_allInterests['id']) {$theCounter++;}
if (in_array($row_allInterests['id'], $theArray)) {$theCounter++;}
}
if($theCounter > 0){echo "checked";}
}
?> name="<? echo $row_allInterests['id']; ?>" class="doCheck" type="checkbox" id="<? echo $row_allInterests['id']; ?>" value="<? echo $row_allInterests['id']; ?>" /> <? echo $row_allInterests['interest']; ?><br />
<? } while ($row_allInterests = mysql_fetch_assoc($allInterests)); ?>
</form>
Instead of
$theArray = array($_GET['interests']);
Use
$theArray = explode( ',', $_GET['interests']);
I've problem with multiple form at one page. At page index I include 4 forms
include('./poll_1a.php');
include('./poll_2a.php');
include('./poll_3a.php');
include('./poll_4a.php');
The form code at every poll page is the same. I include some unique markers ($poll_code) for every scripts but the effect is when I use one form - the sending variable are received in the others. But I would like to work each form individually.
The variable $poll_code is unique for every script -> 1 for poll_1, 2 for poll_2 etc.
The same situation is with $cookie_name
$cookie_name = "poll_cookie_".$poll_code;
than, as I see, cookies have different names.
$poll_code = "1"; // or 2, 3, 4
?>
<p>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" name="<?php echo $poll_code; ?>">
<input type="hidden" name="poll_cookie_<?php echo $poll_code; ?>" value="<?php echo $poll_code; ?>">
<table>
<?php
//print possible answers
for($i=0;$i<count($answers);$i++){
?><tr><td style="\text-allign: left;\"><input type="radio" name="vote_<?php echo $poll_code; ?>" value="<?php echo $i; ?>"> <?php echo $answers[$i]; ?></td></tr><?php
}
echo "</table>";
echo "<br>";
if ($_COOKIE["$cookie_name"] == $poll_code ) {
echo "<br> nie można głosować ponownie ...";
} else {
?>
<p><input type="submit" name="submit_<?php echo $poll_code; ?>" value="głosuj !" onClick="this.disabled = 'true';"></p>
<?php
}
?>
</form>
</p>
Q: how to make this forms to work individually at one page?
//------------------- EDIT
the receiving part of the script
$votes = file($file);
$total = 0;
$totale = 0;
$poll_cookie = 0;
if (isset($_POST["vote_$poll_code"]) && isset($_POST["poll_cookie_$poll_code"])) {
$vote = $_POST["vote_$poll_code"];
$poll_cookie = $_POST["poll_cookie_$poll_code"];
}
//submit vote
if(isset($vote)){
$votes[$vote] = $votes[$vote]+1;
}
//write votes
$handle = fopen($file,"w");
foreach($votes as $v){
$total += $v;
fputs($handle,chop($v)."\n");
}
fclose($handle);
Of course, the $file have the unique declaration too (at top of the script, under the $poll_code declaration).
$file = "poll_".$poll_code.".txt";
I think the issue might be that <?php echo $poll_code; ?> is outside the loop so maybe that it's always using the same value assigned to it, maybe put it inside the loop