|
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-03-2001 18:44
I have a PHP template that grabs data from a MySQL database and populates some fields. One thing I've noticed is that I can't use PHP code in the content that's in the database. It doesn't get parsed. For instance - I have a page of content that has some links to other pages within the same site. I wanted to have the links be formatted as:
<a href="<?=$php_self ?>?article=1">blah</a>. But this ends up being a link like:
http://www.domain.com/<?=$php_self ?>?article=1
If that's just the way it goes, then I can compensate. But I wasn't planning on this, so if there is something that I'm missing, I'd greatly appreciate any help.
Thanks!
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-03-2001 19:11
You should write only $PHP_SELF to the database (without <?=?> parts) and then after fetching the value run it through eval() function...
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-04-2001 03:55
Well, that works, but I have a strange anomaly. I am using this:
<?
$newbody = $row["body"];
print(eval("?>$newbody"));
?>
The eval function runs correctly, but right after the content, it inserts a "1". Every page, every time. I've verified said "1" does not exist in my database record, nor in my template that calls it. Nor are there any typos in the template or db record. It clearly inserts this right after the very last character from the $newbody variable.
If I take out the eval part, the $newbody gets printed, and no "1". But, obviously, my data does not get parsed.
A simple eval test page mimicks the php documentation example, and works fine.
Ideas?
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-04-2001 09:35
Try this:
eval("\$row[body] = \"" . str_replace("\"", "\\\"", $row[body]) . "\";");
And then use simply:
print($row[body]);
If you still keep getting 1 at the end, post the whole $row[body] as it is stored in the database...
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-04-2001 15:41
Ok, Max - why does that work? I'm trying to understand that line, but it's not making sense to me? It seems to have fixed the problem, but I just want to understand it better.
Thanks!
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-04-2001 16:33
That line basically says this:
$row[body] = "text that you entered in <textarea> field before adding it to database";
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-04-2001 20:39
Hmm....ok. I'll take your word for it. I can't say I understand the way it works in your example, but I accept the fact that it does work and solves my problem. Maybe I'll understand it soon...
Thanks!
Me thinks you've got the entire PHP manual memorized. I need a search engine for your brain!
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-04-2001 20:54
|
butcher
Paranoid (IV) Inmate
From: New Jersey, USA Insane since: Oct 2000
|
posted 08-04-2001 23:35
LOL
- Resolutions, Of All My Fruitless Searches -
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-08-2001 17:16
Okay. I seem to have spoken too soon. I've noticed that the Mr. Max solution doesn't work. (Alright, now that the world has stopped spinning on its axis after I said that.....)
My original solution, print(eval("?>$row[body]\n"));
does parse the $row[body] field correctly, but I get a stupid "1" after the text. An example can be seen here. Notice just above the This article has been read... line, you'll see the "1".
The $row[body] field, grabed from MySQL, contains the following:
Welcome to the official web site of the Macomb County Sheriff's Office. We invite you to browse through our pages and learn about the many services we provide the citizens of Michigan's third largest county.
<br>
<br>
Our goal is to be more interactive with the people we serve. We are continuously seeking ways to improve our organization and to building upon our relationships within the community. We anticipate that this site will provide another means for our agency to better communicate with you.
<br>
<br>
Law enforcement is becoming more technologically advanced. We are facing new and exciting challenges as we deal with crime and other concerns within our community. As your Macomb County Sheriff, I promise to keep you better informed through this web site of our efforts to reduce crime and to provide easier access to those services we provide.
<br>
<br>
Thank you for stopping by and checking out our web site.
<br>
<br>
<a href="<?=$php_self ?>?article=22">Sheriff Mark A. Hackel</a>
<br>
The select statement is "Select *....", so I'm grabbing everything, and there is no string manipulation before or after the print(eval("?>$row[body]\n")); statement.
I have verified that the "1" gets inserted right after this line executes, and before anything else in the template (in this case, the very next line that gets printed is <p>This article has been read: <b><? printf("%s\n", $row["page_counter"]); ?></b> times</p>
If you browse some of the other pages, you'll notice that it appears on most pages (although not always on a line of its own, based on the page itself).
Ideas?
[This message has been edited by Pugzly (edited 08-08-2001).]
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-08-2001 17:40
I forgot to mention that Mr. Max's solution yields the desired results (no "1"), but the PHP doesn't get parsed correctly, I end up with a link like:
/<?= ?>?article=2 instead of /article.php?article=2
[This message has been edited by Pugzly (edited 08-08-2001).]
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-08-2001 17:42
What was the problem? Did you type the number 1 in one of your print() statements?
Also, what was wrong with my solution? I've tested it in my weblog/news PHP script and it worked fine...
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-08-2001 17:45
We've both replied at the same time. Actually, your solution messed up the PHP tags. But it did get the "1" out of there. Maybe I should have mentioned I needed a solution that didn't mangle my code
My version: http://www.development.macombsheriff.com/article.php?article=2
Your version: http://www.development.macombsheriff.com/article2.php?article=2 (Check the link)
[This message has been edited by Pugzly (edited 08-08-2001).]
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-08-2001 18:11
Pugzly, you didn't read my reply completely. I said that you should write only $PHP_SELF to the database (without <?=?> parts)...
Your line from db should look like this:
<a href="$php_self?article=22">Sheriff Mark A. Hackel</a>
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-08-2001 18:26
Realizing he has once again made a total @ss of himself, he says
"Oh. Well, that DOES work now, doesn't it....." and scurries off before the public laughter begins......
Thanks. I gotta quit do that "manager's reading" of the the replies, and slow down and actually THINK about it. Novel concept, I know.....
|
Pugzly
Paranoid (IV) Inmate
From: 127.0.0.1 Insane since: Apr 2000
|
posted 08-08-2001 18:55
However, this does raise another question...
If I don't use opening and closing tags within that content, how can I insert PHP code other than simple strings? If I wanted to insert a MySQL query with some PHP functionality, is that still possible? Just a thought.
|
mr.maX
Maniac (V) Mad Scientist
From: Belgrade, Serbia Insane since: Sep 2000
|
posted 08-08-2001 19:55
It can be done, but this breaks the point of content & code separation...
|