Jump to content

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

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

 

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.