Example of what i am trying to do:
<?php
$html_element = '<form><label>country</label><select>'.include_once("country_list.php").'</select></form>';
?>
I have tried the above and the following:
<?php
$html_element = '<form><label>country</label><select><?php include_once("country_list.php"); ?></select></form>';
?>
Of course the "$html_element" is then echoed in some div later. So how can i include the "country_list.php" in this php string variable so that it will pull correctly?
If you want to include the data, no matter which file type it is, do like this:
<?php
$html_element = '<form><label>country</label><select>'.file_get_contents("country_list.php").'</select></form>';
?>
Related
I had two script, one in .php and one in .tpl
I need to pass the variable in php to the tpl.
I tried this one, but nothinng works (but somehow
it works for one or two days, and after that,
it showed blank,
if i create another php script just to echo the variable, it works.
PHP Code:
<?php
$usdidr2 = "12610.198648";
$usdidr2 = number_format($usdidr,2,',','.');
echo $usdidr2;
session_start();
$regValue = $usdidr2;
$_SESSION['regUSDIDR1'] = $regValue;
?>
SMARTY Code:
<li>
<a href="example.php"><strong>
{php}
session_start();
$regValue = $_SESSION['regUSDIDR1'];
$regValue2 = number_format(45.99*$regValue,2,',','.');
echo "Rp. ".$regValue."";
print_r($regValue);
{/php}
</strong></a>
</li>
Here is the syntax to send data from php to tpl
$smarty->assign('variable name with which you can access the data in tpl', $php_data_you_want_to_send);
Update:
$smarty->assign('rate',$usdidr2);// you just need to write rate without $
You can access it in smarty like {$rate} if it is string
You can access it in smarty like {$rate|print_r} if it is array
You can use this syntax:
$res = "Hello World!";
$this->context->smarty->assign('result', $res);
And passing to .tpl file like this:
{$result}
Hope this helping you.
I am trying to display different content on a page based on some options.
Also, I am trying to avoid using php echo for all the html output.
I came up with the following solution accidentally, and now I'm confused about how it actually works.
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<?= $name ?>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<?= $name ?>
It seems like the php interpreter would end up including html tags into a <?php ?> block when it reaches the following line, but somehow, this code works, and the outputted html is valid.
include('page0.php');
Can someone explain what exactly is going on here?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
From PHP manual, include function.
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'm trying to load an php file through ajax using load(). Here's my code:
var loadUrl = "myfile.php";
$("#lst-results").load(loadUrl);
So, when I do this, it works:
<?php
$test = 'Hi :3';
?>
<h3>Hi</h3>
<h3><?php echo $test ?></h3>
But I need to include a file that contain a lot of includes and functions using a lot of different classes. I've tryed to include this file and use it as a function like this, but does not work and Nothing appears int the load() results. This function return an array.
<?php
include 'path/myfile.php';
$result = myFunction();
?>
I can't figure out how to solve it. :(
You need to "echo" the $result.
<?php
include 'path/myfile.php';
$result = myFunction();
echo $result;
<?php
$conn = oci_connect('usr', 'pass', 'host');
$instance_status="command1";
$spacecheck="command2";
$log_apply="command3";
$command=$_GET['name'];
echo $command;
$stid = oci_parse($conn, $command);
--some code--
?>
My HTML Page:
<html>
<title>Status Check</title>
<body>
<b>Spacecheck</b>
<b>Log Application Status</b>
<b>Database Status</b>
</body>
</html>
The above is my code, I intend to assign to $command, the value from the href variable through $_GET. But, when I test this code, $command is not being assigned the value of the variable from $_GET, rather the name of the variable is simply assigned to $command.
Eg, If I click on this:
Spacecheck
This should assign the VALUE of $spacecheck to $command, which is not happening. $command returns '$spacecheck'
How do I do this variable assignment?
You are simply writing $spacecheck. What you need to do is jump inside PHP tags and echo the variable values. Like so:
Spacecheck
or use the php echo shortcut:
Spacecheck
See the difference?
Good luck.
try going the other way around, I mean print from php:
<?php
echo '<b>Spacecheck</b>';
echo '<b>Log Application Status</b>';
echo '<b>Database Status</b>';
?>
You must use PHP open and close tags in order to place PHP code in your HTML page among other things. Try this link:
http://www.php.net/manual/en/tutorial.firstpage.php
Your HTML actually contains the literal string '$spacecheck' in the URL. $variables are only parsed in sections between tags, not in plain HTML.
Try this in your HTML file (which should be called .php) instead:
<?php
$spacecheck = 'foobar'; // (some dummy values)
$log_apply = 'nope';
$instance_status = 'idle';
print("<html>
<title>Status Check</title>
<body>
<b><a href='oraData.php?name=$spacecheck'>Spacecheck</a></b>
<b><a href='oraData.php?name=$log_apply'>Log Application Status</a></b>
<b><a href='oraData.php?name=$instance_status'>Database Status</a></b>
</body>
</html>");
?>