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.
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
<script>
$('#tes').click(function(){
<?php $output = '';
foreach( $votes as $array)
if($array->color === $colors->color)
$output = $output . $array->votes . '<br>';?>
$('#Result').html(<?php $output ?>);
});
</script>
how should I rewrite it to make it work?
Messy code issues aside, this:
<?php $output ?>
Should be:
<?php echo $output ?>
Also note that your JavaScript html() function will need a string, which means quotes around the HTML. Something like:
$('#Result').html("<?php $output ?>")
But then if your PHP $output has quotes in it, that'll break. So then you'll need to look at addslashes().
While this should fix your current issues, the commenters are right that this is a horribly messy/ugly way to write code, you need to refactor this significantly.
Suggestion: One way to make this a bit cleaner would be like this:
// At the top of your page
<?php
$output = '';
foreach( $votes as $array) {
if($array->color === $colors->color) {
$output .= $array->votes . '<br>';
}
}
?>
// Down in your HTML code somewhere
<div id="output" style="display:none"><?php echo $output ?></div>
// Now for the much simpler javascript
<script>
$('#tes').click(function(){
$("#output").show();
});
</script>
This way you have minimal mixing of PHP with HTML/JS, you don't have to worry about escaping quotes, the PHP $output is already on your page (hidden) ahead of time, and JavaScript just has to show it.
I have got a Html and Javascript code, that contains about 1000 lines and I need to put it to php variable.
Sure I was thinking about the EOT method, But there is one problem with it, if there is word function like in javascript is, it will take it like php function, and this will cause errors.
Any other Idea how to do it?
I have already tried other forums, but they can't help me, so I hope they can help me on the best.
Maybe use output buffering...
<?php
ob_start();
?>
<b>
<u>
<font color="#FF0000">
<blink>
<marquee>
1000
LINES
OF
HTML
AND
JAVASCRIPT!
</marquee>
</blink>
</font>
</u>
</b>
<?php
$content = ob_get_contents();
ob_clean();
?>
Then your HTML and JavaScript will be in the $content variable.
You could read directly from an HTML file on disk, using file_get_contents().
You can use the EOF method.
There's no problem with reserved words in that case. (As far as I know)
EDIT:
$output .= <<<HTML
function bla()
{
//Something
}
HTML;
Won't be treated as a php function.
Try this;
class Temp
{
public function html($path)
{
ob_start()
require(path); // or file_get_contents(<URI>);
$html = ob_get_clean ();
return $html
}
}
$temp = new Temp();
$htmlData = $temp->html('somepath/somefile.php')
echo $htmlData;
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
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.
Using Object-Oriented PHP, where should HTML be rendered?
The business processes include several actions to maintain customer records.
Should the rendering of each business process get a separate PHP file? ie. viewCustomerTransactions.php?
Where should code like this reside?
$custTrans = Customer.getTransactions();
foreach ($custTrans as $ct){
$amount = $ct[0];
$date = $ct[1];
$product = $ct[2];
echo '<div class="custTrans">';
echo '<span class="custTransAmount">'.$amount.'</span>';
echo '<span class="custTransDate">'.$date.'</span>';
echo '<span class="custTransproduct">'.$product.'</span>';
echo '</div>';
}
Perhaps an MVC framework like codeigniter would be better?
I'm still figuring out what's the best way to keep php and layout seperate without too much fuzz. For the moment I really like the include-templating approach, beacause it's so simple and has no restrictions.
So, for your example, you would have a php file (example.php) that looks like this:
<?php
$custTrans = Customer.getTransactions();
$displ_transactions = array();
foreach ($custTrans as $ct){
$transaction = array(
'amount' => $ct[0],
'date' => $ct[1];
'product' => $ct[2];
);
$displ_transactions[] = $transaction; // this will push the transacion into the array
}
include 'example.tpl.php'
?>
And then you need a second file (example.tpl.php):
<?php foreach ($displ_transactions as $transaction) { ?>
<div class="custTrans">
<span class='custTransAmount'><?php echo $transaction['amount'] ?></span>;
<span class='custTransDate'><?php echo $transaction['date'] ?></span>;
<span class='custTransproduct'><?php echo $transaction['product'] ?></span>;
</div>
<?php } ?>
Just call example.php in your browser and you will see the same result as you had before.
This is all good and well for small websites, because this method causes some overhead. If you are serious about templating, use smarty. it's easy to learn, and it has automatic caching, so it's super fast.
I just realize you can also do it this way:
example.php:
<?php
$custTrans = Customer.getTransactions();
foreach ($custTrans as $ct){
$amount = $ct[0];
$date = $ct[1];
$product = $ct[2];
include 'example.tpl.php';
}
?>
example.tpl.php:
<div class="custTrans">
<span class='custTransAmount'><?php echo $amount ?></span>;
<span class='custTransDate'><?php echo $date ?></span>;
<span class='custTransproduct'><?php echo $product ?></span>;
</div>
Use whatever suits you best :)
If I were you I would store the html in a variable instead of echoing it out like so:
$custTrans = Customer.getTransactions();
$html = "";
foreach ($custTrans as $ct){
$amount = $ct[0];
$date = $ct[1];
$product = $ct[2];
$html .= "<div class="custTrans">";
$html .= "<span class='custTransAmount'>".$amount."</span>";
$html .= "<span class='custTransDate'>".$date."</span>";
$html .= "<span class='custTransproduct'>".$product."</span>";
$html .= "</div>";
}
You then have this html data stored in the variable $html and you can echo it out where ever you like.
echo $html;
Does that solve you problem mate?
W.
I would have to confirm that the include-templating (mentioned by Jules Colle) is one of the answers, nevertheless, it might get messy to maintain when the project is too large, so keep in mind to document what file is included by what file, since there is (currently) no (free) IDE solution to this type of chaining... a solution that would easily bring you from one file to another or simply lay-out everything into a procedural-like code.
Edit:
do not forget the magic constants:
http://www.php.net/manual/en/language.constants.predefined.php
and this:
$_SERVER['DOCUMENT_ROOT']