If Else statements by inputted form name [duplicate] - php

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));

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 ?>

Add rows in the database table depending on the array [duplicate]

This question already has answers here:
How to get a form input array into a PHP array
(9 answers)
Closed 4 years ago.
Please tell me there is an n-th amount of input. It is always different, maybe 1, maybe 20. The code looks like this:
<form action="/1.php" method="post">
<input type="text" value="1" name="more[1]">
<input type="text" value="2" name="more[2]">
<input type="text" value="3" name="more[3]">
<input type="text" value="4" name="more[4]">
<input type="text" value="5" name="more[5]">
<input type="submit">
</form>
How to add to the database table as many rows as we have input in the form? That is, 5 records should be created in the table. Thank you in advance.
On your Action page Create a loop on your input array
Try this
foreach($_POST['more'] as $value){
$query = mysqli_query('Insert Into TableName Values("",$value)');
}
Hope this idea will help you

Getting the number values from checked and unchecked boxes of a form [duplicate]

This question already has answers here:
detect unchecked checkbox php
(4 answers)
Help PHP and HTML Checkboxes
(3 answers)
PHP $_POST array for unchecked checkboxes
(2 answers)
POST arrays not showing unchecked Checkboxes
(6 answers)
Closed 5 years ago.
I am trying to fetch values from all (checked and unchecked) boxes in a form.
I know that unchecked values are normally not being sent to the server.
I've searched and found this:
<form action="<?php echo $_SERVER[PHP_SELF] ?>" method="post">
<input type="checkbox" id="1" name="box[]" value="1" />
<input type="checkbox" id="2" name="box[]" value="2" />
<input type="checkbox" id="3" name="box[]" value="3" />
<input type="submit" value="Submit" />
</form>
<?php
// $box = $_POST['box'];
$foo = array();
for ($i = 1; $i <= 3; $i++) {
$foo[$i] = in_array($i, $_POST['box']) ? 'on' : 'off';
}
print_r ($foo);
?>
The output is:
Array ( [1] => off [2] => on [3] => off )
It's a pretty interesting solution but it only gets on(1) for checked and off(0) for unchecked boxes, while I want to get the number values from the form.
As an output I'd like to have an array with checked and unchecked values.
How can I do it preferably using the code above as a backbone?
Please help.
I want to elaborate my question. If I have a folowing form:
<form action="" method="post">
<input type = "checkbox" id = "254" name = "voc2[]" value= "8089" />yes
<input type = "checkbox" id = "255" name = "voc2[]" value= "8148" />no
<input type = "checkbox" id = "256" name = "voc2[]" value= "8207" />maybe
<input name="submit" type="submit" />
</form>
Can I get one of the values (say 8089) on the same page if it is not checked?
I've tryed:
<?php
if (isset($_POST['submit'])) {
$voc2 = $_POST['voc2'];
echo '<br> $voc2 = '. $voc2[0];
}
?>
but it still shows only the checked values.

for loop is not echoing the expected values from my array

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])

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
}

Categories