for loop is not echoing the expected values from my array - php

I am writing some code for a module for Zen Cart. $stores_id is an array containing 3 values:
$stores_id[0]="1";
$stores_id[1]="2";
$stores_id[2]="3";
With the following code I am trying to echo a hidden input field, filled with data from the array
for ($i=0, $n=sizeof($stores_id); $i<$n; $i++)
{
echo zen_draw_hidden_field('stores_id['. $stores_id[$i]['stores_id'] .']', htmlspecialchars(stripslashes($stores_id[$stores_id[$i]['stores_id']]), ENT_COMPAT, CHARSET, TRUE));
}
the echoed result is:
<input type="hidden" value="2" name="stores_id[1]">
<input type="hidden" value="3" name="stores_id[2]">
<input type="hidden" name="stores_id[3]">
while I expected it to be:
<input type="hidden" value="1" name="stores_id[1]">
<input type="hidden" value="2" name="stores_id[2]">
<input type="hidden" value="3" name="stores_id[3]">
Can anyone tell me what I am doing wrong?

It looks like you are nesting your 2nd parameter 1 depth too far -
$stores_id[$stores_id[$i]['stores_id']]
So when $i == 0, you are getting $stores_id[1], which is 2, instead of $stores_id[0] which is 1. And when you get to $i == 2 you have $stores_id[3] which is not in the array.
So either remove the outer array -
htmlspecialchars(stripslashes($stores_id[$i]['stores_id'])
or subtract 1 from the inner array returned value
htmlspecialchars(stripslashes($stores_id[$stores_id[$i]['stores_id']-1])

Related

Set the uncheck checkbox to 0 without using hidden textbox

Im trying to get all of the checked values using array i already get all of the checked box and get the value but my problem is i want to pass to array all of value of checbox, 1 for check and 0 for uncheck. sorry for the english.
this is what i want to get:
array[0] = 1 //check
array[1] = 0 // uncheck
// and so on.
i have already tried a hidden box with same name of checkbox but it give me wrong data of array and i am sure its not the answer
<input type = "checkbox" name="checkbox[]" value="1">
$checkbox1 = ($_POST['checkbox'] <> 0) ? ($_POST['checkbox']) : (empty($_POST['checkbox'])) ?'0' : '';
output: 0
output of print_r($checkbox1) : 0
expected output : array[0]=> 1 --> check array[1] => 0 -> uncheck
When working with a PHP backend where you want to post an array of values (using the name="something[]" syntax) you need to create <input type="hidden"> and <input type="checkbox"> pairs that will refer to the exact same array index.
You do so by explicitly defining the index in the name attribute.
For example
<input type="hidden" name="checkbox[0]" value="0">
<input type="checkbox" name="checkbox[0]" value="1">
<input type="hidden" name="checkbox[1]" value="0">
<input type="checkbox" name="checkbox[1]" value="1">
<input type="hidden" name="checkbox[2]" value="0">
<input type="checkbox" name="checkbox[2]" value="1">
I believe this also works for ASP.NET-MVC backends. Not sure about others though.
If you're looping over records or a range, this is as simple as keeping an iteration index and using it in the name attributes
<?php for ($i = 0; $i < $range; $i++): ?>
<input type="hidden" name="checkbox[<?= $i ?>]" value="0">
<input type="checkbox" name="checkbox[<?= $i ?>]" value="1">
<?php endfor ?>

If Else statements by inputted form name [duplicate]

This question already has answers here:
php: check if an array has duplicates
(17 answers)
Closed 7 months ago.
is there a way to write an if else statement by the input field name?
I have a loop that dynamically creates 9 input field,
The loop
for($i=0; $i<($n*$n); $i++){
echo "<tr>";
for($j=0; $j<($n*$n); $j++){
$number = "column".$i.$j;
if($i%$n==0 && $j%$n==0 && $j!==0 && $i!==0){
echo "<td><input class='field' type='text' name=$number value=$_POST[$number]></td>";
}
and the output is this.
<input class="field" type="text" name="column00" value="1">
<input class="field" type="text" name="column01" value="2">
<input class="field" type="text" name="column02" value="1">
.....
<input class="field" type="text" name="column09" value="3">
So what I am trying to do is, that if there is a inputed number on row 1 to 9 that is equal to another number in row 1 to 9 it will echo out that there more than one number thats equal to each other.
I tried something like this but it wouldn't work.
if($number==$number){
echo = "equal number in the same row";
}
Make an array of all the inputs from a row. Then do:
$unique = array_unique($row_values);
if (count($unique) != count($row_values)) {
echo "No duplicate numbers in a row!";
}
Do the same thing for each column.
Use field names in this form:
<input name="number[<row>][<column>]" ...
So, for instance:
<input name="number[0][0]" value="1" />
<input name="number[0][1]" value="2" />
<input name="number[0][2]" value="5" />
<input name="number[0][3]" value="3" />
...
<input name="number[1][0]" value="4" />
<input name="number[1][1]" value="9" />
When posted, you will receive a two-dimensional array:
[[1, 2, 5, 3, ...], [4, 9, ...]]
Now it basically becomes an array problem you have to solve for:
All rows and columns
Rows and columns within each grid
To help you find the value that occurs multiple times in an array:
$multiples = array_filter(array_count_values($arr), function($freq) {
return $freq > 1;
});
// outputs a non-empty array with the numbers that occur more than once.
print_r(array_keys($multiples));

Post values of a checkbox

I have this code on my form for the checkboxs:
<input type="hidden" name="option_desc[]" value="option 1"/>
<label><input type="checkbox" name="option_price[]" value="10" class="option_checkbox"/>option 1</label>
<input type="hidden" name="option_desc[]" value="option 2"/>
<label><input type="checkbox" name="option_price[]" value="20" class="option_checkbox"/>option 2</label>
<input type="hidden" name="option_desc[]" value="option 3"/>
<label><input type="checkbox" name="option_price[]" value="30" class="option_checkbox"/>option 3</label>
I'm trying to get the POST values of the checkbox that the user checked (for example, if he checked the second checkbox: "option 2" + "20") and store them:
$articleDetails['options'] = array();
$count = 0;
if(is_array($_POST['option_price'])){
foreach($_POST['option_price'] as $key => $value){
if($value){
$articleDetails['options'][$count]['option_price'] = $_POST['option_price'][$key];
$articleDetails['options'][$count]['option_desc'] = $_POST['option_desc'][$key];
$count++;
}
}
}
When the user check one of the checkbox, the appropriate 'option_price' is stored correctly, but the 'option_desc' is not the one that belongs to the CHECKED checkbox (for example: when the second checkbox is checked the values that I get are "20" (GOOD) and "option 1" (NOT GOOD).
What I'm doing wrong?
Thanks.
The problem you are having is that if checkboxes are not checked, the data won't get POSTed while your hidden inputs will always be posted. Frankly, I don't see what value your hidden inputs give you here. They tell you absolutely nothing about the posted data that you don't already know about on the server.
You should simply use a defined index in your array notation for the checkbox field, like this:
<input type="checkbox" name="option_price[1]" value="10" class="option_checkbox"/>option 1</label>
<input type="checkbox" name="option_price[2]" value="20" class="option_checkbox"/>option 2</label>
<input type="checkbox" name="option_price[3]" value="30" class="option_checkbox"/>option 3</label>
Note: I didn't use zero-indexed array here as I figured you might want to directly relate $_POST['option_price'][1] to "option 1".
To add some examples related to the discussion in comments below. Say for example you had some option_price checkboxes you want to output, and they come from some dynamic source. Your code to to generate the checkboxes might look something like this:
$price_options = array(
array(
'description' => 'eBook',
'value' => 10
),
array(
'description' => 'articles',
'value' => 20
),
// and so on
);
$count = count($price_options)
for($i = 1; $i<= $count; $i++) {
?>
<input type="checkbox" name="option_price[<?php echo $i; ?>]" value="<?php echo $price_options[$i]['value']; ?>" class="option_checkbox"/><?php echo $price_options[$i]['description']; ?></label>
<?php
} // end for
When POSTing you know that every $_POST['option_price'][x] would correspond to the item at $price_options[x].
You could simply iterate of $_POST['option_price'] to see which items are selected like this:
if(!empty($_POST['option_price']) {
foreach ($_POST['option_price'] as $index => $value) {
// verify value hasn't been tampered with
if ((int) $value === $price_options[$index]['value']) {
// set description
$description = $price_options[$index]['description'];
var_dump($description, $value);
}
}
}
I would suggest give names to input like
<input type=hidden name="option[1][desc]"/>
<input type=hidden name="option[1][price]"/>
Then I think it would be easy to run a forreach as well.

Passing arrays from HTML form to PHP

This is the HTML:
<input type="text" name="shortcut[]" value="a"/> do <input type="text" name="ses[]" value="1" disabled/><br>
<input type="text" name="shortcut[]" value="b"/> do <input type="text" name="ses[]" value="2" disabled/><br>
<input type="text" name="shortcut[]" value="c"/> do <input type="text" name="ses[]" value="3" disabled/><br>
How do I pass the values to PHP but connect the indexes of both arrays?
i.e. put in database value 1 where something = a,
put in database value 2 where something = b
and so on ...
The indexes are connected automatically, since they're numeric arrays.
$nvals = count($_REQUEST['shortcut']);
for ($i = 0; $i < $nvals; $i++) {
// do something with $_REQUEST['shortcut'][$i] and $_REQUEST['ses'][$i]
}
Combined array: array_map(null,$_POST['shortcut'],$_POST['ses']);
But you could of course could foreach over one of the 2, and fetch the other by key.
Note that if you have elements which may or may not be sent (checkboxes for instance), the only way to keep groups together is to assign them a number beforehand (name=sess[1], name=sess[2], etc.)
You can specify shortcut value as the key and the ses value as the value attribute:
<input type="text" name="input[a]" value="1" />
<input type="text" name="input[b]" value="2" />
<input type="text" name="input[c]" value="3" />
On the server-side you could use a foreach loop to iterate over the array:
foreach ($_POST['input'] as $shortcut => $ses) {
// process $shortcut and $ses
}

$_POST and variable indexes

I have a project where I create hidden input values with jQuery. These represent groups and members so I have one array for the groups themselves and arrays with the members of each groups.
For example after a few creations the code in the form includes
<input type="hidden" name="groups['group_2']" value="0">
<input type="hidden" name="groups['group_1']" value="0">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="FWNGVISkjW">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="G0t9E3C0yG">
<input type="hidden" class="mem_id_holder" name="group_2[]" value="NT-JEDVCS9">
for an example of 2 groups with 2 members in group_1 and 1 member in group_2.
Now, after the submit my code to iterate through the values I use this code:
if ($groups_no && isset($_POST['groups']) && !empty($_POST['groups'])){
$groups = $_POST['groups'];
foreach ($groups as $key => $val){
if (isset($_POST[$key]) && !empty($_POST[$key])){
$group_members = $_POST[$key];
foreach ($group_members as $member_key => $member_val){
echo 'Actions to be done here!';
}
}
}
}
The problem I have is that I get the following warning and the program fails:
Notice: Undefined index: 'group_1' in C:\ ... .php on line 31
Warning: Invalid argument supplied for foreach() ...
Notice: Undefined index: 'group_2' in C:\ ... .php on line 31
Warning: Invalid argument supplied for foreach() ...
When I use $_POST['group_1'] everything works fine but since I do not know how many and which groups I will have to insert I need the variable. I have tried some different things suggested via some Google results but nothing worked.
Any ideas? Is $_POST even capable of having variables as indexes? Also if it isn't, is there any other workaround?
In your HTML, don't put quotes around the array indexes.
<input type="hidden" name="groups[group_2]" value="0">
<input type="hidden" name="groups[group_1]" value="0">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="FWNGVISkjW">
<input type="hidden" class="mem_id_holder" name="group_1[]" value="G0t9E3C0yG">
<input type="hidden" class="mem_id_holder" name="group_2[]" value="NT-JEDVCS9">
By doing this:
<input type="hidden" class="mem_id_holder" name="group_1[]" value="FWNGVISkjW">
Instead of this:
<input type="hidden" class="mem_id_holder" name="group_1" value="FWNGVISkjW">
When you post the values you are going to get an array, so $_POST['group_1'] would not exist as a string in the first instance, it would be $_POST['group_1'][0]. So you would have to iterate through that value as well.
Hope this helps.

Categories