问题
I have a site under development with a "News" section and an "Older News" section. The news are stored individually in external PHP files which are loaded by the server by PHP (the server counts the number of PHP files and displays the last one – the current news – in the "News" sections and all the others in the "Older News" section:
<!-- ####### NEWS ####### -->
<div id="news">
<h2>NEWS</h2>
<div>
<a id="showoldnews" href="#news">OLDER</a>
</div>
<?php $directory = "assets/news/"; if (glob($directory . "*.php") != false) { $newscount = count(glob($directory . "*.php")); } else {} ?>
<?php include('assets/news/news' . $newscount . '.php'); ?>
<!-- ####### OLDER NEWS ####### -->
<div id="oldnews">
<h2>OLDER NEWS</h2>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "Older News" section is initially hidden and only made visible by a jQuery trigger:
// Show old NEWS
$('a#showoldnews').click(function () {
$('#oldnews').show(400);
});
But I am expecting problems in long-term: since all news are loaded in the first place, this means that with more and more news coming up the website will be loading slower. And even if it's "acceptable", it's still not the ideal solution.
I was thinking on using jQuery.load()
to load an external PHP file that would then load the old news only when the user asks for it.
The problem is that I don't know if its possible to load the following script by using jQuery.load()
after the user clicks on a link:
<?php
$directory = "assets/news/";
if (glob($directory . "*.php") != false) {
$newscount = count(glob($directory . "*.php"));
}
else {
}
?>
<?php
include('assets/news/news' . $newscount . '.php');
?>
It this approach acceptable? I have tried, but it didn't work at all (I could load static content with the load()
function, but I was not able to make the server process the PHP script. I don't know if it's possible to make the server process the PHP code, because PHP processing is done on the the server side before the website begins loading... which is not the case.
UPDATE #1
I have the following that loads the 'load.php' file from the server:
<script type="text/javascript">
$('#oldnews').load('load.php');
</script>
The content of the load.php file is:
<div class="section" id="oldnews">
<h2>OLDER NEWS</h2>
<div class="topLink">
<a href="#">TOP</a>
</div>
<div class="topLink2">
<a id="hideoldnews" href="#news">HIDE</a>
</div>
<?php
$newscount = $newscount-1;
while ($newscount>0) {
include('assets/news/news' . $newscount .'.php');
--$newscount;
}
?>
</div>
The "TOP" and "HIDE" links appear without any problems. However, the PHP block seems to not be processed at all...
回答1:
PHP can be run before jQuery grabs the content... Here is what I setup and tested...
test.html
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div id="feeds"><b>45</b> feeds found.</div>
<script type="text/JavaScript">
$("#feeds").load("go.php");
</script>
</body>
</html>
go.php
<?php
echo 'Hi';
?>
And the result is that #feeds
.innerHTML
is "Hi"... I would love more information/code in replicating the situation. It might help to take a peek at the jQuery load API documentation.
回答2:
As @joseph told its correct and helped me, but in mycase i wanted to hide the content of the External Php file,
Below is my code changes which had helped me !
<!DOCTYPE HTML>
<html>
<head>
<script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<b>Db file result</b>
<div id="feeds" hidden=""></div>
<script type="text/JavaScript">
$("#feeds").load("db.php");
</script>
</body>
</html>
My Php file had database connection so i had to hide it, db.php
<?php
session_start();
$db = pg_connect("host=ec2-xxx-x0-xxx-xx.compute-1.amazonaws.com port=5432 dbname=dxxxxxxxxx user=vxxxxxxxxxx password=1xxxxxxxxxxxxxxxxxxxxxxxxxxxxd");
pg_select($db, 'post_log', $_POST);
$query=pg_query("(SELECT id,name, FROM organization WHERE is_active = 'true' AND account_token = '".$_SESSION['account_token']."');
$json=array();
while ($student = pg_fetch_array($query)) {
$json[$student["is_user"]."-".$student["id"]] = $student["name"]."-".$student['pan'];
}
$textval = json_encode($json);
$foo = "var peoplenames=" . $textval;
file_put_contents('autocomplete-Files/'.$_SESSION['account_token'].'.js', $foo);
echo "<script>location='filename.php'</script>";
?>
来源:https://stackoverflow.com/questions/15768995/loading-an-external-php-file-with-an-internal-php-include-function-by-using-jque