所以我正在尝试让它选择1.jpg 2.jpg或3.jpg。我的猜测是我做错了,但我没有得到php错误,我的IDE也没有告诉我我的CSS错了,所以我没有什么可继续的。
代码:
HTML/CSS:
<html>
<head>
<link rel="stylesheet" href="css/styles.css" type="text/css" media="all">
<script type="text/javascript" src="js/jquery-1.4.2.min.js" ></script>
<?php
include 'php/backgorundlogic.php';
?>
<style>
body {
background:url($bgset);
background-size:cover;
}
</style>
</head>
<body>
<div class="container">
<input class="searchbar" type="text" />
</div>
</body>
</html>
PHP:
<?php
$rbg = mt_rand(1, 3);
$ext = '.jpg';
$bgset = 'img/' + $rbg + $ext;
?>
演示:此处
使用。而不是+
<?php
$rbg = mt_rand(1, 3);
$ext = '.jpg';
$bgset = 'img/' . $rbg . $ext;
?>
并更改这条线路
<style>
body {
background:url(<?php echo $bgset; ?>);
background-size:cover;
}
</style>
此行:
background:url($bgset);
应该是这样的:
background:url(<?php echo $bgset ?>);
现在,PHP引擎正在以纯文本形式传递$bgset
,因为它不在PHP脚本上下文中。你可能会考虑更经得起未来的考验,HTML也可以逃避这个价值:
background:url(<?php echo htmlspecialchars($bgset) ?>);
background: url('<?php $a = array(‘anyfile.jpg’,'anyotherfile.gif’, ‘somefile.png’); echo $a[array_rand($a)];?>');
或者如果您的图像名称符合末尾带有整数的内容,例如clouds3.png
background: url('/images/lm/clouds<?php echo rand(0,10)?>.png') ;