What may cause differences in displaing http content on server? - php

<section>
<?php echo(file_get_contents('http://server_address'));?>
</section><script>
<!--//--><![CDATA[// ><!--
$("ul").html('http://server_address');
//--><!]]>
</script>
What may cause that first server gets me the content of the link, the second displayed only text "http://server_address". Both servers are Apache2.

In the first case you use file_get_contents in PHP, that is actually doing what you need. The second use you just output the text. So your program is doing exactly what you wrote.
If I understood correct what you need is to output content from the link into ul element. This can be done l
<section>
<?php echo(file_get_contents('http://server_address'));?>
<!-- save content to variable, escaping quote chars -->
<?php $content = addslashes(file_get_contents('http://server_address')); ?>
</section><script>
<!--//--><![CDATA[// ><!--
$("ul").html('<?php echo $content; ?>');
//--><!]]>
</script>

Related

update a php variable from external code

I have a question, there is a way to update $error when his value change on external listPagPrinc.php?
<div id="statoPag">
<h3> Stato : <?php echo $error; ?> </h3>
</div>
<div class="headerCont">
<?php
include('procedure/listPagPrinc.php');
?>
</div>
Not as-is, no. Think of these HTML/PHP files like an office printer, once it prints out each line, you can't "go back" and print over it
In this example, all of the first 5 lines are run and effectively "set in stone" before anything is called in procedure/listPagPrinc.php.
If, and this is just speculation, you can't simply include procedure/listPagPrinc.php before you render $error because it also is printing additional HTML, you just need to encapsulate its code in functions as best as possible: One to set the value of $error, and a separate one to output the HTML you need.
You need to update the text content of the tag; you can do this with e.g. jQuery at runtime. This is the preferred way if some tags, and only those, change during the lifetime of the application page, and you do not wish to reload the whole page from scratch.
In this case, from listPagPrinc.php, you can output some Javascript code:
echo <<<JAVA1
<script>
alert("Ciao, mondo");
</script>
JAVA2;
or in your case using jQuery
echo <<<JAVA2
<script>
$('#statoPag h3').text("Errore!");
</script>
JAVA2;
Very likely the call will need to be inside a jQuery onDocumentReady function to be sure that it executes.
A better and faster way (and as #arkascha observed, nicer and more robust): you can generate the header from listpagPrinc.php or from a wrapper.
// file listPagPrincWrapper.php, replace your current file
// Ideally listPagPrinc could return a text value. In case it is
// printing it, as seems likely, we capture the output. This way
// we don't neet to modify the existing code.
ob_start();
include('procedure/listPagPrinc.php');
$lpp = ob_get_clean();
// At the end, we do the output part.
print <<<HTML
<div id="statoPag">
<h3> Stato : {$error}</h3>
</div>
<div class="headerCont">{$lpp}</div>
HTML;

Possible reasons for one of my tables in PHP not displaying in Dreamweaver?

My code was originally working fine,with the table originally displaying perfectly on the page, but for some odd reason my one section of my code isn't displaying anymore in Dreamweaver. The table in question that isn't working is on the lower half of the page, does anyone see any noticeable errors in the code?
https://docs.google.com/document/d/1F-xakWDC5aA8fG0ionbrAtENZH5lMqTKCjb-d4VwIp8/edit
It's because you close body and html half way down the page, leaving table outside of the document.
Put
</body>
</html>
at the very bottom of your php file.
here:
Prescription Fee = <?php echo $prescript_lense[1
</br>
</p>
</body> <!-- here -->
</html></p> <!-- and here -->
<p> </p>
<p> </p>
<p> </p>
Not only that, but your php call doesn't even end!
<?php echo $prescript_lense[1.
Close it ([1];?>).
Tons of bad code - from echoing image tags inside the head element, through having non-closed php tags with relevant code after them, to using tables for presentational purpose outside the body and even html tags.
That is the reason the table below isn't working - it's outside the body and html tags and inside a php tag.
You're closing your <body> and <html> tags half way through your document. Anything outside of your <html> tags will not be rendered as content on your page and will be ignored.
If you close your </body> and your </html> tags after your table, you will be including this content before your page ends.

Sharing a code fragment all over the website

So, if I have a website containing two or more webpages with some same code fragment (e.g. side menu or top bar), how can I store this fragment in one place for using by all the webpages on the website?
I tried to put that repeating code to separate php file, like:
<?php echo "<div id='menu'> ... </div>";
and then just use
<?php include "menu.php" ?>
but problems occur with double-quotes inside double-quotes (considering I have PHP and JS scripts inside that "central" one, it's even more trouble), interpretation and so on.
What should I use (preferable HTML and PHP tools) to achieve that "code sync"?
Why are you echoing HTML?
How can I put double-quotes inside double-quotes?
You can escape the double-quote by prepending a backslash:
echo "<div id=\"some-id\">";
But, you're dealing with the wrong problem!
You're going along the right lines with re-using code, but you don't need to echo HTML. Any HTML outside of PHP tags will parse as regular HTML anyway, so just do it like this:
<div id='menu'>
...
</div>
Then require_once('menu.php'); to import the file. This way, you won't have to mess around with escaping nested quotes.
What if I have dynamic content in my menu?
Good question, then use something like this:
<div id='menu'>
<?php foreach($menuitems as $menuitem): ?>
<div id='<?php echo $menuitem['id']; ?>'>
<?php echo $menuitem['text']; ?>
</div>
<?php endforeach; ?>
</div>
By keeping the PHP and the HTML in separate tags, you create code which is easy to parse, easy to read and easy to maintain.
Similarly, don't use inline JavaScript
Keep your js in separate files, and call it from within your HTML like this:
<script src="script.js"></script>
You know you can just include 'menu.html' or include 'menu.php' without echo just using html tags and adding any php functionality inline like you would normally.

php syntax for big block of echo <?php if(1): ?> html<?php endif; ?> correct way

Well most is in the title. I wonder if it's supposed to be that way or i can do the same without an if(1) condition I'm doing this because my website pages are all as php includes.
Thank you all
Answer retained:
Okay basically the way to do it is simply to include('file.php') as it will be considered out of the current <?php ?> environment.
Putting
<?php if(1): ?>
...
<?php endif; ?>
around your HTML code in a PHP file will have no effect on the result. You will still be able to include the file without it.
You can think of it like the "default mode" for a PHP file is that it contains HTML content. You only need to add <?php ?> tags if you want to add PHP code. If you're just putting HTML code in a PHP file, they're unnecessary.
The beauty of PHP is that you can move "in" and "out" of PHP very easily. You can do the following without issues:
<?PHP
if(whatever) {
?>
your HTML
<?php
include('whatever.php');
?>
more HTML
<?PHP
}
?>
To build on Zak's answer:
You can also use PHP to echo out things that aren't PHP... as long as you quote it appropriately.
<?php
//HTML
while ($x < 5) {
echo "<p> this is html that you can wrap with html tags! </p>";
$x++;
}
//Javascript
echo "<script type='text/javascript'>
some javascript code
</script>"
?>
Although, it's less confusing to just end the php tag to keep things separate.
And you can even use php as you want within html or javascript as long as you put the tags, and as long as the file is saved as a .php file (so PHP can be processed on the server).
Ex:
<script type="text/javascript">
//set a javascript image array to a php value
var imgArray = [<?php echo implode(',', getImages()) ?>];
</script>
But if you want to do this the other way around (IE, assign a browser-compiled value, such as a javascript value to a php value), you'll need to use AJAX.

Prevent Comments from Parsing

Is there a way to hide commenting in my php/html file?
I want to add markup that I don't want people to be able to view in source in their browsers.
Is this possible?
<!-- Prevent this comment from being viewed -->
<?php...
Thanks in advance.
If you add comments as PHP, people won't see it in their browser.
<div>
<!-- This HTML comment can be seen by people -->
<?php //But this PHP comment can only be seen by me :) ?>
</div>
http://en.wikipedia.org/wiki/PHP
I see what you mean. You can do that with output buffering:
<?php
// this is not
?><!-- this is sent to browser -->
And with output buffering.
<?php
ob_start();
?><!-- this is NOT sent to browser --> <b>This isn't sent as well!</b> <?php
ob_end_clean();
?>
However, if you want to remove comments only, you need to do some parsing:
<?php
ob_start();
?><!-- this is NOT sent to browser --><?php
$html=ob_get_clean();
// TODO: Use a DOM parser to parse $html and remove comments from it.
?>
That does sound a bit over-engineered though...
Just swap lines above and use php comment:
<?php...
// Prevented this comment from being viewed

Categories