get checkbox values if it is unchecked set value 0 - php

I have created a form with array as attr name of input there are input and checkboxes in form I am trying to run single loop to get all form values now the problem comes with checkboxes if any checkbox in checked then I want to set the value as 0 if checkbox is unchecked
I did something like this but had to ran 2 loops which is not good approach I need a better approach to do that
//Defining All Available Checkboxes
$checkboxes = array(
'pre_payment_option' => 0,
'partial_payment' => 0,
'usb_card_use' => 0,
'unpaid_customer_rentals' => 0,
'new_rentals_aknowledge' => 0,
'additional_deposit' => 0,
'customer_profile_edit' => 0,
'customer_billing_edit' => 0,
'customer_schedule_move_out' => 0,
'reserve_limit' => 0
);
foreach($data['site_settings'] as $meta_key => $meta_value):
if(!in_array($meta_key, $checkboxes)) {
echo $meta_key . 'Value Added <br />' ;
}
// echo $meta_key . ' | ' . $meta_value . '<br />';
endforeach;
foreach ($checkboxes as $key => $checkboxe) {
if(isset($data['site_settings'][$key])) {
$meta_key = $data['site_settings'][$key];
} else {
$meta_key = 0;
}
}
Form
<input type="text" name="site_settings[move_outs_days]" />
<input type="checkbox" name="site_settings[customer_billing_edit]" />
...

Related

Check associative array key exists in another array

I have two associative array, and
I am trying to find the keys of the first array if they are present in second array.
<?php
$team_member = ["Person_1" => 0, "Person_2" => 0, "Person_3" => 0, "Person_4" => 0];
$today_active_member = ["Person_1" => 0, "Person_3" => 0, "Person_4" => 0];
$i = count($team_member);
while($i--){ //4, 3, 2, 1, false
if(in_array(key($team_member), $today_active_member)) { // check is key available in second array?
echo key($team_member) . " is present today.<br/>";
next($team_member); // goes to the next key
// code goes here
} else {
echo key($team_member) . " isn't present today.<br/>";
next($team_member); // goes to the next key
}
}
But above code is not giving the correct output. now the output is:
Person_1 is present today.
Person_2 is present today.
Person_3 is present today.
Person_4 is present today.
It should output:
Person_1 is present today.
Person_2 isn't present today.
Person_3 is present today.
Person_4 is present today.
How can I find those keys of first array which are in second array.
I've also tried to solve the problem with array_keys(), array_search() but it doesn't work
Instead, you should use the array_key_exists() function, which takes two arguments: the key you want to check for and the array you want to check in.
<?php
$team_member = ["Person_1" => 0, "Person_2" => 0, "Person_3" => 0, "Person_4" => 0];
$today_active_member = ["Person_1" => 0, "Person_3" => 0, "Person_4" => 0];
$i = count($team_member);
while($i--){
if(in_array(key($team_member), array_keys($today_active_member))) {
echo key($team_member) . " is present today.<br/>";
next($team_member);
} else {
echo key($team_member) . " isn't present today.<br/>";
next($team_member);
}
}
PHP online : https://onlinephp.io/c/48b7c
There is a far simpler way using a foreach loop and array_key_exists()
$team_member = ["Person_1" => 0, "Person_2" => 0, "Person_3" => 0, "Person_4" => 0];
$today_active_member = ["Person_1" => 0, "Person_3" => 0, "Person_4" => 0];
foreach( $team_member as $key=>$val){
if (array_key_exists($key, $today_active_member)){
echo $key . " is present today.<br/>";
} else {
echo $key . " isn't present today.<br/>";
}
}
RESULT
Person_1 is present today.<br/>
Person_2 isn't present today.<br/>
Person_3 is present today.<br/>
Person_4 is present today.<br/>

How to Insert checked checkboxes and delete unchecked checkboxes using php and mysql?

I have a 3 tables:
product, category and product_category.
I am creating a form who edit(insert/delete) categories for a product.
I am trying to insert an array of checkboxes in a relation table.
<input name="category[]" type="checkbox" value="<?php echo $cat->slug; ?>">
I know how to insert the checked values, but I don't know how to delete the unchecked ones.
$category = $_POST['category'];
for ($i = 0; $i < count($category); $i++) {
if (!empty($category)) {
$verifycategory = BD::conn()->prepare("SELECT * FROM `product_category` WHERE id_product = ? AND id_category = ?");
$verifycategory->execute(array($id_prod, $category[$i]));
if ($verifycategory->rowCount() == 0) {
$anm = BD::conn()->prepare("INSERT INTO product_category(id_product, id_category) VALUES(?,?)");
$anm->execute(array($id_prod, $category[$i]));
}
}
}
When you created the form/output you must have had an array of the possible checkboxes.
Just compare the values in this list to the ones posted.
$aCheckboxes = array(
'0' => array('ID' => 1, 'Name' => 'box1'),
'1' => array('ID' => 2, 'Name' => 'box2'),
'2' => array('ID' => 3, 'Name' => 'box3')
);
$aToDelete = array();
foreach($aCheckboxes as $iPos => $a){
if(!in_array($a['ID'], $_POST['category']){
$aToDelete[] = (int)$_POST['category'];
}
}
var_dump($aToDelete);

Passing an Array Key to a variable

I have a script which puts products into a shopping cart;
if (isset($_POST["top"])) {
$name = $_POST["name"];
$_SESSION[$$name] += 1;
$$name = $_SESSION[$$name];
$name = $name.$$name;
$piid = $_SESSION["piid"];
$prod = $_POST["prod"];
$_SESSION["cart"][$name] = array("id" => $prod, "name" => $_POST["name"], "quantity" => 1, "des" => $_POST["des"]);
foreach ($piid as $value) {
$ab = $value[id];
$qty = $_POST["htop".$ab];
if ($qty > 0) {
$piid[] = array("id" => $row["ID"], "des" => $row["des"], "hid" => $row["hide"]);
$_SESSION["cart"][$name]["top".$value[id]] = array("id" => $value[id], "dec" => $value[des], "qty" => $qty);
}
}
} else {
$name = $_POST["name"];
$name = $name.$$name;
if (isset($_SESSION['cart'][$name]) && ($_SESSION['cart'][$name]['des'] === $_POST['des'])) {
$_SESSION['cart'][$name]['quantity'] += 1;
} elseif (isset($_SESSION['cart'][$name]) && ($_SESSION['cart'][$name]['des'] <> $_POST['des'])) {
$_SESSION[$$name] += 1;
$name = $_SESSION[$$name];
$_SESSION["cart"][$name] = array("id" => $_POST["prod"], "name" => $_POST["name"], "quantity" => 1, "des" => $_POST["des"]);
} else {
$_SESSION["cart"][$name] = array("id" => $_POST["prod"], "name" => $_POST["name"], "quantity" => 1, "des" => $_POST["des"]);
}
}
To avoid confusion in the array when items have different description it will set a multi-dimensional array by using the product's name and an incremental id (where required)
Now my question is how do I get a remove button to work something like this?
I need to pass that sub-array's name/key as a variable so we can then pass that back to the POST method.
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post"
enctype="application/x-www-form-urlencoded">
<button type="submit">Remove
<input type="hidden" name="rprid" value="' .$name. '" />
<button</form></div>
Thanks!
I've managed to figure out the code I need which should be a follows;
foreach($cart as $key => $value)
{
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="application/x-www-form-urlencoded"><button type="submit">Remove<input type="hidden" name="rprid" value="' .$key. '" /></form></div>';
}
Thanks for the thoughts all!
Edit: The solution is to use $key => $value that way you can simply use $key to return the key which you are looking at.

editing a value in a multidimentional array in php

I've got a session called Cart_array which holds a multidimensional array in the following way:
$_SESSION['Cart_array'] = array(
1 => array(
"ID" => $pid,
"QTY" => 1
)
);
this is how items are added to the cart session. pid is obtained from another form
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
if (!isset($_SESSION['Cart_array']) || count($_SESSION['Cart_array']) < 1) { //check if cart session is not set or empty
$_SESSION['Cart_array'] = array(
1 => array(
"ID" => $pid,
"QTY" => 1
)
);
} else {
array_push($_SESSION['Cart_array'], array(
"ID" => $pid,
"QTY" => 1
));
} //end else
} //end if
the user has a form with the following things in a function:
<?php foreach ($_SESSION['Cart_array'] as $eachItem) {
$itemID = $eachItem['ID'];
$itemQty = $eachItem['QTY']; >?
<input class="qty" name="quantity" type="number" value="<?php echo $itemQty;?>" />
<input type="submit" name="qtyChange<?php echo $itemID;?>" value="Change Qty" />
<input name="qtyOfItem" type="hidden" value="<?php echo $itemID?>"/>
}
This form will go through the Cart_array and display the quantity in the cart for every item.
I want the user to be able to change the quantity in the cart for the specific item that they chose when they click the Change Qty button
I'm not sure how to go around doing this?
You could edit the array like this
Your Array
$list = array([0]=>
array(
[ID]=>'XYZ'
[QTY]=>'1'
)
);
my_function()
{
$list=$_SESSION['Cart_array'];
global $list;
$list[0]['QTY'] = '2'; //or this 2 value can be taken from user using jquery
}
my_function();
For the script that this submits to, you want to loop through each item in the cart_array session and find it by that ID then change the quantity for that item.
foreach($_SESSION['cart_array'] as $index => $item){
if($item['ID'] == $_POST['ID']){
$_SESSION['cart_array'][$index]['quantity'] = $_POST['quantity'];
}
}
Just pass your quantity value from the user like this
<?php
$_SESSION['Cart_array'] = array(
1 => array(
"ID" => $pid,
"QTY" => 1
)
);
$_SESSION['Cart_array'][1]['QTY']=30;//Relaces the quantity from 1 to 30

Work out percentage to auto-populate HTML Table

I have the following code:
function GetPercent($Arg){
$Count = count($Arg);
return /* Confused here */;
}
$Test_Array = array(
"ID" => 1,
"User" => "Test",
"Perm" => 1,
"Test" => "String"
);
I'm going to eventually populate a HTML Table column with the count($Test_Array), but I need to have the percentages to put inside the table attrib:
<td align=left style="width:XX%">
BUt, how would I go about working out the percentage?
Use floor() to round down so you don't end up with a total percentage of over 100%, and simply pass your array into the function to get the average width.
<?php
function GetPercent($Arg){
$Count = count($Arg);
return floor( 100 / $Count );
}
$Test_Array = array(
"ID" => 1,
"User" => "Test",
"Perm" => 1,
"Test" => "String"
);
$average_widths = GetPercent($Test_Array); // in this case will return 25
// ...table tags here etc etc etc
// output results
foreach( $Test_Array as $key => $value ) {
echo '<td align=left style="width:' . $average_widths . '%">';
echo $key . ' -> ' . $value;
echo '</td>';
}
?>

Categories