I need to include html, not from file, just simple code.
I have main content included by:
<?php
$content = array(
'001'=>'content/001_001.php',
'002'=>'content/001_002.php',
'003'=>'content/001_003.php'
);
if(in_array($_GET['show'], array_keys($content))) {
include($content[$_GET['show']]);
} else {
include('content/001_001.php');
}
?>
And on a side i'd like to include simple html, but that's more like 3 buttons in each category, so cloning lots of *.html & *.php files won't be right and clean work.
In case of opened page: ?show=001, on a side would be added <div>001</div>;
?show=002, on a side would be added <div>002</div> and etc.
If I understand your question correctly, you want to load HTML code from an array rather than from a file. You can do this by changing your include to echo.
<?php
$content = array(
'001'=>'<div>001</div>',
'002'=>'<div>002</div>',
'003'=>'<div>003</div>'
);
if(!empty($_GET['show']) && isset($content[$_GET['show']])) {
echo $content[$_GET['show']];
} else {
echo $content['001'];
}
?>
Related
Im setting up a xampp php website with auto creating css for site (If site named xyz.php/html is created, then a css is created too). Unfortunatelly css doesn't want to include in website using php echo's and html tags. No error.
In style.php:
$arr = explode("/",$_SERVER['PHP_SELF']);
$style = "";
foreach ($arr as $key){
if(strpos($key, ".php")){
$style = str_replace(".php", "style.css", $key);
}
}
if($fp = fopen($_SERVER['DOCUMENT_ROOT']."/TestPHP/".$addr,"wb+")){
fwrite($fp,"body{background-color:#666;}");
fclose($fp);
}
echo $addr = "lib/require/styles/".$style;
echo '<link href="'.$addr.'" rel="stylesheet">';
In index.php:
require_once 'lib/require/php/styles.php';
That's because the only HTML (as i can see in your code) doesn't have anything else than the style, no head, no body... Why don't you directly paste the HTML inside the file instead of making PHP echo it?
I wanna replace braces with <?php ?> in a file with php extension.
I have a class as a library and in this class I have three function like these:
function replace_left_delimeter($buffer)
{
return($this->replace_right_delimeter(str_replace("{", "<?php echo $", $buffer)));
}
function replace_right_delimeter($buffer)
{
return(str_replace("}", "; ?> ", $buffer));
}
function parser($view,$data)
{
ob_start(array($this,"replace_left_delimeter"));
include APP_DIR.DS.'view'.DS.$view.'.php';
ob_end_flush();
}
and I have a view file with php extension like this:
{tmp} tmpstr
in output I save just tmpstr and in source code in browser I get
<?php echo $tmp; ?>
tmpstr
In include file <? shown as <!--? and be comment. Why?
What you're trying to do here won't work. The replacements carried out by the output buffering callback occur after PHP code has already been parsed and executed. Introducing new PHP code tags at this stage won't cause them to be executed.
You will need to instead preprocess the PHP source file before evaluating it, e.g.
$tp = file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$tp = str_replace("{", "<?php echo \$", $tp);
$tp = str_replace("}", "; ?>", $tp);
eval($tp);
However, I'd strongly recommend using an existing template engine; this approach will be inefficient and limited. You might want to give Twig a shot, for instance.
do this:
function parser($view,$data)
{
$data=array("data"=>$data);
$template=file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$replace = array();
foreach ($data as $key => $value) {
#if $data is array...
$replace = array_merge(
$replace,array("{".$key."}"=>$value)
);
}
$template=strtr($template,$replace);
echo $template;
}
and ignore other two functions.
How does this work:
process.php:
<?php
$contents = file_get_contents('php://stdin');
$contents = preg_replace('/\{([a-zA-Z_][a-zA-Z_0-9]*)\}/', '<?php echo $\1; ?>', $contents);
echo $contents;
bash script:
process.php < my_file.php
Note that the above works by doing a one-off search and replace. You can easily modify the script if you want to do this on the fly.
Note also, that modifying PHP code from within PHP code is a bad idea. Self-modifying code can lead to hard-to-find bugs, and is often associated with malicious software. If you explain what you are trying to achieve - your purpose - you might get a better response.
Why does this if statement have each of its conditionals wrapped in PHP tags?
<?php if(!is_null($sel_subject)) { //subject selected? ?>
<h2><?php echo $sel_subject["menu_name"]; ?></h2>
<?php } elseif (!is_null($sel_page)) { //page selected? ?>
<h2><?php echo $sel_page["menu_name"]; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or a page to edit</h2>
<?php } ?>
Because there is html used. Jumping between PHP and HTML is called escaping.
But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.
Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.
Sample
<?php $title="sample"; ?>
<html>
<title><?php echo $title; ?></title>
<body>
</body>
</html>
This is not much html but a sample how it could look like.
That sample you provided us should more look like....
<?php
if(!is_null($sel_subject))
{ //subject selected?
$content = $sel_subject["menu_name"];
}
else if (!is_null($sel_page))
{ //page selected?
$content = $sel_page["menu_name"];
}
else
{ // nothing selected
$content = "Select a subject or a page to edit";
}
echo "<h2>{$content}</h2>";
?>
You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.
According to some comments i did a approvement to the source :)
Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.
This is called escaping.
Because you cannot just type html between your php tags.
However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.
<?php
if(!is_null($sel_subject))
{ //subject selected?
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
}
elseif (!is_null($sel_page))
{ //page selected?
ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
}
else
{ // nothing selected
echo "<h2>Select a subject or a page to edit</h2>";
}
Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:
<?php if(/*condition*/){ ?> <html></html> <?php } ?>
or:
<?php if(/*condition*/){ echo '<html></html>' ; }
That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.
There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:
<?php
//subject selected?
if (!is_null($sel_subject)) {
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
//page selected?
} elseif (!is_null($sel_page)) {
echo "<h2>" . $sel_page["menu_name"] . "</h2>";
// nothing selected
} else {
echo "<h2>Select a subject or a page to edit</h2>";
}
?>
using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.
Hi I'm trying to get a piece of html to only show on the main page which is http://www.domain.com/ ... I wrote the code below but it doesn't work the HTML is showing regardless of the page, am I missing something
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
?>
<div style="margin:0 auto;">
<div style="float:left">
<?php endif; ?>
First of all - please change
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
into
$hweb = 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$hweb may be initialized somewhere before.
Second:
As long as you request 'http://www.domain.com/somename.php' your if condition will never get executed. REQUEST_URI will always hold '/somename.php' except you use some url rewriting.
Third:
Make sure all calls go to 'http://www.domain.com' and not to 'http://domain.com'. Subdomain configurtaions sometimes are very complicated.
At the risk of getting it wrong again..
Why not initialize a variable in the main file before including the header
<?php
$mainfile = true;
?>
then in the header
<?php
if ($mainfile===true)
....
This way the main file can be called anything and be placed anywhere.
Solution 1:
If the above code is written inside 'http://www.domain.com/index.php' file then it may work fine.
Solution 2:
else make sure that $hewb is set with null value earlier b4 this code so that ".=" would not add extra value b4 'http...'.
Now
$hweb = '';
echo $hweb .= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
That is because the the HTML is inline in the php file but outside of the PHP tags. You can simply echo the HTML inside the if.
if ($hweb == 'http://www.domain.com/')
{
echo '<div style="margin:0 auto;">';
echo '<div style="float:left">';
}
or if you have lots of HTML you could do it like this
<?php
ob_start();
?>
<html>
<body>
<p>This HTML only be echoed </p>
</body>
</html>
<?php
$hweb .= 'http://' .$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
if ($hweb == 'http://www.domain.com/'):
{
ob_end_flush();
}
else
{
ob_end_clean(); // Probably not needed
}
?>
I have a problem with 3rd-party-system integration in my drupal site.
Sorry for my english, i'm from russia, but i will try to explain my problem well.
Integration idea:
2 .php files
2 php-script lines (include
function's)
The problem is:
this scripts call to outside perl
(.pl) script. Perl script read the
parameters (parameters transfers by
url) and generate content.
I can't see this perl script, but i
know - hes working, but not in my
page :)
2 php files:
spectrum_view.php
<?php
$url = "http://young.spectrum.ru/cgi-bin/programs_view.pl";
$param = $_GET;
if (!empty($param))
{
$url .= "?";
foreach ($param as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
} echo $content = file_get_contents($url);
?>
spectrum_form.php
<?php
$url ="http://young.spectrum.ru/cgi-bin/programs_form.pl";
$params = $_GET;
if (!empty($params))
{
$url .= "?";
foreach ($params as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
} echo iconv("windows-1251","utf-8",(file_get_contents($url)));
?>
and the 2 php-lines, wich i insert in my drupal pages
(the first i insert in page http://new.velo-travel.ru/view
and the second in the right block)
include("http://new.velo-travel.ru/themes/themex/spectrum_view.php?$QUERY_STRING");
include("http://new.velo-travel.ru/themes/themex/spectrum_form.php?act=/view$QUERY_STRING");
So, i solved this problem, but not in drupal - only on my Localohost, i just create a 2 page:
form.php:
<?php
$url ="http://young.spectrum.ru/cgi-bin/programs_form.pl";
$params = $_GET;
if (!empty($params)){
$url .= "?";
foreach ($params as $keys=>$value) $url .= "&".$keys."=".urlencode($value);
}
$content = file_get_contents($url);
print $content;
**require_once 'view.php';**
?>
view.php:
<?php
$url = "http://young.spectrum.ru/cgi-bin/programs_view.pl";
$param = $_GET;
if (!empty($param))
{
$url .= "?";
foreach ($param as $keys=>$value)
{
$url .= "&".$keys."=".urlencode($value);
}
}
$content = file_get_contents($url);
print $content;
?>
=(
I'm not entirely sure, as to what you are trying to do. But it seems like you want to generate this content from the perl script. If this is a special page with it's own template, you should move all this code into template.php. This file is made to hold some logic you want to create the content for your page.
Personally I would prefer to make a module to handle all this, but it's probably easier to do this in the theme, with what you got now. It seems like you are making a form, and some content based on the form. This could be done in a module. You could create a Drupal form, and then handle the validation with drupal, and jst submit the data to perl. But if you would want to get it from perl, going with the theme is probably best. So how do you do it?
Implement a preprocess function for the tpl.php file you use.
Create all the logic here, you could copy the php files you use over or just include them. Import, assign the result to a variable the will be accessible in the template file.
Print the variable in your template.
In code this would look something like this:
//template.php file
function mytheme_preprocess_somename(&$vars) {
include('php');
// Do some logic.
$vars['form'] = $result_a;
$vars['my_content'] = $result_b;
}
// your .tpl.php
// Some markup here
<div><?php print $my_content; ?></div>
<div><?php print $form; ?></div>
Now, I'm not sure exactly what you are after, but something like this should help you along. Note it's important what you call your variables inside the template file, as you can overwrite some Drupal variables like $content, which can cause some bugs.
You probably are running into a security issue. Please note allow_url_fopen and allow_url_include - these settings must have accordant settings in your php.ini. Otherwise you can't e.g. include a remote file for security reasons.