Using variables in a mySQL text block

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bittman
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2004 4:17 pm

Using variables in a mySQL text block

Post 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 :)
bittman
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2004 4:17 pm

Post 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;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Whats wrong with it?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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"];
?>
bittman
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2004 4:17 pm

Post by bittman »

Without eval () the variables will not print their value... they will just print the variable name (e.g. $headline_begin )
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
bittman
Forum Newbie
Posts: 4
Joined: Sat Feb 07, 2004 4:17 pm

Post 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.
Post Reply