I have few sets of code to display right side of my webpage. I am using this place to show some user information and some short of ads. For ads I am using that code in my database. Now the problem is that I want to display one external php file which is located in the same directory where I am trying to include or require this php file.
But I dont know why this is not working. I tried with iframe that works fine but I want to use php include but it is not working .please tell where I am doing mistake. Here is my code:
if (is_array($data['blocks'])) {
$output .= '<div id="appside">';
foreach($data['blocks'] as $block) {
if (is_array($block)) {
$output .= '
<div class="block">';
if ($block['title']) {
$output .= '<div class="block_title">'.$block['title'].'</div>';
}
$output .= '<div class="block_content">
'.$block['content'].
'</div>
</div>
';
}
}
$output .= get_gvar('theme_block_sidebar').
'<div><?php include("/home/xxxxxxx/public_html/xxxxxxxa.php"); ?></div>
</div>
';// end of app_sidebar
I tried with all short of alternatives but its printing raw php code. its not getting executed. I tried to put <?php include(\'/home/xxxxxxx/public_html/xxxxxxxa.php\'); ?>
and all other sort of alternatives but its printing raw php code.
'<div><?php include("/home/xxxxxxx/public_html/xxxxxxxa.php"); ?></div>
PHP won't execute inside a string (which is all the above is).
You can't really concatenate an include statement (unless the included file returns something via return). Given I can't tell if your code snippet is inside a function or how it's executed, my best solution is to use output buffering.
$output .= get_gvar('theme_block_sidebar') . '<div>';
ob_start();
include "/home/xxxxxxx/public_html/xxxxxxxa.php";
$output .= ob_get_contents();
ob_end_clean();
$output .= '</div>'
if the file you wish to include is in the same directory as this file, then you should include the filename only
<?php include('filename.php'); ?>
if it is in a higher level or in a directory or a sub-directory in a higher level, then you should move up to this level then down to the file.
<?php include('../../path/to/file.php'); ?>
where ../../ is going up 2 levels, you can set it to as many levels you want to go up.
If you want to get the output from an external php file in a variable, you can get it using the PHP buffering functions:
ob_start();
include("/home/xxxxxxx/public_html/xxxxxxxa.php");
$output_from_php_file = ob_get_contents();
ob_end_clean();
Then you can append $output_from_php_file to your $output variable wherever you want.
Related
i would to know what is good practice for writing code to put all HTML code inside PHP function and in my front index.php file just call function to show code.
class.php:
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
echo '<ul class="centreList">';
while($nem->fetch())
{
echo '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
echo '</ul>';
}
index.php:
<?php $connection->test(); ?>
This work fine, but I would like to know is this proper way or is not a good practice to use html code inside PHP functions?
It's ok to build HTML within PHP, but I would not echo to the screen directly from within the function. Instead, return the built HTML string.
$html = '<ul class="centreList">';
while($nem->fetch())
{
$html .= '<li>'.$id.'<a href="'.$title.'" >Download</a></li>';
}
$html .='</ul>';
return $html
The function should not be responsible for pushing content to the browser because it really limits what you can do with your code. What if you wanted to further process the HTML? What if you run into a condition later in the code and decided to abort? What if you wanted to set some response headers later? Some content would already be gone so none of these things would be possible without clever workarounds.
In general you want to separate your responsibilities: I would even break things down further:
one piece of code is in charge of retrieving info from the DB and returning
Another piece is in charge of building the HTML string
A third piece is in charge of displaying the HTML (probably your index.php)
New index.php
<?= $connection->test(); ?>
Do not use echo to print the html directly, wrap the html within while loop surrounded by php tags
public function test() {
$sql='select id,title from test ';
$nem=$this->db->prepare($sql);
$nem->execute();
$nem->bind_result($id,$title);
return $nem;
}
<ul class="centreList">
<?php $res = test()->fetch();
while( $res->fetch() ) { ?>
<li> <?php echo $id ?> Download </li>;
<?php } ?>
</ul>
I am looking for a simple solution to add a Snippet in my index.php file to load and display the content shown in a file from an other Domain.
Plan was to add the Code to the 'Footer' before to show a floating ad on several of my websites.
Sourcesite: http://domainX.tld/floating/floater.txt
Content of file: little bit css for styling of the ad + script snippet for a close button + html to get it into shape.
Target Site gets a simple snippet to show content from txt file as its own content.
I have tried by now
<?php
$StrLessDescription = ("//domainX.tld/floating/floater.txt");
$file_contents = file_get_contents($StrLessDescription);
?>
Site loads but doen't shows anything of my code.
<?php
$handle = fopen("//domainX.tld/floating/floater.txt", "rb");
$delimiter = ",";
while (!feof($handle) ) {
$line = fgets($handle);
$data = explode($delimiter, $line);
foreach($data as $v) {
echo $v;
}
}
fclose($handle);
?>
Site wouldn't even load.
<?php
$f = fopen("//domain.tld/floating/floatr.txt", "r");
// Read line by line until end of file
while(!feof($f)) {
echo fgets($f) . "<br />";
}
fclose($f);
?>
Creates an endless amount of where my Code should be
Other Fails i have deleted already.
Once i had a simple snippet that had done the trick, does one have any idea how to accomplish that again?
This should do the trick:
<?php
echo file_get_contents('//domain.tld/floating/floatr.txt');
Sticking to the straightest way to do it as your intention is and supposing that:
URL you provide for the txt file is correct
you have read access to it
the file has contents to display
your PHP version is (PHP 4 >= 4.3.0, PHP 5, PHP 7) to support
the file_get_contents() function
You are missing in your first approach to echo the contents of your variable $StrLessDescription to send it to output.
<?php
$StrLessDescription = ("//domainX.tld/floating/floater.txt");
$file_contents = file_get_contents($StrLessDescription);
echo $file_contents;
?>
Remember that for large projects you could consider using a framework to achieve the same goal in a more organized way. This is a solution to a quick-and-dirty approach you inquiry.
Is this possible?
What I am trying to accomplish:
Create a html template in the form of a string
Inside the string, add a script, something like include 'php/countries.php';
Echo entire string to html page
Everything but the 2nd step works. I would like to see a php file echo the question being asked, onto the html page, including an echo from another php file.
EXAMPLE
echo "<div id=\"first\"><?php include \'countries.php\'; ?></div>";
I have tried the above, as well as the below:
EXAMPLE
echo "<div id=\"first\">".include 'countries.php'."</div>";
Would this require eval?
Any and all help is appreciated.
Seems a bit silly, but you could do the following:
echo "<div id=\"first\">" . file_get_contents('countries.php') . "</div>";
Or...
echo "<div id=\"first\">";
include "countries.php";
echo "</div>";
Or...
$externalfile = compileexternal('countries.php');
function compileexternal($file) {
ob_start();
require $file;
return ob_get_clean();
}
echo "<div id=\"first\">" . $externalfile . "</div>";
If none of these are what you need, please update the question. There are a dozen ways.
You can use
eval()
But it is not a good practice.
You can use a regular expression.
For example, your string could be;
<div id="first">{{countries.php}}</div>
You'd then do;
$string = "<div id='first'>{{test2.php}}</div>";
echo preg_replace_callback("/(\{\{.+\}\})/", function($matches) {
include_once( str_replace(array("{", "}"), "", $matches[0]));
}, $string);
Check the file exists if( file_exists() )
Check the file can be included (we don't want to include ../../../../../etc/passwd
Just trying to tidy up a little bit of code here. I have a php command to output some html. However, one of my comands is quite a large amount of html, I was wondering if it's possible to output code referenced in a different file?
For example, my current php looks like this:
$output .= '<div class="contact-form '.$css_class.'" >';
$output .= '<h4 class="form-title">'.$title.'</h4>';
$output .= 'SOME VERY LONG CODE'
Is it possible to do something like this:
$output .= include('file_with_long_code.html');
instead? I aven't tested this, but am curious to know if it works or what the proper way of doing it is
you can instead use getfilecontent function of php
$output .= file_get_contents('file_with_long_code.html');
You could do something like this:
ob_start();
include('somefile.php');
$output = ob_get_contents();
Read more about output buffering in the docs:
http://php.net/manual/en/function.ob-start.php
I recommend using a PHP Framework, most of them have a very good functionality for these Kinds of "Problems".
html file.tpl.php:
<div class="contact-form <?=$css_class; ?>" >
<h4 class="form-title"><?=$title; ?></h4>
SOME VERY LONG CODE
main file:
<?php
ob_start();
include('file.tpl.php');
$output = ob_get_contents();
ob_end_clean();
?>
$output .= file_get_contents('file_with_long_code.html');
Yes, it is possible.
Let's say I have a controller function with these lines:
$this->load->view('stdHeader_view');
echo "<div class='main'>";
$this->loadView('foo_view');
echo '</div>';
$this->load->view('stdFooter_view');
This won't do what I want, because $this->load->view() doesn't immediately echo the view it loads, so the 2 echoed lines will appear at the top of the file that ultimately gets generated:
<div class='main'></div><html>...
So is there a way to do what I want, essentially "echo" snippets of HTML inline within the controller and have them appear in the same place relative to the views loaded above and below them? Obviously I could accomplish this by making entire view files for <div class='main'> and </div>, but this seems a little silly.
Why would you wan't to do that?
You should load your variable data into the view and manipulate the view from the data instead.
If this really has to be done, do something like this:
$html = $this->load->view('stdHeader_view', TRUE); //add TRUE to the second parameter, to return the views content
$html .= '<div class="main">';
$html .= $this->load->view('foo_view', TRUE);
$html .= '</div>';
$html .= $this->load->view('stdFooter_view', TRUE);
$this->output->set_output($html); //ends the controller and shows $html as output