在PHP之前先加载并显示HTML [英] Load and display HTML first before PHP

查看:294
本文介绍了在PHP之前先加载并显示HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个php文件(index.php和lelong.php)。我试图先将html加载到index.php(表)中,并在第二列中显示word(计算中...),而lelong.php从网站提取数据后再输出。

I have 2 php files(index.php and lelong.php). I am trying to load the html first in the index.php (table) and display the word(Calculating...) on the second column while the lelong.php extracting the data from the website before outputting them.

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

Is there a way to do that? I heard of using JS or AJAX but not really sure how to do it.

Index.php

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

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 已经可以正常工作,是的,您可以使用ajax获得结果:

Assuming lelong.php is already working fine, yes you could use ajax to get the result:

基本示例:

因此在您的HTML中:

So in your 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;

?>

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

The effects on the front are yours to control. You could use plugins for that.

这篇关于在PHP之前先加载并显示HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆