i have 3 files, maintransaction.php, computation.php, and computepayment.php
in maintransaction, it lists all the procedures and price which is in checkbox. once submitted, redirects to computation.php where it computes the checked procedures.
computation.php
foreach($ercharge as $charge) {
$p = explode(",", $charge);
$price[] = $p[0]; //stores the price of the $charge in $price array
$procedure[] = $p[1]; // stores the procedure of the $charge in $procedure array
I added that to my 2nd file to get the procedure and the price. its in foreach so that when the user selects MULTIPLE values, it will store it in an array. Cause thats the tricky part, when a user selects more than one in the checkbox.
i also added this to test that the mutliple selected procedures where passed
foreach ($procedure as $pro) {
$_SESSION['receipt'] = $pro;
echo $_SESSION['receipt'];
}
it works fine. all the procedures checked where printed out.
after clicking another button, redirects to computepayment.php, where you enter the amount to be paid.
i added this to the file:
print_r($_SESSION['receipt']);
it only shows ONE value. not ALL the values checked. i copied the foreach format from computation.php, as i wouldve guessed, it wouldnt work cause it doesnt recognize the variable $procedure.
How can i output the same values that were from the 2nd file, into the third file?
You need to set the session values in $_SESSION['receipt']. The way you did it, you just overrode at every iteration, the code below adds it to the array.
$_SESSION['receipt'] = array();
foreach ($procedure as $pro) {
$_SESSION['receipt'][] = $pro;
}
Related
I have a JSON multi-dimensional array of products from Shopify that I am looping through. Each product has 3 options and each option can have infinite numbers of values.
For example:
Lamp (product)
Glass Finish (option)
Clear (value)
Smoke (value)
Metal Finish (option)
Polished Chrome (value)
Drop (option)
600mm (value)
800mm (value)
Within the loop I am creating rows within a 'repeater' field in a CMS (one for each option and then a repeater within each option for each value).
This is all fine BUT I want to do a few checks as the array of products is cached to every 30 minutes so if the cache was updated and either an option was removed or a value to that option was added then it should update.
I have my first check in place (loop through all the existing repeater options that have been added, before doing anything, and if an option already exists then skip it (and thus not creating multiple of the same options).
What I need to do is to check each of the values to see if any new ones have been added. I can write the code to actually add the value to the repeater field but I am unsure how to check as by this point, if the option already exists, it skips over.
Any thoughts?
foreach($product['options'] as $option) {
foreach($p->shop_product_options as $options) {
// If this option already exists... then skip the parent loop on this product
if ($options->global_text == $option['name']) {
continue 2;
}
}
$options = $p->shop_product_options->getNew();
$options->of(false);
$options->global_text = $option['name'];
$options->save();
$p->shop_product_options->add($options);
foreach($option['values'] as $o) {
$values = $options->shop_product_options_option->getNew();
$values->of(false);
$values->global_text = $o;
$values->save();
$options->save();
$options->shop_product_options_option->add($values);
}
$options->save();
}
Still not sure I am completely understanding what you are after but it sounds like you need something like this :
foreach($product['options'] as $option) {
$optionNames = array_column($options, 'name');
$options = $p->shop_product_options->getNew();
foreach($option['values'] as $o) {
$values = $options->shop_product_options_option->getNew();
if($values->global_text === $o && in_array($option->global_text, $optionNames, true)) {
continue 2;
}
$values->of(false);
$values->global_text = $o;
$values->save();
$options->shop_product_options_option->add($values);
}
$options->of(false);
$options->global_text = $option['name'];
$p->shop_product_options->add($options);
$options->save();
}
array_column will create an array getting all the values of a multidimensional array when the key is name, and then the if statement will look for a combination of name and value.
And since the saving logic for the option is below the loop for the values,
it will check the cache before saving anything, this might not be a copy/paste answer, i'm just giving you some logic to work with
hey there so basically i'm trying to make a cart and i want the id products to be in a array so that i can serialize them in a cookie for 15 days, the problem i that each time i press the button "add to cart" it changes the previous id instead of adding to the array, i've tried making a loop and changing the index each time and i've tried just adding with $cart[]=$itemId; and the array_push(); function, nothing seems to work.
if (isset($_POST['item'])) {
$item = $_POST['item'];
$panier = array();
$panier[] = $item;
print_r($panier);
}
Just to explain the code, this code will be used when the add to cart button is pressed and retain the product id from the post variable and then add to the array each time but instead i only get a single value in the array that keeps changing everytime i press the button to add.
That's because you're creating a new array each time and then just adding one value to it. Remove $panier = array() to avoid creating a new array each time.
Combining #IncredibleHat's suggestion into working example:
$item = 0;
while($item <= 5) {
if (!isset($panier)) { $panier = array(); }
$panier[] = $item;
$item++;
}
print_r($panier);
Notice that if you remove the if(!isset($panier), then $panier only ends up containing the last value, because it gets re-initialised to empty every iteration through the loop.
In addition to not using storage that persists between requests, your method prevents people buying more than one of an item:
<?php
session_start();
if (isset($_POST['item'])) {
$_SESSION['panier'][$_POST['item']]=
isset($_SESSION['panier'][$_POST['item']) ?
$_SESSION['panier'][$_POST['item']] + 1 :
1;
}
I need to get all the elements from a textarea in a HTML form and insert them into the MySQL database using PHP. I managed to get them into an array and also find the number of elements in the array as well. However when I try to execute it in a while loop it continues displaying the word "execution" (inside the loop) over a 1000 times in the page.
I cannot figure out what would be the issue, because the while loop is the only applicable one for this instance
$sent = $_REQUEST['EmpEmergencyNumbers'];
$data_array = explode("\n", $sent);
print_r($data_array);
$array_length = count($data_array);
echo $array_length;
while(count($data_array)){
echo "execution "; // This would be replaced by the SQL insert statement
}
you should use
foreach($data_array as $array)
{
//sql
}
When you access the submitted data in your php, it will be available in either $_GET or $_POST arrays, depending upon the method(GET/POST) in which you have submitted it. Avoid using the $_REQUEST array. Instead use, $_GET / $_POST (depending upon the method used).
To loop through each element in an array, you could use a foreach loop.
Example:
//...
foreach($data_array as $d)
{
// now $d will contain the array element
echo $d; // use $d to insert it into the db or do something
}
Use foreach for the array or decrement the count, your while loop
is a infinite loop if count is >0
I have a form with some checkboxes in Drupal and I need to get the checked boxes to add them to a database. To get the values in the checkboxes I use var_export which returns an array indicating if the checkbox has been checked. After I have this array store in a variable I do this:
$checked = array();
if(is_array($data) {
foreach($data as &$value) {
if($value != 0) { //the checkbox was checked
$checked[] = $value;
}
}
However, when I print out the variable $checked there is nothing stored in it. What am I doing wrong?
The normal way to do this in Drupal would be:
$checked = array_filter($form_state['values']['name_of_checkboxes_element']);
That will give you an array of all the values selected in your checkbox element, assuming you're running this code in the submit/validate handler for the form.
Also I should mention the Devel module, it has a wonderful function called dpm() which prints the value of any variable to the messages area in a hierarchical format that you can navigate through easily.
I post some data over to another page from a form. It's a shopping cart, and the form that's being submitted is being generated on the page before depending on how many items are in the cart. For example, if there's only 1 items then we only have the field name 'item_name_1' which should store a value like "Sticker" and 'item_price_1' which stores the price of that item. But if someone has 5 items, we would need 'item_name_2', 'item_name_3', etc. to get the values for each item up to the fifth one.
What would be the best way to loop through those items to get the values?
Here's what I have, which obviously isn't working.
extract($_POST);
$x = 1; // Assuming there's always one item we're on this page, we set the variable to get into the loop
while(${'item_name_' .$x} != '') {
echo ${'item_name' .$x};
$x++;
}
I'm still relatively new to this kind of usage, so I'm not entirely how the best way to deal with it.
Thanks.
First, please do not use extract(), it can be a security problem because it is easy to manipulate POST parameters
In addition, you don't have to use variable variable names (that sounds odd), instead:
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value'";
}
To ensure that you have only parameters beginning with 'item_name' you can check it like so:
$param_name = 'item_name';
if(substr($key, 0, strlen($param_name)) == $param_name) {
// do something
}
Use array-like fields:
<input name="name_for_the_items[]"/>
You can loop through the fields:
foreach($_POST['name_for_the_items'] as $item)
{
//do something with $item
}
If your post keys have to be parsed and the keys are sequences with data, you can try this:
Post data example: Storeitem|14=data14
foreach($_POST as $key => $value){
$key=Filterdata($key); $value=Filterdata($value);
echo($key."=".$value."<br>");
}
then you can use strpos to isolate the end of the key separating the number from the key.
i wouldn't do it this way
I'd use name arrays in the form elements
so i'd get the layout
$_POST['field'][0]['name'] = 'value';
$_POST['field'][0]['price'] = 'value';
$_POST['field'][1]['name'] = 'value';
$_POST['field'][1]['price'] = 'value';
then you could do an array slice to get the amount you need