Сначала загрузите и отобразите HTML перед переполнением стека

У меня есть 2 php-файла (index.php и lelong.php). Я пытаюсь загрузить html сначала в index.php (таблица) и отобразить слово (Calculation …) во втором столбце, в то время как 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 , '.' , ',' );

?>

Спасибо

0

Решение

Если предположить, 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;

?>

Эффекты на фронте ваши, чтобы контролировать. Вы можете использовать плагины для этого.

1

Другие решения

В вашем lelong.php страница, Добавить строку echo $lelong; В конце.

В вашем index.php, Удалить <?php include 'lelong.php'; ?>и импортировать библиотеку JQuery. Например <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

замещать <td><?php echo $lelong; ?></td> от <td id='lelong'>Calculating...</td>

Добавить код jquery <script>$('#lelong').load('lelong.php');</script>

Лучше сначала изучить jQuery.

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector