Page 1 of 1

Using variables in a mySQL text block

Posted: Sat Feb 07, 2004 4:17 pm
by bittman
I have defined the following variables on my page and would like their values to appear in text that I'm pulling from a mySQL db

$headline_begin = "<span class=\"headline\">";
$headline_end = "</span><P>";

I'm using the following to retrieve the text block from the db:

$result = mysql_query("SELECT * FROM content_pages ORDER BY id ASC");

while ($row = mysql_fetch_array($result)) {

echo $row["text"]; }


The actual text in the database is the following:

$headline_begin Lorem ipsum dolor sit amet $headline_end Consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

I'm trying to simplify text formatting for non-html users by using variables to apply CSS styles...

Thanks for your help :)

Posted: Sun Feb 08, 2004 11:49 am
by bittman
I've modified the call to the db...but here is some code that works using eval():

$result = mysql_query("SELECT * FROM content_pages ORDER BY id ASC");

$row = mysql_fetch_array($result);
$content = $row["text"];
eval("\$content = \"$content\";");
print $content;

Posted: Sun Feb 08, 2004 12:51 pm
by John Cartwright
Whats wrong with it?

Posted: Sun Feb 08, 2004 3:38 pm
by DuFF
Whats wrong with just doing . . .

Code: Select all

<?php
$result = mysql_query("SELECT * FROM content_pages ORDER BY id ASC");

$row = mysql_fetch_array($result);
echo $row["text"];
?>

Posted: Sun Feb 08, 2004 10:02 pm
by bittman
Without eval () the variables will not print their value... they will just print the variable name (e.g. $headline_begin )

Posted: Mon Feb 09, 2004 5:54 am
by DuFF
Ahhh, I understand now.

The easier way would probably been to have a "headline" field and then a "body" field for the rest of the text.

Posted: Mon Feb 09, 2004 8:36 am
by bittman
Yeah...but I'm also using variables for formatting body text, links, etc. and will have more than one headline in a text block.