Load a html file - php

Probably i didnt explain very well, i updated the code to turn more easy to see what i want.
I need to load a external html file in that "div external_page....", i thing that the best way is to load a file in the php file(External PHP content), tell me if im wrong :)
This is the demo: http://vasplus.info/demos/load_and_refresh_div_every_10_seconds/index.php
(UPDATE)
<script language="javascript" src="js/jquery_1.5.2.js"></script>
<div id="external_page_content_displayer">External page contents will be here</div>
<!-- EXTERNAL PHP CONTENTS-->
<?php
srand((float) microtime() * 10005224);
$lines = file('ajax/rssatom/rss-atom.html');
$This_Page_Content = array($lines,
"teste","teste","teste");
$This_Page_Content_Rand_Keys = array_rand($This_Page_Content, 2);
$This_Page_Content_Displayer = $This_Page_Content[$This_Page_Content_Rand_Keys[0]] . "\n";
echo strip_tags($This_Page_Content_Displayer);
?>
<!-- LOAD FUNCTION-->
<script>
function Load_external_content()
{
$('#external_page_content_displayer').load('external_content.php').hide().fadeIn(3000);
}
setInterval('Load_external_content()', 10000);
</script>
Regards

You can try with this:
$lines = file('ajax/rssatom/rss-atom.html');

Related

Using PHP to determine what HTML to write out

This block of PHP code prints out some information from a file in the directory, but I want the information printed out by echo to be used inside the HTML below it. Any help how to do this? Am I even asking this question right? Thanks.
if(array_pop($words) == "fulltrajectory.xyz") {
$DIR = explode("/",htmlspecialchars($_GET["name"]));
$truncatedDIR = array_pop($DIR);
$truncatedDIR2 = ''.implode("/",$DIR);
$conffile = fopen("/var/www/scmods/fileviewer/".$truncatedDIR2."/conf.txt",'r');
$line = trim(fgets($conffile));
while(!feof($conffile)) {
$words = preg_split('/\s+/',$line);
if(strcmp($words[0],"FROZENATOMS") == 0) {
print_r($words);
$frozen = implode(",", array_slice(preg_split('/\s+/',$line), 1));
}
$line = trim(fgets($conffile));
}
echo $frozen . "<br>";
}
?>
The above code prints out some information using an echo. The information printed out in that echo I want in the HTML code below where it has $PRINTHERE. How do I get it to do that? Thanks.
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno=[$PRINTHERE]; halos on;", "frozen on")
You just need to make sure that your file is a php file..
Then you can use html tags with php scripts, no need to add it using JS.
It's as simple as this:
<div>
<?php echo $PRINTHERE; ?>
</div>
Do remember that PHP is server-side and JS is client-side. But if you really want to do that, you can pass a php variable like this:
<script>
var print = <?php echo $PRINTHERE; ?>;
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno="+print+"; halos on;", "frozen on"));
</script>

How to update live data?

I'm trying to figure out how to update my live data, I found some examples on Google of Ajax but I can't seem to get them to work.
The part that contains and places the live data in a paragraph is :
$file = "Data.txt";
$data = file($file);
$line = $data[count($data)-1];
for($i=1;$i<6;$i++){
switch ($line) {
case $i:
echo "<p class ='bus".$i."'> <img id='bus' src = 'bus.png'> </p>";
break;
}
}
This is the full html file
<!DOCTYPE>
<html>
<head>
<title>Bus</title>
<link rel="stylesheet" href="stijlenbestand.css">
</head>
<body>
<?php
//aanmaken 5 bushaltes
echo '<figure>';
for($i=1;$i<6;$i++){
echo "<img src = 'bushalte.png'>";
}
echo '</figure>';
//laatste lijn van tekstbestand.
$file = "Data.txt";
$data = file($file);
$line = $data[count($data)-1];
for($i=1;$i<6;$i++){
switch ($line) {
case $i:
echo "<p class ='bus".$i."'> <img id='bus' src = 'bus.png'> </p>";
break;
}
}
?>
</body>
</html>
For a live update you need two parts.
First is the part where your page is and the second part is where your data comes from.
Php is a very static language. Once your script is finished it won't do anything anymore.
For a "live-website" you need Javascript.
if you want to use jQuery i would recommend you to use the jQuery.post() function.
jQuery Code in your Website:
$.post( "test.php", { name: "John", time: "2pm" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
Your test.php
if(isset($_POST['name'])) {
//Do Some Stuff
$a = 'var a';
echo json_encode($a);
}
This is not ajax. Ajax means having frontend code fetching new information on the background. This information then gets appended to the DOM. (Usually the information is transfered as JSON encoded data but lets keep that out of scope.)
For this you need two files:
A frontend file (for instance static index.html with some content)
A backend file providing the data
The frontend file would run some JavaScript then requests the backend file
The backend file responds and returns some output
The javascript adds output to the DOM.
There are many ways of doing this and I don't have the te time to explain all of it here but you might want to have a look at: http://www.w3schools.com/jquery/jquery_ajax_intro.asp
This provides a simple example based on jQuery.

Reading a php file from another php file

I currently have a php file that I'm using as a template but I need it to read in data from another php file that I'm using for the page content. I'm doing it this way to save on code and time, however it doesn't appear to be working. I have done a test with shorter amounts of code but it still isn't working. they are both .php files.
Code -
<html>
<head>
<title></title>
</head>
<body>
<?php
$temp = 'test-2.php';
$file = fopen($temp, 'r');
$cont = fread($file, filesize($temp));
print $cont;
fclose($file);
?>
</body>
test-2.php
<?php
echo 'hello world';
?>
just use <?php include('test-2.php'); ?> surely that'll do what you want?

Is it possible to add a <script> tag into HTML with PHP str_replace?

I have en exercise Im working on right now where we can't use libraries. I have a REST based system that instantiates different PHP classes depending on what needs to be done. I have several HTML files that are loaded into PHP and then using str_replace I switch out the variables I want to inject into the HTML.
I now want to add a tag at the end of one of my HTML files. My ../../html/body.html file looks like this:
<div id="content-wrapper">
<div id="content">
<table>
<!-- $content -->
</table>
</div>
</div>
</div>
<!-- javascript -->
</body>
</html>
I use this "body" page several times so the " $content " varies depending on what I replace them with in PHP.
Now for some reason when I try to replace javascript with a script tag it doesn't work. Heres my PHP that is trying to do this:
$javascript = '<script type="text/javascript" language="javascript" src="../../js/subscriber.js"></script>';
$page = file_get_contents("../../html/body.html");
$head = file_get_contents("../../html/doctype.html");
$nav = file_get_contents("../../html/Navbar.html");
$page = str_replace('<!--javascript -->', $javascript, $page);
$page = str_replace('<!-- $content -->', $theFeed, $page);
$nav = str_replace('<!-- $name -->', $this->user, $nav);
$nav = str_replace('$user', $this->you, $nav);
$nav = str_replace('$key', $this->key, $nav);
$nav = str_replace('$picUrl', $picUrl, $nav);
echo($head);
echo($nav);
echo($page);
I was thinking that this way I only need to include scripts on the necesarry pages or not at all if the page doesn't need any javascript. Does PHP or HTML block script tags somehow? Im not sure how to get around this. Thanks!
You're missing a space. Your markup contains:
<!-- javascript -->
^ space here
But your PHP code is missing that space:
$page = str_replace('<!--javascript -->', $javascript, $page);
Change it to:
$page = str_replace('<!-- javascript -->', $javascript, $page);
I think your problem is that in the PHP you have no space after

get a text file using php and send the string to javascript

Ok, what I am trying to do is make a javascript loop of images, but first I have to get a list of the images. In javascript there is no way to directly grab this text file... http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt but it can be done eaisly in php, I am currently gettung the txt file using php, but the javascript cannot read the variable. How can I make javascript be able to read this variable. Here is what I have...
<?php
$file = "http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"; //Path to your *.txt file
$contents = file($file);
$string = implode($contents);
echo $string;
?>
<script type="text/javascript">
function prnt() {
var whatever = "<?= $string ?>";
alert(whatever);
}
</script>
You can use echo or print to write to the page in PHP.
var whatever = "<?php echo $string; ?>";
Although, if the file has line breaks in it, you will need to remove those.
Make it a bit more interesting: go ahead and split the fields and use JSON encoding. It should read directly in javascript without needing to call JSON.parse() on the client.
<?php
$lines = file_get_contents('http://...');
$lines = explode("\n",trim($lines));
foreach ($lines as &$line) {
$line = preg_split('/,? /',$line);
}
$js = json_encode($lines);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var dar = <?php echo $js; ?>;
</script>
</body>
</html>
You should also consider using a local proxy to cache the results of that file if you plan to run this frequently and especially if you are going to serve it up on a public web server somewhere. Store the file locally as "noaa_data.txt" and have a second script on a cron job (12 hours or something):
<?php
file_put_contents("/var/www/noaa_data.txt",file_get_contents("http://www.ssd.noaa.gov/goes/east/tatl/txtfiles/ft_names.txt"));
?>

Categories