1

我在一页上有一个背景和一个标题。每次访问页面时,我都需要它们旋转。我发现了大量的图像旋转脚本,你只是引用脚本,就好像它是 css 中的图像,但我找不到一个我也可以为每个图像调用标题的脚本。有谁知道可以帮助我的任何事情?(停止从内到外学习 PHP)

例如

<html>

 <head>

  <title>Page Title</title>

  <style>

   body {
     background: url('this-image-needs-to-change.jpeg');
   }

  </style>

 </head>

 <body>

  <h1>At The Same Time As This Header</h1>

 </body>

</html>
4

2 回答 2

1

Randomizing using PHP is easy, just do:

 <?php
     $bgs    = array("bg1.jpg","bg2.jpg","bg3.jpg");
     $titles = array("Title1", "Title2", "Title3");

     $currentIdx = array_rand($bgs);
 ?>

 <style>

 body {
   background: url('<?php echo $bgs[$currentIdx]; ?>');
 }

 </style>
<body>
    <h1><?php echo $titles[$currentIdx];?></h1>
 </body>

Should do the job.

于 2012-08-09T09:11:15.763 回答
0

Do you want the image to change every time a specific user visits a page, or everytime any user visits a page?

E.g. person 1 views page 3 times and sees images 1, 2 + 3. Person 2 comes along, does he see image 1 or 4?

If the specific user, you will want to store a cookie that keeps track of what image count they are on. Then on index.php: Check if cookie exists, if so, add 1 to it and save it, then get the image corresponding to that number and output it. If not, set the cookie, and get the image. There are a few different ways you could grab some text relating to the image. You could use a database, a notepad file or even hard code the text into the webpage. Gonna have to clarify on that for more specific help I fear

If you want the image to change for every user, you will need to have a database as you will have to store some information on your side. I can't really see why you would want to do this so won't bother explaining it.

于 2012-08-09T09:07:25.800 回答