Jump to content

PHP Coding help -getting variable name from variable


Ramamoorthy

Recommended Posts

Hi guys .

I have came up here with one horrible question. I ve been searching around the web for more than 2 weeks and can't get the answer ..

 

here the codings:

Index.php

<?php require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
echo $varone_latest; echo"<br />";
echo $varone_latest_string;?>

db.php

<?php 
$varone_1234 = "1234th variable"
$varone_1234_string = "this is the 1234 th line"; 
/* and more 100 variables related to varone_1234*/




$varone_latest = $varone_1234;
?>

now in the index.php 

the first echo $varone_latest; prints the right string . but the second echo $varone_latest_string; doesn't gives and it will give error of undefined variable ..

now my problem I want to print the value of $varone_1234_string; in Index.php instead of $varone_latest_string;

I know it is not possible . but I have came up with idea..which is .

first the php have to determine what variable assigned to $varone_latest .

answer will be $varone_1234 .. and then it has to join remaining " _string" and have to convert it as variable .. then it has to print ..

How to do that ...? I hope I am not clear in my question . so any help me to build the question well..
 

 

example in javascript used by Disqus.com

var disqus_shortname = 'filedelivery';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';

here shortname is filedelivery .. and deafult url for loading is .disqus.com/embed.js  .. the js embeds shortname and default url and makes it as a url and then loads the file from the created url ..

 

Note: this is just the example that I have gave in js .. anyway to achieve it in php ..?

 

 

PHP does have a feature called variable variables. Which is where you use a value from one variable to define the name of another variable.
Your javavscript example is using the value of the disqus_shortname variable and concatenating it into the string defined for dsq.src which is not the same thing as the problem you'e describing.
 
You could do this in db.php

<?php 
$varone_1234 = "1234th variable"
$varone_1234_string = "this is the 1234 th line"; 
/* and more 100 variables related to varone_1234*/

...

$latest = '1234';       // define the latest variable number
                        // variable variables used below
$varone_latest        = ${'varone_'.$latest}
$varone_latest_string = ${'varone_'.$latest.'_string'}
?>

But if you are storing data this way you are better of using a database.

i've just reviewed your threads and you are making life hard for yourself (i'm pretty sure that programming is not intended to be a painful masochist activity.)

 

you are making a wall of hundreds and perhaps thousands of variables that take a huge amount of time to create in the first place, to change, or to add anything to.

 

this can all be replaced with a few lines of php code, if that code is using a database. in a fraction of the time you have spent trying to get just a small part of your your current scheme to work, you could have learned enough to fully use a database to do this.

 

perhaps an example. your 1234 value is a key/index to your data.

// make a database connection/select the database here (using the mysqli database extension)
$mysqli = new mysqli(.... connection details here....);

$key_value = 1234; // from wherever your input value is coming from now
$query = "SELECT * FROM your_table WHERE id = $key_value"; // form the query statement
$result = $mysqli->query($query); // run the query

// use the query result object in $result any way you want ...
// assuming you expect one matching row from the query (no loop needed) -
 
$row = $result->fetch_assoc();

// $row is now an array holding the columns of data you stored in your_table for the key_value 1234.

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.