As the title says. I'm trying to store every value from $_POST['spell'.$j.'icon'.$i] inside a 2d array and I'm curious if there is any way to make this array look the same way so $spell[$j]icon[$i]
$spellicon=array();
for($i=1;$i<=$_POST['champ_number']; $i++){
for($j=1; $j<=$noofspellschamp[$i]; $j++){
$spellicon[$j][$i]=$_POST['spell'.$j.'icon'.$i];
}
}
EDIT Bit of code from the previous site. Data is nested so I need it to be in 2d array.
for($i=1;$i<=$champ_number; $i++){
echo $_POST['champno'.$i].'<br/>';
//echo '<input type="hidden" value="'.$_POST['champno'.$i].'" name="champno'.$i.'">';
$champno[$i] = $_POST['champno'.$i];
//echo '<input type="hidden" value="'.$_POST['noofspellschamp'.$i].'" name="noofspellschamp'.$i.'">';
$noofspellschamp[$i] = $_POST['noofspellschamp'.$i];
for($j=1; $j<=$_POST['noofspellschamp'.$i]; $j++){
echo '<select name="spell'.$j.'icon'.$i.'">';
echo '<option value="Passive">Passive</option>';
echo '<option value="Q" selected>Q</option>';
echo '<option value="W">W</option>';
echo '<option value="E">E</option>';
echo '<option value="R">R</option>';
echo '</select>';
if($i==1&&$j==1){
echo '<input type="text" name="spell'.$j.'title'.$i.'" placeholder="Spell '.$j.' name" required autofocus><br/>';
}
else{
echo '<input type="text" name="spell'.$j.'title'.$i.'" placeholder="Spell '.$j.' name" required><br/>';
}
Edit 2 Print_R($_POST)
Array (
[formid] => 6
[champ_number] => 2
[spell1icon1] => Q
[spell1title1] => spell1
[champno1spellno1] => 1
[description111] => sakdop
[change111] => buff
[spell1icon2] => Q
[spell1title2] => sdkop
[champno2spellno1] => 1
[description121] => sadas
[change121] => buff
[noofspellschamp] => {"1":"1","2":"1"}
[champno] => {"1":"Garen","2":"Katarina"}
[patch] => 0.03
)
There's no such thing as $spell[$j]icon[$i] in PHP. The approach you're using($spellicon[$j][$i]) is the only one in standard PHP (I mean, without classes).
But it'll be better if you do it like this: $spellicon[$i][$j], because it's the champion that has the spells, and not the other way around.
Code below is simplification. Check included topic. Just use
$_POST['spellicon'][$j][$i]
or
$_POST['spell'][$j]['icon'][$i]
and the you will have same naming convention as your variable.
Considering form building it will be cleaner and you can group your data with good manner. If you don't know how to send multidimensional arrays via POST, check this topic
Related
Heloo,
I have the following PHP code:
<?php
$ratings = array(
1 => "All",
2 => "Love",
3 => "Hate",
4 => "Maybe",
5 => "Super"
);
$question = array(
1 => "Pizza",
2 => "Prajitura",
3 => "Placinta",
);
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<p>How do you feel about each topic?</p>';
foreach ($question as $key=>$value){
echo "".$value."<br>";
echo "aici are valoarea".$key;
$importHtml="";
foreach ($ratings as $cheie => $raspuns) {
// echo "cheie: ".$key."cheie raspuns:".$cheie."raspuns".$raspuns."\n";
$importHtml .= "$raspuns <input type='checkbox' name='i_".$key."_importance[] id='$raspuns' value='$cheie' />";
}
echo "". $importHtml."<br>\n";
}
echo '<input type="submit" value="Save Questionnaire" name="submit" />';
echo '</form>';
if (isset($_POST['submit'])) {
foreach ($question as $key => $value) {
//if(isset( $_REQUEST["i_".$key."_importance"]))
$print="";
$importance = $_REQUEST["i_".$key."_importance"];
//var_dump($importance);
echo "cheie >".$key."<br>\n";
foreach($importance as $cheie=>$valoare){
$print .="cheie".$cheie."valoare =".$valoare;
//echo "cheie".$cheie."valoare =".$valoare;
echo "<br>\n";
}
echo $print;
}
}
?>
And I wish to retrieve values stored into every array named i_$key_importance using a foreach loop or a for loop. The result is not as I expected to. In the i_$key_importance i want to store 0 value if the client has not checked the box and the key value from associative array ratings if has done so.
The expected outcome:
$importance = $_REQUEST["i_".$key."_importance"];
$importance will be an array containing {
1=> 1,
2=> 0 (here the user hasn't selected an option),
3=> 3,
4=> 0 (here the user hasn't selected an option),
}
the result will be stored in an table with Id = key and corresponding column for every answer will contain 0 (if client hasn't selected an answer) or a number (if has selected an answer).
Any help great appreciation,
You should maybe consider a tag instead of the regular checkboxes.
In order to do what you want to do with the checkboxes, you can just test if the value returned by the checkbox is equal to the value linked to it, if not that means the user didn't check it.
<?php
$ctr = 0;
foreach($rows as $row){
?>
<input type="hidden" name="client<?php echo $ctr; ?>['client_id']" value="<?php echo $row['client_id']; ?>">
<input type="hidden" name="client<?php echo $ctr; ?>['transaction_id']" value="<?php echo $row['id']; ?>">
<input style="max-width: 100px;min-width:100px;" class="form-control right" type="text" name="client<?php echo $ctr;?>['amount']" value="" />
<?php
}
?>
Update: I have included the form where $_POST coming from.
This it the output of print_r ($_POST);:
Array
(
[client0] => Array
(
['client_id'] => 1
['transaction_id'] => 1
['amount'] => 1000
['mode'] => cash
)
[client1] => Array
(
['client_id'] => 2
['transaction_id'] => 5
['amount'] => 600
['mode'] => cash
)
[client2] => Array
(
['client_id'] => 3
['transaction_id'] => 6
['amount'] => 200
['mode'] => cash
)
[save] =>
)
When I try writing this: echo $_POST['client0']['amount'];.
I'm expecting an output of 1000
but it gives me an error like this: Notice: Undefined index: amount.
So guys can you please tell me whats wrong with my code.TIA
Compare the print_r output carefully. One key is given as client0 while the next is given as 'amount'. That's because your key is actually 'amount', not amount. Because you're including unnecessary quotes in your HTML. Fix your HTML so the input name becomes:
name="client0[amount]"
(Or alternatively address the key as $_POST['client0']["'amount'"].)
And while you're at it, you may want to rename your fields to:
name="clients[0][amount]"
Because then you can simply traverse the data using:
foreach ($_POST['clients'] as $client) {
echo $client['id'], $client['amount'], ..;
}
Which is much more readable and sane.
You should use
echo $_POST['client0']['amount'];
You are using wrong variable amount_id just check it out your array having variable is "amount" so change the variable name like bellow
echo $_POST['client0']['amount'];
I hope this will help you.
Try this way ..
foreach ($_POST['client0'] as $val) {
echo $val['amount'] ;
}
I have an HTML form that's submitting to a PHP script and send it as an email.
The problem I'm having is iterating the array and getting the output formatted into a table correctly.
My input fields look like this:
<input class="form-control" type="text" name="alternate[][qty]" />
<input class="form-control" type="text" name="alternate[][height]" />
I am iterating the array like this:
if ( isset( $_POST['alternate']))
{
foreach($_POST['alternate'] as $alt)
{
$message .= "<tr><td>" . $alt['qty'] . "</td><td>" . $alt['height'] . "</td></tr>";
}
}
I'm getting the correct values from the array but they are not formatted correctly. I am looking for an output something like this:
123 45
but instead it breaks across two rows like this:
How can I get both values on the same line?
EDIT:
Using
echo '<pre>';
print_r($_POST['alternate']);
echo '</pre>';
I get
Array
(
[0] => Array
(
[qty] => 54
)
[1] => Array
(
[height] => 5
)
[2] => Array
(
[qty] => 34
)
[3] => Array
(
[height] => 5
)
[4] => Array
(
[qty] => 36
)
[5] => Array
(
[height] => 45
)
...
)
which makes it look like I actually have 6 arrays...? That would explain why I'm getting each cell on a separate row, but I still don't understand how to fix it...
You're iterating through every element in $_POST['alternate'] and creating a row for each iteration. There are two elements, thus two rows.
There's no need to iterate since you already know which elements you'll get:
if ( isset( $_POST['alternate']))
{
$message = "<tr><td>{$_POST['alternate']['qty']}</td><td>{$_POST['alternate']['height']}</td></tr>";
}
Give this a shot, I hope that's what you're opting for.
if(isset($_POST['alternate']))
{
$message = "<tr>";
foreach($_POST['alternate'] as $alt)
{
if(isset($alt['qty']))
$message .= "<td>" . $alt['qty'] . "</td>";
elseif(isset($alt['height']))
$message .= "<td>" . $alt['height'] . "</td>";
}
$message .= "</tr>";
}
This gave me <tr><td>123</td><td>45</td></tr>.
EDITED SOLUTION
This script actually takes care of <tr> tags inside of the loop.
if(isset($_POST['alternate']))
{
foreach($_POST['alternate'] as $alt)
{
if(isset($alt['qty']))
$message .= "<tr><td>" . $alt['qty'] . "</td>";
elseif(isset($alt['height']))
$message .= "<td>" . $alt['height'] . "</td></tr>";
}
}
Try it out and let me know.
Your HTML actually declares separate array entries. You need to group them by defining keys for the array. Something like:
<input class="form-control" type="text" name="alternate[0][qty]" />
<input class="form-control" type="text" name="alternate[0][height]" />
Then the next group of fields uses "1" and so on.
This is my php array $data.
Array
(
[upcoming] => Array
(
[webinars] => Array
(
[0] => Array
(
[webinarKey] => 123456
[subject] => webinar title ...
[times] => Array
(
[0] => Array
(
[startTime] => 2014-04-03T00:00:00Z
Please check my code below. I want to get values of "webinarkey" and "subject" and "start date - with date formatted" and put them into my select box. I can now populate "webinarkey and "subject". Thank you all to help to to populate "webinarkey and subject" but now i want to show "start date with date format as well.
echo '<form method="POST"><select>';
foreach ($data["upcoming"]["webinars"] as $webinar) {
echo '<option value="' . htmlspecialchars($webinar["webinarKey"]) . '">' . htmlspecialchars($webinar["subject"]) . htmlspecialchars($webinar["times"]["startTime"]) . '</option>';
}
echo '</select></form>';
Please help me to show start date as well.
$data = array(/**/);
foreach ($data["upcoming"]["webinars"] as $webinar) {
echo '<option value="' . htmlspecialchars($webinar["webinarKey"]) . '">' . htmlspecialchars($webinar["subject"]) . '</option>';
}
From what I gather the only array you need to loop over is $data["upcoming"]["webinars"].
Ensure your output HTML is valid and your data is escaped properly.
Since you are trying to access a value in a multidimensional array, you cannot use -> as that is for properties of objects. Instead you need to access it like a normal array: $array[key]
Updated Code:
echo '<select>';
foreach ($webinars as $obj)
{
foreach ($obj['webinars'] as $ob)
{
echo '<option value='.$ob['webinarKey'].'>'.$ob['subject'].'</option>';
}
}
echo '</select>';
You're trying to use properties, it's an array not an object.
You need to use $ob['----'] instead of $ob-> notation
echo '<select>';
foreach ($webinars as $obj)
{
foreach ($obj['webinars'] as $ob)
{
echo '<option value='.$ob['webinarKey'].'>'.$ob['subject'].'</option>';
}
}
echo '</select>';
I have a multi-select box for country selection. I want to select any countries which are associated, meaning an array I get from the database.
Here's the code I have:
<?php
foreach($countries as $country){
if(!empty($offer_countries)){
foreach($offer_countries as $key => $offer_country){
if(isset($offer_country['country_id']) && ($offer_country['country_id'] == $country['id'])){
echo '<option value="'.$country['id'].'" selected>'.$country['name'].'</option>';
}else{
echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';
}
}
}else{
echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';
}
}
?>
The $offer_countries array, looks like this:
Array
(
[0] => Array
(
[country_id] => 1
)
[1] => Array
(
[country_id] => 2
)
[2] => Array
(
[country_id] => 3
)
)
I'm looping all countries to display them, then I have a nested foreach to see if the country is already set, if so, make the option box selected.
The problem with this is that let's say I have 3 items selected, it'll display 3 of the same country, based on the number of items in the array. So if United States should be checked, it'll show it three times, with the last one checked.
Ok, sorry for the looong explanation, it's probably fairly self explanatory, but any help would be awesome!
This solved it:
<?php
foreach($countries as $country){
$i = 0;
if(!empty($offer_countries)){
foreach($offer_countries as $key => $offer_country){
if($offer_country['country_id'] == $country['id']){
echo '<option value="'.$country['id'].'" selected>'.$country['name'].'</option>';
$i = 1;
break;
}
}
if($i == 0){
echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';
}
}else{
echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';
}
}
?>
Your inner 'foreach' statement is going to output 'something' whether or not the value is set, and it does so based on the $country variable set up in the outer foreach loop.
So what happens is that you output on the outer 'foreach' loop once for each time it runs on the inner foreach loop.