Jump to content

Placing GETs Contents infront of a Variable


Harsh

Recommended Posts

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>";

?>

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;
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.