Eruondo Posted January 31, 2010 Posted January 31, 2010 I'm trying to write a little snippet of code, and I've encountered a problem. What I'm trying to do, is to put a variable with a value inside the $_GET, e.g: $b = 1; $c = $_GET['data{$b}']; And later I use a while loop to auto increment $b + 1 for so and so many times. My problem is, I can't get this to work! It simply doesn't read the variable, no matter what I try. Does anyone have any suggestions on how to fix this? Thank you very much. Quote
Zeggy Posted January 31, 2010 Posted January 31, 2010 Any one of these should work: $c = $_GET['data' . $b]; $c = $_GET["data$b"]; Quote
Dayo Posted January 31, 2010 Posted January 31, 2010 what i think he is trying to do is this $b = array('1a', '2b', '3c', '4d', '5e'); //put the data into an array $c = $_GET['data-'.implode(',', $b)]; // make the array a string and add "data-" before it //then to get the info $data=explode('-', $c); //makes $c into an array with data as $c[0] and your data string as $c[1] (to add more data just add "-" then your string) $data=explode(',', $data[1]); // remakes the origanal array from the string echo $data[0]; //returns 1a this is not tested but hope it helps :) Quote
Dayo Posted February 1, 2010 Posted February 1, 2010 lol i just saw what i wrote lack of sleep and a 14 hour shift does hell for my coding :P it should be $b = array('1a', '2b', '3c', '4d', '5e'); //put the data into an array $_GET['data]=implode(',', $b); // make the array a string and assign it ot $_GET['data'] $c=$_GET['data']; //then to get the info $data=explode('-', $c); //makes $c into an array with data as $c[0] and your data string as $c[1] (to add more data just add "-" then your string) $data=explode(',', $data[1]); // remakes the origanal array from the string echo $data[0]; //returns 1a Quote
Zeggy Posted February 1, 2010 Posted February 1, 2010 lol i just saw what i wrote lack of sleep and a 14 hour shift does hell for my coding :P it should be $b = array('1a', '2b', '3c', '4d', '5e'); //put the data into an array $_GET['data]=implode(',', $b); // make the array a string and assign it ot $_GET['data'] $c=$_GET['data']; //then to get the info $data=explode('-', $c); //makes $c into an array with data as $c[0] and your data string as $c[1] (to add more data just add "-" then your string) $data=explode(',', $data[1]); // remakes the origanal array from the string echo $data[0]; //returns 1a lol I'm also wondering how you got this problem to solve from looking at his topic... Nothing is said about arrays, 1a 2b etc., or the format of his data... :P Quote
Dayo Posted February 1, 2010 Posted February 1, 2010 yes but it is a better way to pass info thro varables rather then making 20 difftrent $_GET varables Quote
Zeggy Posted February 1, 2010 Posted February 1, 2010 I don't think so... You have the added processing/time penalty of having to implode/explode variables, compared to using GET variables normally. Still, when faced with this situation it would be better to not use GET variables at all... Quote
Eruondo Posted February 5, 2010 Author Posted February 5, 2010 Thanks both of you, that worked, :) Sorry, I forgot to answer, :P Quote
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.