kaustic Posted May 4, 2007 Share Posted May 4, 2007 I have an SWF passing a querystring to a PHP file. ex: page.php?var=foo&var=bar&var1=3 The querystring contains multiple values with the same key (var in the example above). How can I loop through the "var" key and get its values? I've tried over and over again with no luck. Basically, using the example above, I'd like to loop through var and echo all values (foo and bar). I realize this is probably simple but I'm simply not able to get a loop to work with the querystring. Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/50017-querystring-loop-simple-but-not-getting-it/ Share on other sites More sharing options...
utexas_pjm Posted May 4, 2007 Share Posted May 4, 2007 <?php $qs = $_SERVER['QUERY_STRING']; $q_array = explode('&', $qs); foreach($q_array as $pair){ $p_array = explode('=', $pair); echo $p_array[0] . ' ' .$p_array[1] . '<br />'; } ?> Best, Patrick Link to comment https://forums.phpfreaks.com/topic/50017-querystring-loop-simple-but-not-getting-it/#findComment-245533 Share on other sites More sharing options...
kaustic Posted May 4, 2007 Author Share Posted May 4, 2007 Patrick, that worked. I added an if statement to pull just the var values. Is there a more efficient manner if all I want to extract is the var values? Thanks for the help. I'd tried something similar but never got it to work. Think I've been up too long. Link to comment https://forums.phpfreaks.com/topic/50017-querystring-loop-simple-but-not-getting-it/#findComment-245538 Share on other sites More sharing options...
utexas_pjm Posted May 4, 2007 Share Posted May 4, 2007 Why are you getting data like this: page.php?var=foo&var=bar&var1=3 instead of like this: page.php?var[]=foo&var[]=bar&var1=3 ? The latter affords you the ability to access var through the $_GET array as an array ala: <?php print_r($_GET); // prints: Array ( [var] => Array ( [0] => foo [1] => bar ) [var1] => 3 ) ?> Best, Patrick Link to comment https://forums.phpfreaks.com/topic/50017-querystring-loop-simple-but-not-getting-it/#findComment-245544 Share on other sites More sharing options...
kaustic Posted May 4, 2007 Author Share Posted May 4, 2007 Patrick, I don't have access to the original FLA. So, I have to deal with how the data is being sent.... No choice in the matter. Link to comment https://forums.phpfreaks.com/topic/50017-querystring-loop-simple-but-not-getting-it/#findComment-245559 Share on other sites More sharing options...
utexas_pjm Posted May 4, 2007 Share Posted May 4, 2007 I see. Unfortunately there's not going to be a really elegant solution to the problem, since you're dealing with data that is technically malformed. Parsing the query string manually (like the code I originally posted) is probably your best bet. Patrick Link to comment https://forums.phpfreaks.com/topic/50017-querystring-loop-simple-but-not-getting-it/#findComment-245566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.