yami007 Posted November 29, 2009 Share Posted November 29, 2009 I thought this would bring me a cool result and be done but <?php foreach($_GET as $keyname => $value) { echo implode(" , ", "$keyname=$value"); } ?> this what it displays : Warning: implode() [function.implode]: Bad arguments. in C:\AppServ\www\test\link.php on line 17 i know why it dosnt work, but dont know how to get it to work!! Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/ Share on other sites More sharing options...
.josh Posted November 29, 2009 Share Posted November 29, 2009 care to explain what it is you are trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967219 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 care to explain what it is you are trying to do? well, i'm trying to get the query strings from the url and produce a new link. for example, the actual link is index.php?action=news&article=1 with my code i want to output something like this : action=news&article=1 but it's not working !! Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967222 Share on other sites More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 The implode function "implodes" an array into a string seperated by the "glue" given in the first argurment... The second argument required for the implode function is the array of values you wish to implode. You are getting the error because you are passing a string... Also; why is your first arguement ' , ' when your desired result is seperated by ampersands? $strQuery = ''; foreach ($_GET as $k => $v): $strQuery .= $k . '=' . $v . '&'; endforeach; $strQuery = substr($strQuery, -1); That should give the desired result. //EDIT If you wish to have the leading questionmark change $strQuery = ''; //to $strQuery = '?'; Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967223 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 Alternatively you could use httpquerystring. foreach($_GET as $key => $val) $vars[] = "$key=$val"; echo implode('&', $vars); Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967224 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 $strQuery = ''; foreach ($_GET as $k => $v): $strQuery .= $k . '=' . $v . '&'; endforeach; $strQuery = substr($strQuery, -1); what do i echo here?? sorry but i dont quite understand Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967226 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 Alternatively you could use httpquerystring. foreach($_GET as $key => $val) $vars[] = "$key=$val"; echo implode('&', $vars); well this is what it displays : topic=2500topic=2500&article=science there's only 1 key "topic" why is it multiplied?? Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967228 Share on other sites More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 You echo the $strQuery variable. Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967229 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 You echo the $strQuery variable. I did and this is the output: &topic=2500&topic=2500article=science dont know why it is duplicating the first key, and why does the symbol & appears at the beginning of the string!!! Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967230 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 It shouldn't do that. Are you sure you're not echoing out the first variable previously? Or have it twice in your url?.. Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967233 Share on other sites More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 echo '<pre>' . print_r($_GET, true) . '</pre>'; Please run that as post the output back. Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967235 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 echo '<pre>' . print_r($_GET, true) . '</pre>'; Please run that as post the output back. OK this what it ouptuts : Array ( => 2500 [article] => science ) Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967236 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 It shouldn't do that. Are you sure you're not echoing out the first variable previously? Or have it twice in your url?.. Yes i am sure, i tried your code independtly on a file like this : <?php foreach($_GET as $key => $val): $vars[] = "$key=$val"; echo implode('&', $vars); endforeach; ?> with nothing else and poited the url to test.php?id=1&name=john and it still multiplies the firs key. Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967237 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 That's not my code.. You tried to add an unnecessary endforeach; to my code and put it in the wrong place which is what was making it not work. <?php foreach($_GET as $key => $val) $vars[] = "$key=$val"; echo implode('&', $vars); Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967238 Share on other sites More sharing options...
.josh Posted November 29, 2009 Share Posted November 29, 2009 $url = "http://www.somesite.com/somepage.php?x=123&y=456&z=789"; echo parse_url($url, PHP_URL_QUERY); Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967244 Share on other sites More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 lol never thought, echo $_SERVER['QUERY_STRING']; Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967246 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 That's not my code.. You tried to add an unnecessary endforeach; to my code and put it in the wrong place which is what was making it not work. <?php foreach($_GET as $key => $val) $vars[] = "$key=$val"; echo implode('&', $vars); Oh thanks, it worked thanks everyone : ) Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967248 Share on other sites More sharing options...
.josh Posted November 29, 2009 Share Posted November 29, 2009 lol never thought, echo $_SERVER['QUERY_STRING']; haha yeah i just remembered that, was fixin' to post that just now till I saw your post Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967249 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 lol never thought, echo $_SERVER['QUERY_STRING']; haha yeah i just remembered that, was fixin' to post that just now till I saw your post yes i know it works with server global lol but what drove me to all this trouble is that i wanted to exclude a key for example : i am on the page articles.php?id=1245&title=new+beginning&lang=en here i only want to get id=1245&title=new+beginning and add lang=en or lang=sp statically os i can change the language. that's the ponit : ) i really appreciate your help, guys : ) Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967250 Share on other sites More sharing options...
Alex Posted November 29, 2009 Share Posted November 29, 2009 lol never thought, echo $_SERVER['QUERY_STRING']; Lol, I forgot about that too.. I was pretty sure there was something like that, I went to test it and I must've typed it in wrong. Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967251 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 lol never thought, echo $_SERVER['QUERY_STRING']; Lol, I forgot about that too.. I was pretty sure there was something like that, I went to test it and I must've typed it in wrong. it really suprised me the way u did with foreach, i never knew that, still newbie lol Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967252 Share on other sites More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 IF the lang value is always a 2 letter country code you could use echo substr( $_SERVER['QUERY_STRING'], 0, (strlen($_SERVER['QUERY_STRING']) - ); //edit That code also relies on the lang=?? being the last part of the query string... Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967254 Share on other sites More sharing options...
yami007 Posted November 29, 2009 Author Share Posted November 29, 2009 IF the lang value is always a 2 letter country code you could use echo substr( $_SERVER['QUERY_STRING'], 0, (strlen($_SERVER['QUERY_STRING']) - ); Yeah i'm actually working with just 3 languages, en, fr and ar lol i will work with this code better thanks //edit That code also relies on the lang=?? being the last part of the query string... Quote Link to comment https://forums.phpfreaks.com/topic/183260-implode-function-with-foreach/#findComment-967257 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.