我想制作一个网页,显示一个随机的网站(在当前网页内),只显示15秒,然后显示另一个页面,等等。我希望该网页从MySQL数据库中获得要显示的网站列表。
我不确定这是否可以在PHP中实现,因为我知道你可以在Javascript中使用iframe,但如果可能的话,我想在PHP中完成。如果有人能给我指明正确的方向或写一点代码,我将不胜感激。
将其添加到HTML head
:
<meta http-equiv="refresh" content="15;url=http://www.yourdomain.com">
然后,您可以每次为该页面提供不同的iframe。
这里有一个例子:
<!DOCTYPE html>
<html>
<head>
<title>Random website</title>
<meta http-equiv="refresh" content="15;url=http://www.yourdomain.com">
</head>
<body>
<iframe src="<?php echo $website_pulled_from_database; ?>"></iframe>
</body>
</html>
如果你坚持使用纯PHP,你可以在PHP中使用sleep函数,而不是使用jS。但我认为这样做毫无意义。http://php.net/manual/en/function.sleep.php您还应该研究PHP中的ajax。
您可以在PHP 中使用header()
header('refresh:15;url=http://www.google.com/');
上面的代码将在15秒后将用户重定向到谷歌。
好吧,我认为你不需要在纯PHP中这样做。您可以从数据库中检索内容,并使用javascript随时显示它们。您可以将setInterval函数用于javascript(jQuery)。
对于。例如:
<script type="text/javascript">
$(function(){
var i=0;
$('li').not(':first').hide();//Hide everything other than the first li item
setInterval(function(){
$('li').eq(i).hide();//Hide the current li item
i++;
if($('li').eq(i).text().length==0) //check if it is the last li item
{
i=0;
}
$('li').eq(i).fadeIn(1000); //FadeIn the next li item
},3000);
});
</script>
希望这对你有所帮助。