Harsh Posted May 2, 2009 Share Posted May 2, 2009 So, I want to use GET to get an ID then add it infront of a variable (name). I believe its possible. eg. if it was $_GET['id'] = 001, then it would print $001_name . I'm just not sure how you do it. My code so far; <?php include('/info.php'); $id = $_GET['id']; echo "<table>"; echo "<tr>"; echo "<td>[" . $id . "] " . $id_name . "</td>; echo "<td>Lvl; " . $id_level . " - HP; " . $id_hp . "</td>"; echo "</tr>"; echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/156541-placing-gets-contents-infront-of-a-variable/ Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 You are talking about variable variables: http://be.php.net/language.variables.variable However you can not make a variable that starts with a number it must be either an underscrore or an alphabetic character create a variable variable like this: <?php $var = "foo"; ${$var} = "bar"; echo $foo; ?> Quote Link to comment https://forums.phpfreaks.com/topic/156541-placing-gets-contents-infront-of-a-variable/#findComment-824251 Share on other sites More sharing options...
shlumph Posted May 2, 2009 Share Posted May 2, 2009 What would happen if $_GET['id'] = I%20hate-programm!ng ? Then what would your variable be? Heh, silly goose. Quote Link to comment https://forums.phpfreaks.com/topic/156541-placing-gets-contents-infront-of-a-variable/#findComment-824256 Share on other sites More sharing options...
Harsh Posted May 2, 2009 Author Share Posted May 2, 2009 Its confusing me a bit. :-\ I have a list already with all the info, listing the $_id_name for tons of IDs. So, can I do something like: <?php $id = $_GET['id']; $_{$id}_name = $_{$id}_name; echo "[" . $id . "] " . $_ + $id + _name; ?> Quote Link to comment https://forums.phpfreaks.com/topic/156541-placing-gets-contents-infront-of-a-variable/#findComment-824262 Share on other sites More sharing options...
premiso Posted May 2, 2009 Share Posted May 2, 2009 Look into arrays. Learn how to use them as they will be a life safer and make everything go smoother. PHP.net Manual for Arrays This is what arrays were made for. Your could would then turn into something like: $id = intval($_GET['id']); // cast to int to prevent weird stuff. foreach ($list[$id] as $item) { echo $item['name'] . " has the id of " . $item['id'] . "<Br />"; } You want to look into the multi-dimensional array. If you want help on setting it up describe how you get your "listing". But if you are going to do it, you might as well not do it the slow half-assed way. Learn arrays and you will not be sorry, as it will make it a ton easier. Quote Link to comment https://forums.phpfreaks.com/topic/156541-placing-gets-contents-infront-of-a-variable/#findComment-824275 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.