在PHP之前先加载并显示HTML
我有两个php文件(index.php和lelong.php)。我试图在index.php(表)中首先加载html,并在第二列显示单词(计算…),而lelong.php在输出数据之前从网站提取数据 有办法吗?我听说过使用JS或AJAX,但不知道如何使用 Index.php在PHP之前先加载并显示HTML,php,html,xpath,web-scraping,domdocument,Php,Html,Xpath,Web Scraping,Domdocument,我有两个php文件(index.php和lelong.php)。我试图在index.php(表)中首先加载html,并在第二列显示单词(计算…),而lelong.php在输出数据之前从网站提取数据 有办法吗?我听说过使用JS或AJAX,但不知道如何使用 Index.php <!DOCTYPE html> <html> <head> <?php include 'lelong.php'; ?> </head> <body>
<!DOCTYPE html>
<html>
<head>
<?php include 'lelong.php'; ?>
</head>
<body>
<table border ="1" style = "width:50%">
<tr>
<td>E-Commerce Website</td>
<td>No. of Products </td>
</tr>
<tr>
<td>Lelong</a></td>
<td><?php echo $lelong; ?></td>
</tr>
</table>
<body>
电子商务网站
产品数量
勒隆
lelong.php
<?php
$grep = new DoMDocument();
@$grep->loadHTMLFile("http://www.lelong.com.my/Auc/List/BrowseAll.asp");
$finder = new DomXPath($grep);
$class = "CatLevel1";
$nodes = $finder->query("//*[contains(@class, '$class')]");
$total_L = 0;
foreach ($nodes as $node) {
$span = $node->childNodes;
$search = array(0,1,2,3,4,5,6,7,8,9);
$number = str_replace($search, '', $span->item(1)->nodeValue);
$number = preg_replace("/[^0-9]/", '', $span->item(1)->nodeValue);
$total_L += (int) $number;
}
$lelong = number_format( $total_L , 0 , '.' , ',' );
?>
在lelong.php
页面中,添加一行echo$lelong代码>在末尾
在index.php
中,删除
,然后导入JQuery库。例如
将
替换为计算…
添加jquery代码$('#lelong').load('lelong.php')代码>
最好先学习jQuery。假设lelong.php
已经运行良好,可以使用ajax获得结果:
基本示例:
因此,在HTML中:
<table border ="1" style = "width:50%">
<tr>
<td>E-Commerce Website</td>
<td>No. of Products </td>
</tr>
<tr>
<td>Lelong</a></td>
<td class="result_data">Calculating ...</td><!-- initial content. Loading ... -->
</tr>
</table>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(document).ready(function(){
// use ajax, call the PHP
$.ajax({
url: 'lelong.php', // path of lelong
success: function(response){
$('.result_data').text(response);
}
})
});
</script>
电子商务网站
产品数量
勒隆
精明的。。。
$(文档).ready(函数(){
//使用ajax,调用PHP
$.ajax({
url:'lelong.php',//lelong的路径
成功:功能(响应){
$('.result_data')。文本(响应);
}
})
});
然后在PHP中:
<?php
$grep = new DoMDocument();
@$grep->loadHTMLFile("http://www.lelong.com.my/Auc/List/BrowseAll.asp");
$finder = new DomXPath($grep);
$class = "CatLevel1";
$nodes = $finder->query("//*[contains(@class, '$class')]");
$total_L = 0;
foreach ($nodes as $node) {
$span = $node->childNodes;
$search = array(0,1,2,3,4,5,6,7,8,9);
$number = str_replace($search, '', $span->item(1)->nodeValue);
$number = preg_replace("/[^0-9]/", '', $span->item(1)->nodeValue);
$total_L += (int) $number;
}
$lelong = number_format( $total_L , 0 , '.' , ',' );
echo $lelong; // output lelong
exit;
?>
如果您对Ajax语言有基本的了解,那么Ajax上的非常可靠。Ajax将满足您的需求,但如果您尝试自己实现,并向我们展示您尝试的解决方案,我们可以更好地帮助您。