Author Topic: PHP within PHP?  (Read 1113 times)

0 Members and 1 Guest are viewing this topic.

Offline nyc_fanTopic starter

  • Irregular
  • Posts: 3
    • View Profile
PHP within PHP?
« on: July 10, 2006, 01:43:33 PM »
Hi,

I really hope one of you nice people will be able to help as i've searched and searched for the answer to this question, but i just can't figure it out!

I have a mysql database which contains the main content of pages for my website.

So, when i need to select a page, the script finds the right row in the table and outputs it to display for the user. To display the text that is retrieved from the database, i use this normal bit of code -

<?php   echo($dbarray['page']);   ?>

This all works fine, and if i include HTML within the 'page' field of the database, this is output to the page, so i can make things bold etc.

However, i want to be able to use a PHP script within the data that is retrieved from the database. For example, as part of the page text, i want to be able to use the users name, which is set in the main document. However, when i use this code -

<?php   echo($username);   ?>

as part of the page text that is saved in the database, nothing hapens - there's just white space on the page!

People have suggested i use this code instead -

<' . "? echo($username) ?" . '>';

however all this does is output the whole PHP code to the screen, and the code isn't actually processed?

I hope this all makes sense, and if anyone has the answer as to how i can get this to work, it would be greatly appreciated.

Thanks in advance guys!

Offline ShogunWarrior

  • Devotee
  • Posts: 533
  • Gender: Male
  • # IRELAND #
    • View Profile
    • David Doran Media
Re: PHP within PHP?
« Reply #1 on: July 10, 2006, 02:36:47 PM »
When you echo something it is sent straight to the browser, code will not be excuted unless you eval it.
You could do this: have placeholders in the database pages like {user}, and then replace them with the real variable values like so:
Code: [Select]
$page  = str_replace('{user}',$username,$dbarray['page']);
echo ($page);
<a href="http://www.daviddoranmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddoranmedia.com/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

Offline nyc_fanTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: PHP within PHP?
« Reply #2 on: July 10, 2006, 03:49:11 PM »
ShogunWarrior, to be honest, this wasn't the response i was looking for - simply because it has caused me more work! and i just wanted to do what i guess is impossible!

However, your response has worked a treat and i've now got my page looking as it should.

Thanks very much!

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: PHP within PHP?
« Reply #3 on: July 10, 2006, 04:02:37 PM »
Are you looking for something like this?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline ShogunWarrior

  • Devotee
  • Posts: 533
  • Gender: Male
  • # IRELAND #
    • View Profile
    • David Doran Media
Re: PHP within PHP?
« Reply #4 on: July 10, 2006, 04:16:36 PM »
The code you posted would only work if it was inline in a page, the best thing to do is use placeholders otherwise you'll have to find each piece of PHP in the page and eval them one by one, reinserting the result.
<a href="http://www.daviddoranmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddoranmedia.com/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

Offline redarrow

  • Freak!
  • Posts: 8,150
  • Gender: Male
  • PHP IS FOR LIFE!
    • View Profile
    • my free dating site (a blind php programmer!)
Re: PHP within PHP?
« Reply #5 on: July 10, 2006, 04:28:23 PM »
there you go a nice example php within html.



<?

$name="redarrow";
$age="31";
$comment="i love php";

?>

<html>
<head>
<body>

<table border="4">
<td>
<? echo $name; ?>
</td>
<td>
<? echo $age; ?>
</td>
<td>
<? echo $comment; ?>
</td>
</form
</td>
</html>
</body>
</head>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.ath.cx/paypal_info/paypal1_info.inc

Offline ShogunWarrior

  • Devotee
  • Posts: 533
  • Gender: Male
  • # IRELAND #
    • View Profile
    • David Doran Media
Re: PHP within PHP?
« Reply #6 on: July 10, 2006, 04:31:34 PM »
Unless I'm mixing stuff up he's getting the page from a database, how does it magically get inline in the page?

Placeholders.
<a href="http://www.daviddoranmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddoranmedia.com/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

Offline redarrow

  • Freak!
  • Posts: 8,150
  • Gender: Male
  • PHP IS FOR LIFE!
    • View Profile
    • my free dating site (a blind php programmer!)
Re: PHP within PHP?
« Reply #7 on: July 10, 2006, 04:46:19 PM »
are you useing a while loop then .

while($record=mysql_fetch_assoc($result) {

}?>


<html>
<body>
<? echo $record['user_name']; ?>
</body>
<html>

<?}?>
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.ath.cx/paypal_info/paypal1_info.inc

Offline nyc_fanTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: PHP within PHP?
« Reply #8 on: July 10, 2006, 04:47:43 PM »
redarrow, thanks for your reply, but i think ShogunWarrior is right as i am pulling the code from a database where it is stored as text, assigning it to an array and then trying to execute it.

I don't think this is possible, but i have used the first reply from ShogunWarrior and have worked some magic and now i can do exactly what i want to do - and it seems even easier than i imaged (although there is a bit of work involved to start with!)

Offline redarrow

  • Freak!
  • Posts: 8,150
  • Gender: Male
  • PHP IS FOR LIFE!
    • View Profile
    • my free dating site (a blind php programmer!)
Re: PHP within PHP?
« Reply #9 on: July 10, 2006, 05:01:45 PM »
ok mate
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.ath.cx/paypal_info/paypal1_info.inc