Incrementing through POST variables - php

I am making a website that has x amount of input fields. Each of the input fields is labeled team0, team1, team2 ... teamx and they are wrapped in a post form. Upon posting, I would like to check the POST variables, store them in an array, print out their values, but it seems I cannot use a variable when calling $_POST. I have tried it like so:
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team$i'];
echo $teamNames[$i] . "<br>";
}
What is the correct way to do this?

Try double quotes
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST["team$i"];
echo $teamNames[$i] . "<br>";
}
OR
for ($i = 0; $i < $NUMBER_OF_TEAMS; $i++) {
$teamNames[$i] = $_POST['team'.$i];
echo $teamNames[$i] . "<br>";
}

Related

populating <select> tag with PHP

I am trying to dynamically populate a select tag in php, by printing values from an array of objects.
this is my code
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[i]->getId().'">'.$z[i]->getDescription().'</option>');
}
the array is populated, in fact if i try to print only the fields i get a result, but in the combo-box nothing appears
Replace i with $i
<select name="name">
<?php
for($i = 0; $i < count($z); $i++)
{
print('<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>');
}?>
</select>
Add select tag and replace i with $i
echo "<select>";
for($i = 0; $i < count($z); $i++) {
echo '<option value ="'.$z[$i]->getId().'">'.$z[$i]->getDescription().'</option>';
}
echo "</select>";
ok, i resolved by creating two support arrays
$ids = array();
$descs = array();
for ($i = 0; $i < count($z); $i++) {
$ids[$i] = $z[$i]->getId();
$descs[$i] = $z[$i]->getDescritpion();
}
and using them instead of the objects
for ($i = 0; $i < count($z); $i++) {
print('<option value ="' . $ids[$i] . '">' . $descs[$i] . '</option>');
}
not very efficient, but working.
thanks for the answer :)

How to change the value with a variable within a for loop?

I have the following code:
$extraPhoto_1 = get_field('extra_photo_1');
$extraPhoto_2 = get_field('extra_photo_2');
$extraPhoto_3 = get_field('extra_photo_3');
$extraPhoto_4 = get_field('extra_photo_4');
But I would like to rewrite it with a for loop, but I can't figure out how to put a variable within the value field. What I have so far is:
for($i = 1; $i < 5; $i++) {
${'extraPhoto_' . $i} = get_field('extra_photo_ . $i');
}
I've tried with an array like this:
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["$extraPhoto_$i"] = get_field('extra_photo_ . $i');
}
Nothing seems to fix my problem. I'v searched on the PHP website (variable variable).
There is some bug in your code which not allowing. Use 'extra_photo_'. $i instead of 'extra_photo_. $i'
for($i = 1; $i < 5; $i++) {
$extraPhoto_.$i = get_field('extra_photo_'. $i);
}
You can build an array as defined below and than just call extract($myfiles) to access them as variables.
Again your syntax for the get field is incorrect you should append $i after the quotes.
$myfiles = array();
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_".$i] = get_field('extra_photo_'.$i);
}
extract($myfiles);
If you want to create dynamic variable, use below code
for ($i = 1; $i < 5; $i++) {
${'extraPhoto_'.$i} = $_POST['extra_photo_'.$i];
}
if you want to assign variables to array use below code.
for ($i = 1; $i < 5; $i++) {
$myfiles["extraPhoto_$i"] = $_POST['extra_photo_'.$i];
/// $myfiles["extraPhoto_$i"] = get_field('extra_photo_'.$i);
}
and than to see if values are assign to new array or not, you can use below code.
echo "<pre>";print_r($myfiles);echo "</pre>";

Simple For Loop Output

for ($i = $field +1; $i < $field2; $i++) {
echo $i.'<br/>';
}
what i'm trying to do here is Echo numbers between the input values field and field2, this works, however i was wondering if there was a more efficient way of doing this (eg changing the $i = $field +1;)
Another using while loop example:
<?php
$i = $field;
while(++$i < $field2) {
echo $i.'<br/>';
}

How to return the object value within for loop?

Okay so i am trying to retrieve the object value iterating through a for loop.
Manually it looks like this:
echo $game->stats->item0;
echo $game->stats->item1;
...
I want to do it something like this:
for($i = 0; $i < 6; $i++) {
echo $game->stats->item.$i;
}
The above however just returns the value of $i. How can i return the actual object value?
Thanks
Basically, your statement should be like this:
echo $game->stats->{'item'.$i}
Regards,
Instead of item.$i, use {"item$i"}.
for($i = 0; $i < 6; $i++) {
echo $game->stats->{"item$i"};
}
Another way you can do it is to set a variable to be equal to 'item' . $i.
for($i = 0; $i < 6; $i++) {
$item = 'item' . $i;
echo $game->stats->$item;
}

PHP variables in a complex way

<?php
$td = date("d");
for ($i = 1; $i <= $td; $i++)
{
$num18 = count($this->numbermonth18);
echo "['1'," . $num18 . "],";
}
The above code will display the output like, ['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],['1',12],.
I need to replace the 18 with $i value in the above code. How can I use $i instead of 18 in the for loop to display all the values of $this->numbermonth1, $this->numbermonth2, $this->numbermonth3 etc. and print the array?
You could do with:
for($i=1;$i<=$td;$i++)
{
$num=count($this->{'numbermonth'.$i});
echo "['1',".$num."],";
}

Categories