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
10 5811
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
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: -
$location="localhost";
-
$username="root";
-
$password="root";
-
$database="smartoffice";
-
-
$conn=mysql_connect("$location","$username","$password");
-
if (!$conn = mysql_connect($location, $username, $password) ) {
-
die(mysql_error());
-
}
-
-
//Select the database
-
-
if (!mysql_select_db($database,$conn) ) {
-
die('Could not select database');
-
-
}
-
-
$res=mysql_query("select * from form") or die(mysql_error());
-
$row=mysql_fetch_array($res);
-
$layout=$row["layout"]
-
__________________________________________________ ______________
If you arnt clear and want me to explain my situation again do let me
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: -
//...
-
// you database fetch code here
-
//...
-
$x = 2;
-
$row = mysql_fetch_assoc($mysql_result);
-
echo str_replace('$x', $x, $row['layout']);
-
If the text in the layout column is: "The value is $x"
then the output will be like this : "The value is 2"
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
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:
$row["template"] should be like $row[0]. Its numeric, to identify the value inside the array.
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.
You can parse using eval,
Here is a sample: -
<?php
-
-
// php is
-
$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"); ?>";
-
-
// turn on output buffering
-
ob_start();
-
// this parses the php code in out $php variable; this is the heart of our templating logic
-
eval(" ?>" . $php . "<? ");
-
// store the final parsed output in a variable $output
-
$output = ob_get_contents();
-
// clear buffer and stop buffering
-
ob_end_clean();
-
-
// this line show the parsed final data in the browser
-
echo $output;
-
-
?>
-
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: -
$name="sumaiya";
-
-
$res=mysql_query("select * from work");
-
$row=mysql_fetch_array($res);
-
-
echo str_replace('$name', $name, $row["template"]);
-
// Notice that I use '$name' and not "$name"
-
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.
Atli 5,058
Expert 4TB
Ok, if I understand correctly, you've dynamically created a form that has PHP variables in it?
Like this: -
<form action="?">
-
<input type="hidden" name="userID" value="$userID" />
-
<input type="hidden" name="userName" value="$userName" />
-
<input type="text" name="someinput" value="Please type some input." />
-
</form>
-
And you need to replace the strings that look like PHP variables with acctual PHP variables?
Would this work for you? -
// Using the form posted above.
-
$form = "This would contain you form html";
-
-
// Get user Info
-
$userID = 1;
-
$userName = "Atli";
-
-
// Create replacement arrays
-
$old = array('$userID', '$userName');
-
$new = array($userID, $userName);
-
-
// Replace and print
-
echo str_replace($old, $new, $form);
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 ";'.
...
|
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...
|
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):
...
|
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:...
|
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: " ....
|
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...
|
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 =...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| |