473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Mysql PHP echo variable inside variable

sumaiya
43
The problem is explained below:

1- on a php page i have $x=2;
2- there is a mysql table with a field called "layout"
3- The data in "layout" field = My problem number is $x

4- Now what i want is when i do echo $layout it shoud display

My problem number is 2

but it is displaying

My problem number is $x

Can you help me here. It is very important just to simplify my problem i have explained it like that in reality i have quite a complicated scenario.

Please reply back. This is my 3-4 question but never receive any replies from your side.

Regards,
Sumaiya
www.phpjavascript.com
Jun 11 '07 #1
10 5811
nomad
664 Expert 512MB
The problem is explained below:

1- on a php page i have $x=2;
2- there is a mysql table with a field called "layout"
3- The data in "layout" field = My problem number is $x

4- Now what i want is when i do echo $layout it shoud display

My problem number is 2

but it is displaying

My problem number is $x

Can you help me here. It is very important just to simplify my problem i have explained it like that in reality i have quite a complicated scenario.

Please reply back. This is my 3-4 question but never receive any replies from your side.

Regards,
Sumaiya
www.phpjavascript.com
How are you connecting to the db. can you show use the connection.

nomad
Jun 11 '07 #2
sumaiya
43
How are you connecting to the db. can you show use the connection.

nomad
Actually it is a huge and complicated program. To simplify it I had explained it the way i did.

Your Ques: How am I connecting to the database?
Answer:
Expand|Select|Wrap|Line Numbers
  1. $location="localhost";            
  2. $username="root";        
  3. $password="root";            
  4. $database="smartoffice";        
  5.  
  6. $conn=mysql_connect("$location","$username","$password");
  7. if (!$conn = mysql_connect($location, $username, $password) ) {
  8.         die(mysql_error());
  9.     }
  10.  
  11. //Select the database
  12.  
  13.     if (!mysql_select_db($database,$conn) ) {
  14.         die('Could not select database');
  15.  
  16.     }
  17.  
  18. $res=mysql_query("select * from form") or die(mysql_error());
  19. $row=mysql_fetch_array($res);
  20. $layout=$row["layout"]
  21.  
__________________________________________________ ______________

If you arnt clear and want me to explain my situation again do let me
Jun 12 '07 #3
Atli
5,058 Expert 4TB
Hi.

The "$x" in the $row['layout'] string is not a variable reference.

By that I mean, PHP does not try to look for variables inside text you fetch from your database. It only looks for them in text you "hard-code" into the source.

You will have to replace it with the value you want displayed.

For example:
Expand|Select|Wrap|Line Numbers
  1. //...
  2. // you database fetch code here
  3. //...
  4. $x = 2;
  5. $row = mysql_fetch_assoc($mysql_result);
  6. echo str_replace('$x', $x, $row['layout']);
  7.  
If the text in the layout column is: "The value is $x"
then the output will be like this : "The value is 2"
Jun 16 '07 #4
sumaiya
43
Still isnt working............ i ll explain my problem again, help me....... this is so important

I will explain through a simple example.

$location="localhost";
$username="root";
$password="";
$database="intranet";

$conn=mysql_connect("$location","$username","$pass word");

}

<?
$name="sumaiya";

$res=mysql_query("select * from work");
$row=mysql_fetch_array($res);

echo $row["template"];

?>

And table 'work' has a field called 'template' where value stored is "$name"

********* Table Work *****************
|-------------template-----------------|
|------------- $name ------------------|

so echo $row["template"]; should display Sumaiya
Aug 7 '07 #5
dafodil
392 256MB
Still isnt working............ i ll explain my problem again, help me....... this is so important

I will explain through a simple example.

$location="localhost";
$username="root";
$password="";
$database="intranet";

$conn=mysql_connect("$location","$username","$pass word");

}

<?
$name="sumaiya";

$res=mysql_query("select * from work");
$row=mysql_fetch_array($res);

echo $row["template"];

?>

And table 'work' has a field called 'template' where value stored is "$name"

********* Table Work *****************
|-------------template-----------------|
|------------- $name ------------------|

so echo $row["template"]; should display Sumaiya
Is this another problem?

You should declare $row as an array before you use it and you cannot put a name inside [ ].
Here's a sample of declaring an array:
Expand|Select|Wrap|Line Numbers
  1. $row = array();
  2.  
$row["template"] should be like $row[0]. Its numeric, to identify the value inside the array.
Aug 7 '07 #6
sumaiya
43
Thanks for your help, but you arnt getting my problem. No it is the same problem.

I tried using mysql_fetch_assoc() which was suggested before, but that didnt work.

I know that PHP treats data extracted from a database as plain text, it does not parse it. I am storing variable names along with html formatting in my mysql table and i want php to parse these variables and display variables values.
Aug 8 '07 #7
dafodil
392 256MB
You can parse using eval,
Here is a sample:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     // php is
  4.     $php = "this is the text with php code in it. right now date & time is <? echo date ("l dS of F Y h:i:s A"); ?>";
  5.  
  6.     // turn on output buffering
  7.     ob_start();
  8.     // this parses the php code in out $php variable; this is the heart of our templating logic
  9.     eval(" ?>" . $php . "<? ");
  10.     // store the final parsed output in a variable $output
  11.     $output = ob_get_contents();
  12.     // clear buffer and stop buffering
  13.     ob_end_clean();
  14.  
  15.     // this line show the parsed final data in the browser
  16.     echo $output;
  17.  
  18. ?> 
  19.  
Aug 8 '07 #8
Atli
5,058 Expert 4TB
...
<?
$name="sumaiya";

$res=mysql_query("select * from work");
$row=mysql_fetch_array($res);

echo $row["template"];

?>
...
so echo $row["template"]; should display Sumaiya
As I said before, the value returned by the database is NOT evaluated as a variable, but rather as a string.

What you need to do is replace $name with the value you want like so:
Expand|Select|Wrap|Line Numbers
  1. $name="sumaiya";
  2.  
  3. $res=mysql_query("select * from work");
  4. $row=mysql_fetch_array($res);
  5.  
  6. echo str_replace('$name', $name, $row["template"]);
  7. // Notice that I use '$name' and not "$name"
  8.  
Aug 8 '07 #9
sumaiya
43
I understand what you are saying but the thing is i need to store variables in the DB and extract them at run time.

The reason i have such a requirement is because i want to store a form layout in my database.

I have a web based software for my office and one of its module deals with request forms. It goes like this..

An employee logs in the system, select lets say "leave application form" -> submits info like "leave date", "reason"++ -> a form is produced with the following:

1 - Predefined form layout
2 - leave application details ("leave date", "reason")
3 - Employee information previously stored in the database (name, designation, department++)

************************************************** *********************
Example:

Smarthome
Berlington USA

Sub: Leave application

Date: 3-3-07

I Martha from accounts department is applying for annual leave starting from from 4th July to 10th Aug.
..................
............
************************************************** **************************************


Now i have a PHP page where this form layout is hard coded and the employee information like name++ is extracted from the DB and integrated with the form layout. And everything works fine.

BUT i want this hardcoded form layout along with the variables name to be stored in another table, so that it is editable from the system's interface.

When i try to do this the html formatting works fine but the variables names as you said are treated like strings not variables.

So can you tell me a way through which i can force it to be treated as variables, and if there is no such way can you suggest an alternative which would fit in here.
Aug 11 '07 #10
Atli
5,058 Expert 4TB
Ok, if I understand correctly, you've dynamically created a form that has PHP variables in it?
Like this:
Expand|Select|Wrap|Line Numbers
  1. <form action="?">
  2.   <input type="hidden" name="userID" value="$userID" />
  3.   <input type="hidden" name="userName" value="$userName" />
  4.   <input type="text" name="someinput" value="Please type some input." />
  5. </form>
  6.  
And you need to replace the strings that look like PHP variables with acctual PHP variables?

Would this work for you?
Expand|Select|Wrap|Line Numbers
  1. // Using the form posted above.
  2. $form = "This would contain you form html";
  3.  
  4. // Get user Info
  5. $userID = 1;
  6. $userName = "Atli";
  7.  
  8. // Create replacement arrays
  9. $old = array('$userID', '$userName');
  10. $new = array($userID, $userName);
  11.  
  12. // Replace and print
  13. echo str_replace($old, $new, $form);
  14.  
Aug 11 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark | last post by:
A beginner in this area, I have been able to read a record from a MySQL database and populate an HTML form (wow!). Now, my goal is to allow the user to edit the contents of the form and then...
1
by: Agathe | last post by:
Bonjour, Je souhaite insérer dans une table MySQL des données provenant d'un fichier texte grâce à un script PHP. Mon fichier porte l'extension "txt" et les données sont séparées par des ";'. ...
2
by: Prabu Subroto | last post by:
Dear my friends... I am trying to develop a database application with PHP Version 4.3.2, MS Window 2000, MySQL 4.0.13-nt and Apache 2. I tried to insert a record onto my MySQL but I got this...
1
by: Micke | last post by:
I have a problem with string insert that I hope someone can help me with. I'm doint a one-line insert into my database from an other program and it works perfect (thanks to Bill Karwin): ...
3
by: auron | last post by:
Hi there, I have a really stupid and banal problem with showing the results of a MySQL query in PHP, preciselly with MySQL count() function that gives to a variable in PHP the result. NOTE:...
9
by: Petr Vileta | last post by:
Hi, I'm new here and excuse me if this question was be here earlier. I have a simple code <html><body> <?php <?php $link = mysql_connect("localhost", "user", "password") or die("Grr: " ....
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
4
ddtpmyra
by: ddtpmyra | last post by:
Im having trouble on updating the file inside mysql database using the php. Should I use <form> or im wrong on how I called the variable from previous page? FORM 1 - that accepts the file pointer...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.