Variables inside parameter names

One of my page has 5 parameters in URL.
Let’s suppose they are “a1=1&a2=2&a3=3&a4=4&a5=5.”

With the code below, I can get my target result below.

[b]php code[/b]
echo $_GET['[COLOR="blue"]a[/COLOR][COLOR="Red"]1[/COLOR]'];
echo $_GET['[COLOR="blue"]a[/COLOR][COLOR="red"]2[/COLOR]'];
echo $_GET['[COLOR="blue"]a[/COLOR][COLOR="red"]3[/COLOR]'];
echo $_GET['[COLOR="blue"]a[/COLOR][COLOR="red"]4[/COLOR]'];
echo $_GET['[COLOR="blue"]a[/COLOR][COLOR="red"]5[/COLOR]'];

[b]target result[/b]
1
2
3
4
5

However, I like to use WHILE so I made the code below which doesn’t work correctly.

[b]code[/b]
$whileIindex = 1
WHILE($whileIindex < 6 )
{
echo $_GET['{[COLOR="Red"][COLOR="Blue"]a[/COLOR].$whileIindex[/COLOR]}'];

$whileIndex++;
}

How can I make it work correctly?
How can I get my target result with WHILE?

If you changed them to a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5, $_GET[‘a’] would be a regular array which you could iterate over.

Sterling has the right idea though it may complicate any javascript you happen to be doing. But to directly answer your original question, this should do the trick:


error_reporting(E_ALL);
$whileIndex = 1
while($whileIndex < 6 )
{
echo $_GET['a' . $whileIndex];

$whileIndex++;
}

Syntax and spelling is important.

If your incoming get string is ?a1=1 then the value of $_GET[‘a1’] is already 1, which is what you say you want?

The value of a1 is not always 1. It could be 8 or another number.

I can change them to a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5 with the code below.

[b]code[/b]
$myArray=str_replace("a","a[",$string);
$myArray=str_replace("=","]=",$myArray);
echo $myArray;

[b]result[/b]
`a[1]=1&a[2]=2&a[3]=3&a[4]=4&a[5]=5`

Now how can I get my target result?

The trial code below causes an error;


$whileIndex = 1
while($whileIndex &lt; 6 )
{
echo $myArray[$whileIndex];

$whileIndex++;
}  

The trial code below doesn’t work correctly either.


$myArray=str_replace("a","a[",$string);
$myArray=str_replace("=","]=",$myArray);
echo $_GET['myArray']."<br>";

If your incoming get string is ?a1=8 then the value of $_GET[‘a1’] is already 8

Now are you trying to retrieve the value 1 or 8 in this particular example case?

Yes, it is true.

The code below can get the values of a1, a2, a3, a4, and a5.

echo $_GET['a1'];
echo $_GET['a2'];
echo $_GET['a3'];
echo $_GET['a4'];
echo $_GET['a5'];

I like to get the the values of a1, a2, a3, a4, and a5 with WHILE.
The reason why I want to get the values of a1, a2, a3, a4, and a5 with WHILE is I don’t know how many a parameters is in the url.

I hope the code below which doesn’t work correctly shows why I like to get the values of a1, a2, a3, a4, and a5 with WHILE.


$string=$_SERVER['QUERY_STRING'];
$delimiterCount=strlen($string)-strlen(str_replace("&","",$string) );

$whileIndex = 1; 
while($whileIndex < $delimiterCount+2)
{
echo  $_GET['{A.$whileIndex}']."<br>";
$whileI++;
}

Look at Post #3. I gave you the correct code. The rest of the folks are trying to show you how you should do it. I showed you how to do it your way.

Yes, your code had it. Thank you.

echo $_GET['[COLOR="Red"]a[/COLOR]' . $whileIndex];

Glad to of help.

Before you move on though, take a closer look at post #2. That is really how you should be dealing with this. It will save you trouble down the line.

In your html you currently have something like:
<input name=“a1” value=“1” />
<input name=“a2” value=“2” />
Change it to:
<input name=“a[1]” value=“1” />
<input name=“a[2]” value=“2” />

Now you can:
$a = $_GET[‘a’];
foreach($a as $key => $value)
{
echo $key . ’ ’ $value;
}
No need for all this index stuff.

Or here’s also an option:

foreach($_GET as $Gkey => $Gvalue){

if(substr($Gkey,0,1)=="a"){

echo "".$Gkey." =>> ".$Gavalue."<br/>\
";

}

}