【问题标题】:Load and display HTML first before PHP在 PHP 之前先加载和显示 HTML
【发布时间】:2014-10-16 03:14:47
【问题描述】:

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

有没有办法做到这一点?我听说过使用 JS 或 AJAX,但不确定如何使用。

索引.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 , '.' , ',' );

?>

谢谢

【问题讨论】:

  • 如果您对 Ajax 有基本的了解,jQuery's documentation 非常可靠。 Ajax 将满足您的需求,但如果您尝试自己完成并向我们展示您尝试的解决方案,我们可以更好地帮助您。

标签: php html xpath web-scraping domdocument


【解决方案1】:

假设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>

然后在你的 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;

?>

前面的效果由您控制。您可以为此使用插件。

【讨论】:

    【解决方案2】:

    在您的lelong.php 页面中,在末尾添加一行echo $lelong;

    在您的index.php 中,删除&lt;?php include 'lelong.php'; ?&gt;,并导入JQuery 库。例如&lt;script src="//code.jquery.com/jquery-1.11.0.min.js"&gt;&lt;/script&gt;

    &lt;td&gt;&lt;?php echo $lelong; ?&gt;&lt;/td&gt; 替换为&lt;td id='lelong'&gt;Calculating...&lt;/td&gt;

    添加jquery代码&lt;script&gt;$('#lelong').load('lelong.php');&lt;/script&gt;

    你最好先学jQuery。

    【讨论】:

      猜你喜欢
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多