[英]Dynamically change content scraped with PHP
I am developing a website which collates information from several weather/snow information sites for mountain walking in Scotland. 我正在开发一个网站,该网站将整理来自多个天气/雪地信息站点的信息,以便在苏格兰进行山地徒步旅行。 I currently have a page which uses an iFrame to fetch and show an avalanche report map for a region and a series of links which call a javascript function to change the map shown for different locations.
我目前有一个页面,该页面使用iFrame来获取并显示某个区域的雪崩报告地图,以及一系列链接,这些链接调用javascript函数来更改针对不同位置显示的地图。 I am now adding in a scraping method in php to pull the text report from a seperate page on the original site and add it with the map.
我现在在php中添加一种抓取方法,以从原始网站上单独的页面中提取文本报告,并将其与地图一起添加。 I've got that function working ok but what I am struggling with is how to change this text content when the user chooses a location by clicking on the link.
我已经可以正常使用该功能,但是我正在努力解决的是如何在用户通过单击链接选择位置时如何更改此文本内容。
Anyone have any suggestions on how to achieve this? 有人对如何实现这一目标有什么建议吗? Preferably I want to do it without refreshing the page (should I be using something like Ajax for the whole thing?).
最好是我不想刷新页面就这样做(我应该在整个过程中使用Ajax之类的东西吗?)。 Here are the 2 scripts I have at the moment:
这是我目前拥有的两个脚本:
PHP Scraping script: PHP搜寻脚本:
<?php
$data = file_get_contents('http://www.sais.gov.uk/page_Creag%20Meagaidh.asp');
$regex = '/Observed Weather Influences........(.+?)\W\Wp\W/';
$preg_match($regex,$data,$match);
echo $match[1];
?>
Javascript map change: JavaScript映射更改:
<script type="text/javascript">
function changeMap(val) {
frame=document.getElementById('avalanchemap');
frame.src="http://www.sais.gov.uk/summary_report.asp?area_id=" + val;
}
</script>
jQuery's not required but will simplify the process for you (doesn't sound like you're that confident) : jQuery不是必需的,但是它将为您简化过程(听起来并不那么自信):
Set up a container div in your HTML. 在HTML中设置容器div。 And add a script like so:
并添加如下脚本:
$('.dropdown-or-whatever').change(function(){
$.ajax({
url: 'http://www.domain.com/the_location/_of_your_script.php',
success: function(result){
$("#your-container-div").html(result);
}
});
});
The change() function on the first line is assuming you're using a dropdown (select) element, which might not be correct. 第一行的change()函数假设您使用的是下拉(选择)元素,该元素可能不正确。
You'll also (probably) want to pass your PHP script different parameters based on which dropdown choice is selected and scrape a different page depending what that's set to. 您(可能)还希望根据选择了哪个下拉选项,将不同的参数传递给PHP脚本,并根据设置的内容来刮取不同的页面。 Parameters can be added at the end of the URL string like so:
可以在URL字符串的末尾添加参数,如下所示:
?place=scotland
Any questions, just ask. 有任何问题,只问。
Edit: got bored and basically did it for you. 编辑:很无聊,基本上是为你做的。 http://pastebin.com/np7GVh3f Take a look at the code and hopefully you'll learn something :)
http://pastebin.com/np7GVh3f看一下代码,希望您能学到一些东西:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.