Php 如何多次回显图像?
我正在尝试制作一个网页,根据它有多少访问者来回复图像。到目前为止,我使用的代码统计访问页面的次数,并将数字存储在变量Php 如何多次回显图像?,php,image,Php,Image,我正在尝试制作一个网页,根据它有多少访问者来回复图像。到目前为止,我使用的代码统计访问页面的次数,并将数字存储在变量$count中。然后,我在页面$image上回显随机图像。现在我想将页面上显示的随机图像乘以访问量$count。因此,如果有20次访问,20张随机图像将在页面上回响。有什么办法吗 以下是我目前的代码: $fp = fopen("count.txt", "r"); $count = fread($fp, 1024); fclose($fp); $count = $count + 1;
$count
中。然后,我在页面$image
上回显随机图像。现在我想将页面上显示的随机图像乘以访问量$count
。因此,如果有20次访问,20张随机图像将在页面上回响。有什么办法吗
以下是我目前的代码:
$fp = fopen("count.txt", "r");
$count = fread($fp, 1024);
fclose($fp);
$count = $count + 1;
$random = rand(0, 30);
$image = "<img src='" . $random . ".jpg'/>";
echo $image;
$fp = fopen("count.txt", "w");
fwrite($fp, $count);
fclose($fp);
$fp=fopen(“count.txt”、“r”);
$count=fread($fp,1024);
fclose($fp);
$count=$count+1;
$random=rand(0,30);
$image=“”;
回波图像;
$fp=fopen(“count.txt”,“w”);
fwrite($fp,$count);
fclose($fp);
非常感谢您的帮助。
谢谢:)简单到:
for($i=0;$i<$count;$i++) {
$random = rand(0, 30);
echo '<img src="'. $random . '.jpg"/>';
}
for($i=0;$ifor($i=1;$ifor($i=1;$i$count
是访问次数。请执行以下操作:
$count = 20; // I suppose that there are 20 visitors
for($i = 0; $i < $count; $i++) {
$random = rand(0, 30);
$image = "<img src='" . $random . ".jpg'/>";
echo $image;
}
$count=20;//我估计有20位访客
对于($i=0;$i<$count;$i++){
$random=rand(0,30);
$image=“”;
回波图像;
}
输出图像并在循环中定义一个随机数。不要使用旧的文件处理工具
$count = file_get_contents("count.txt") + 1;
$a = array();
for($i = 0; $i < $count; $i++) {
$random = rand(0, 30);
while (in_array($random, $a) && count($a) <= 30) {
$random = rand(0, 30);
}
array_push($a, $random);
$image = "<img src='" . $random . ".jpg'/>";
echo $image;
}
file_put_contents("count.txt", $count);
$count=file\u get\u contents(“count.txt”)+1;
$a=数组();
对于($i=0;$i<$count;$i++){
$random=rand(0,30);
而(在_数组($random,$a)和&count($a)中,这应该会提供您所需要的
for($i = 0; $i < $count; $i++) {
$random = rand(0, 30);
$image = "<img src='" . $random . ".jpg'/>";
echo $image;
}
为了清楚这一点的局限性-如果您使用的计数高于随机范围,您将在此处遇到无限循环。此外,您应该初始化数组(在for循环之外),例如:
$random_used = array();
…只是为了阻止PHP通知
可能有一种更简单的方法可以做到这一点,但这是我通常使用的方法。看起来像是一个家庭作业。投票结束感谢您的回答,非常有用:)出于兴趣,你知道获得独特随机图像的最佳方法是什么吗?我已经扩展了答案以回应这个问题。谢谢Stephen,感谢你。请参阅我答案中的解释。每次都会显示相同的图像。
<?php
$images = range(0, 12);
for($i = 0; $i < $count; $i++) {
// you shoulod check whether count is not above the number of available images
$image = array_rand($images, 1);
echo "<img src='" . $images[$image] . ".jpg'/>";
unset($images);
}
while(in_array($random, $random_used)) {
$random = rand(0, 30);
}
$random_used[] = $random;
$random_used = array();
<?php
$images = range(0, 12);
for($i = 0; $i < $count; $i++) {
// you shoulod check whether count is not above the number of available images
$image = array_rand($images, 1);
echo "<img src='" . $images[$image] . ".jpg'/>";
unset($images);
}