How to insert PHP-Code via PHP-Script?

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
gerd101
Forum Newbie
Posts: 5
Joined: Sat Jan 16, 2010 5:33 am

How to insert PHP-Code via PHP-Script?

Post by gerd101 »

Dear community,
I've got a complete static HTML-page generated dynamically by PHP-script.

Code: Select all

<?PHP
..
$var .= "<a href=\"$index?do=$i&...\">$i</a>";
..
?>

Code: Select all

<html>
..
<a href="index.php?do=1&...">1</a>
<a href="index.php?do=2&...">2</a>
..
</html>
Now i want to insert some php-code within the HTML-document by PHP-script. The result should look like this:

Code: Select all

<html>
..
<a href="index.php?do=1&...">
[color=#FF0000]<? if ($do==1) echo '<div style=\"font-weight:bold; color:red;\">'; ?>[/color]1[color=#FF0000]<? if ($do==1) echo '</div>'; ?>[/color]</a>
 
<a href="index.php?do=2&...">
[color=#FF0000]<? if ($do==2) echo '<div style=\"font-weight:bold; color:red;\">'; ?>[/color]2[color=#FF0000]<? if ($do==2) echo '</div>'; ?>[/color]</a>
..
</html>
But the Code doesn't work!!

Code: Select all

<?PHP
..
$var .= "<a href=\"$index?do=$i&...\"><? if ($do==$i) echo '<div style=\"font-weight:bold; color:red;\">'; ?>";  
$var .= "$i <? if ($do==$i) echo '</div>'; ?></a>"; 
..
?>
My trials are not successful. Either via google or via testing. In my opinion it is a problem of escaping "<?" and "?>".
Is there anybody with any idea? That would be great.

Best regards
Gerd
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to insert PHP-Code via PHP-Script?

Post by requinix »

If the code you posted is also from index.php, why not just execute the PHP then and there?

Code: Select all

$do = (isset($_GET["do"]) ? $_GET["do"] : "default value");
 
// ...
 
echo "<a href=\"$index?do=1&...\">";
if ($do==1) echo "<div style=\"font-weight:bold; color:red;\">";
echo "1";
if ($do==1) echo "</div>";
echo "</a>";
 
echo "<a href=\"$index?do=2&...\">";
if ($do==2) echo "<div style=\"font-weight:bold; color:red;\">";
echo "2";
if ($do==2) echo "</div>";
echo "</a>";
I'd give props for using & in your links if I didn't take it right back for using a <div> as a means of highlighting text.
gerd101
Forum Newbie
Posts: 5
Joined: Sat Jan 16, 2010 5:33 am

Re: How to insert PHP-Code via PHP-Script?

Post by gerd101 »

Hi tasairis,
Thanks für your response.
It is an existing script which i am working on. And the problem goes one step further.

Your suggestion shows the classical way to create dynamic pages.

I need to put an if-condition in the final html-page. And this html-page is created by my php-script so that i have to place a PHP-Code in there. In other words: I will put PHP-Code into a variable.

It's hard to describe, i know. Here another short example:

Code: Select all

<?PHP
..
$mix = "<a href=\"$index?do=1&...\">[color=#FF0000][b]<?[/b][/color] if ($do == $i) echo '<font-color=red>'; [color=#FF0000][b]?>[/b][/color]EQUAL</font></a>";
..
?>
This doesn't work, because there might be a conflict by the introducing PHP sign "<?".

Best regards
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to insert PHP-Code via PHP-Script?

Post by AbraCadaver »

Still not entirely sure what you're trying to do, but:

Code: Select all

<?php
$mix = "<a href=\"$index?do=1&...\">";
if ($do == $i) {
    $mix .= "<font-color=\"red\">EQUAL</font>";
} else {
    $mix .= "EQUAL";
}
$mix .= "</a>";
?>
However, if you're just controlling color nd styles, I would set a class and use a style sheet.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

You cannot put PHP inside of a variable. It will not work. When your code is executed, it outputs the contents of the string: it doesn't execute them. If I understand you correctly, you want a nested If statement, eg.

Code: Select all

if ($foo == 'bar'){
    if ($x != 'y'){
        echo 'something';
    }else{
        echo 'something' . $totally . $different;
    }
}else{
    echo 'and now for something completely different';
}
gerd101
Forum Newbie
Posts: 5
Joined: Sat Jan 16, 2010 5:33 am

Re: How to insert PHP-Code via PHP-Script?

Post by gerd101 »

Hello Jonah,
that is the right expression, my intention is to put executive PHP-code into a variable of my script.

When this script is executed it creates a static HTML-page with the content of that variable, the executive code.

So when the HTML-Page is loaded, why shouldn't the executive code work?

Nearly everything is fine with the exception of that line, i just have to write it escaped or masked so that the script will ignore the "<?" "?>" during runtime.

Code: Select all

$li .= "<a href=\"$index?do=$i&rub=$rub\">";
$li .= '<? if ($do == '.$i.') echo "<div style="font-weight:bold; color:red;"> ?>";';
????

Best regards
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to insert PHP-Code via PHP-Script?

Post by McInfo »

I am unsure what you are seeking, so I hope my guess is correct.

This example uses the ternary operator.

Code: Select all

<?php
$counter = 2;
$html = '<p>Counter is ' . (($counter > 1) ? 'greater than' : 'less than or equal to') . ' one.</p>';
echo $html;
When $counter is 2 or greater, the result is

Code: Select all

<p>Counter is greater than one.</p>
When $counter is 1 or less, the result is

Code: Select all

<p>Counter is less than or equal to one.</p>
This example uses a function.

Code: Select all

<?php
function comparison ($number, $compareTo) {
    if ($number > $compareTo) {
        return 'greater than';
    } else {
        return 'less than or equal to';
    }
}
 
$counter = 2;
$html = '<p>Counter is ' . comparison($counter, 1) . ' one.</p>';
echo $html;
The results are the same as with the first example. You can achieve a more precise analysis of $counter by using a more complex conditional statement in the second example or by nesting ternary expressions in the first example.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 4:07 pm, edited 1 time in total.
gerd101
Forum Newbie
Posts: 5
Joined: Sat Jan 16, 2010 5:33 am

Re: How to insert PHP-Code via PHP-Script?

Post by gerd101 »

Hello McInfo
$html = '<p>Counter is ' . (($counter > 1) ? 'greater than' : 'less than or equal to') . ' one.</p>';
here the executive code is separated as usual so it creates the static output "Counter is greater than" eg. during runtime.

I don't need a static result but an executive code ....

The actual HTML-Page (the result of my script) looks like this

Code: Select all

<div id="pagecounter">
<a href="index.php?do=1&rub=1258379740">1</a>
<a href="index.php?do=2&rub=1258379740">2</a>
</div>
And i want it that way as the result of my script

Code: Select all

<div id="pagecounter">
<a href="index.php?do=1&rub=1258379740">
    [b] [color=#800000] <?php (if $do == 1) echo '<div class = "red">'; else echo '<div class = "norm">' ?> 1 </div></a>[/color][/b]
<a href="index.php?do=2&rub=1258379740">
  [b]  [color=#800000] <?php (if $do == 2) echo '<div class = "red">; else echo '<div class = "norm">'' ?> 2 </div></a>[/color][/b]
</div>
I have to insert executive code into a variable.

Best regards
gerd
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: How to insert PHP-Code via PHP-Script?

Post by timWebUK »

You can't insert executable PHP code into a PHP variable, it doesn't work like that.

I understand you want to dynamically create an HTML page which is fine, you can do that, you can also output PHP variables. But you can't output PHP code that will be run.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: How to insert PHP-Code via PHP-Script?

Post by McInfo »

My other guess is that the PHP code is to be displayed like it is in the code blocks on this page. In that case, use htmlentities().

Example:

Code: Select all

<?php
$php = '<?php echo "Hello, World."; ?>';
$html = '<p>Some example code: <code>' . htmlentities($php) . '</code></p>';
echo $html;
The HTML returned to the browser is

Code: Select all

<p>Some example code: <code><?php echo "Hello, World."; ?></code></p>
And the user sees the HTML rendered as

Code: Select all

Some example code: <?php echo "Hello, World."; ?>
Notice that the PHP code (in the $php variable) in the example is not executable code; it is a string.

Nothing else makes sense; so if that doesn't answer your question, carefully reread the posts on this page. You might have missed something. If that is not the case, you might be misunderstanding something fundamental about the way PHP works. We might be able to better guide you if you explain, without the distraction of code, what you are trying to do and why you think passing PHP code around will help you do that.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Thu Jun 17, 2010 4:09 pm, edited 1 time in total.
gerd101
Forum Newbie
Posts: 5
Joined: Sat Jan 16, 2010 5:33 am

Re: How to insert PHP-Code via PHP-Script?

Post by gerd101 »

Okay, let me say "Thank you" to all of you.

I will try a "quick & dirty" - method by patching the HTML-File after its creation before designing a code alternative.


Thank you and best regards
Gerd
Post Reply