Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP之前先加载并显示HTML_Php_Html_Xpath_Web Scraping_Domdocument - Fatal编程技术网

在PHP之前先加载并显示HTML

在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>

我有两个php文件(index.php和lelong.php)。我试图在index.php(表)中首先加载html,并在第二列显示单词(计算…),而lelong.php在输出数据之前从网站提取数据

有办法吗?我听说过使用JS或AJAX,但不知道如何使用

Index.php

<!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将满足您的需求,但如果您尝试自己实现,并向我们展示您尝试的解决方案,我们可以更好地帮助您。