So I got the following: I'm creating dynamic pages (based on the page ID (obtained through $_GET)). My page consists of a $_get check (the html may only be shown if a $_GET variable is set) and the echo'ing of a page with html tags + php variables. Things like:
<table>
<tr>
<td>Name:</td>
<td>myFunction('foo');</td>
</tr>
</table>
etc etc etc.
I include this code above myincludedfile.php into my main file with functions include and isset($_get) .
echo include 'myincludedfile.php';
This, however, not seems to work. Altough the html gets shown, the variables and functions remain text and don't get executed.
Can anyone help me out?
Change it like this:
<table>
<tr>
<td>Name:</td>
<td><?=myFunction('foo');?></td>
</tr>
</table>
That way you are actually opening a small PHP block in the template. <?= is a shorthand notation for <?php echo, although it depends on your server configuration if the shorthand is enabled.
If the function doesn't return a value, but echos it instead, you can leave out the = as well and just execute the function like this: <? myFunction('foo'); ?> or the long notation: <?php myFunction('foo'); ?>.
In general, you don't need <?php at the start of your file. It's just that everything inside <?php .. ?> tags is executed as php and the rest is considered static output.
B.t.w. you don't need to use echo when you include a file.
Here is another alternative
$myfunction = myFunction('foo');
echo <<<CODE
<table>
<tr>
<td>Name:</td>
<td>$myFunction</td>
</tr>
</table>
CODE;
include 'myincludedfile.php';
This is called the heardoc syntax http://php.net/manual/en/language.types.string.php#example-75
You will have to include the file like this:
include 'myincludedfile.php';
What you are doing, is echo'ing the php file
Look:
<?php
$html="<h1>Hello</h1>";
function hello()
{
echo "<h1>Hello</h1>";
}
function hello2()
{
?>
<h1>Hello</h1>
<?php
}
?>
All these does the same thing.
to use them, you can do the following:
<?php
hello(); // Hello
hello2(); // Hello
echo $html; // Hello
?>
I don't think i get what you need, but try to be more specefic.
Related
I currently have a php which echo my html template.
However in that HTML template there is another echo which calls from another php script.
Just wondering how do I do that? Because once I echo my html template the other it doesn't seems to echo my content from the other php script.
HTML TEMPLATE
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
CONTACT TEMPLATE
<php? $name = "hello world"; $email = "hello#world.com"; ?>
I can see what you're trying to do, and it's a simple error. You can't escape php like that whilst inside setting a variable.
Also, I must add that you are declaring php incorrectly.
This is preferred
<?php
not
<php?
So make sure for your contact template you use the correct tag.
Also to include a file you have to call it/require it.
Back to the original question - Here is your method
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
Here is the correct method
<?php
require('contact.php');
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
echo $html;
?>
First I created the variable. And when doing so I insert the existing variables by escaping the php. Only once this final variable is created do I echo it.
Hope this helps you on your way.
Try to use include. The include statement includes and evaluates the specified file, in this case - your template.
Just Concatenation
<?
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
?>
Change the tags from <php? ?> to <?php ?> in your script
Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!
I've done a project that now needs to be changed in order to display one div if a variable is in an array and a different div if it isn't in the array.
Normally I'd just do
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "quarter code in here";}
else
{echo "nonquarter code in here";}
?>
and be on my merry way, however the code I've got already contains a load of html and php code already, which doesn't like encapsulated within another PHP block (as far as I'm aware?)
e.g.
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "Quarter HTML CODE
<?php quarter phpcode ?>";}
else
{echo "Non-Quarter HTML CODE
<?php non-quarter phpcode ?>";}
?>
So my question is, what is the best way to tackle this? Is it simply to do a javascript hide div A when the variable is met and hide divB when the variable isn't met, or is there a better solution?
Thanks
<?php
if (in_array($month,$quartermonths))
{ ?>
Quarter HTML CODE
<?php quarter phpcode ?>
<?php } ?>
split your html code from php code like this.
It sounds like you just want to concatenate the value of quarter phpcode. Let's say it's a single function, quarter_phpcode(). You can just do this:
{ echo "Quarter HTML CODE" . quarter_phpcode(); }
I'm looking for something much like the Using PHP variables inside HTML tags? question, but a little different.
In my case, I'd like to use code ore like this:
$somevar = 'a test';
include("file.html");
and file.html would contain
<b>hello, this is {$somevar}</b>
The problem is that it just prints hello, this is {$somevar}.
How can I make the HTML read the vars in the included file?
echo "<b>hello, this is {$somevar}</b>";
or
<b>hello, this is <?=$somevar?></b>
or
<b>hello, this is <?php echo $somevar; ?></b>
or
<b>hello, this is <?php print $somevar; ?></b>
You need to include the variable defining program, in the other program wanting to access it.
Example:
Say test.html has $somevar.
in file.html you do,
<?php
include('test.html');
echo "<b>hello, this is $somevar</b>";
?>
<?php
include "stuff.php";
$somevar = "test";
?>
<html>
<body><p><?php echo($somevar); ?></p></body>
</html>
I want to grab data from a mysql database by using php. The data looks something like this:
apple 3
orange 2
banana 4
I want to take the data and put it in a html table and use css to make it look pretty, but I dont want to deal with it inside <?php ?>
After I grab the
$result = mysql_query("SELECT * FROM Table");
can I reference the result variable outside the <? php ?> tags?
No. PHP can only be done in <?php ... ?> or <?= ... ?>. Use a template engine such as Smarty if you want substitution in this manner.
in short, no you cant, it is a php variable (technically a resource in this case) so you have to parse it through the php engine, which requires the php tags
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr><td>'.$row['fruit'].'</td><td>'.$row['id'].'</td></tr>';
}
echo '</table>';
Short answer is no. HTML cannot deal with dynamic content.
If you want to cut down the amount of echo statements within your code you can store the html within a given variable and then make reference to it.
I find it better to do the following:
<table>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row['fruit']?></td>
<td><?php echo $row['id']?></td>
</tr>
<?php endforeach; ?>
</table>
This provides clarity and minimizes concatenation.