使用php在文件夹中显示图像
我在文件夹中有一些图像,在文件中有图像的名称。 我想根据单击的按钮显示图像使用php在文件夹中显示图像,php,forms,Php,Forms,我在文件夹中有一些图像,在文件中有图像的名称。 我想根据单击的按钮显示图像 <!DOCTYPE html> <html> <head> <title>Your Album</title> </head> <body> <form method="post"> <input type="submit"
<!DOCTYPE html>
<html>
<head>
<title>Your Album</title>
</head>
<body>
<form method="post">
<input type="submit" name="first" value="FIRST">
<input type="submit" name="previous" value="PREVIOUS">
<input type="submit" name="next" value="NEXT">
<input type="submit" name="last" value="LAST">
<input type="submit" name="dele" value="DELETE">
</form>
<br>
<?php
$fp = fopen("imgname.csv","r");
$line = fread($fp,filesize("imgname.csv"));
$item = explode("\n", $line);
$count;
$sizecsv = sizeof($item);
if(isset($_POST['next'])) {
$count++;
echo "<img src='images/$item[$count]' width='250px'>";
echo "<br>","Image No: ",$count+1;
}
if(isset($_POST['previous'])) {
$count--;
echo "<img src='images/$item[$count]' width='250px'>";
echo "<br>","Image No: ",$count+1;
}
if(isset($_POST['first'])) {
$count = 0;
echo "<img src='images/$item[$count]' width='250px'>";
echo "<br>","Image No: ",$count+1;
}
if(isset($_POST['last'])) {
$count = $item[$sizecsv-1];
echo "<img src='images/$item[$count]' width='250px'>";
echo "<br>","Image No: ",$count+1;
}
?>
</body>
</html>
你的相册
这是我的密码。我将图像名称存储在一个数组中,并尝试根据单击的按钮操作索引。这适用于“第一个”按钮,但不适用于其他任何东西。我不明白这个问题。是不是每次点击一个按钮,页面就会重新加载,$count的值就会丢失。
请给出一个解决方案。
此外,我是新的php,所以如果你能保持它的简单,那么它将是有益的。提前谢谢。我会循环执行此操作
$i = 1;
while($i<=100){
$name = $i<10?"0".$i:$i;
echo "<a href='images/".$item.".jpg' class='image'>
<img src='images/thumbs/image".$name.".jpg' alt=''/>
</a>";
$i++;
}
$i=1;
而($i我假设您的csv文件数据是由新行分隔的文件名数组,如下所示:
some-img-01.jpg
img-02.jpg
img-05.jpg
....
请检查以下代码。
我已经重新构造了代码,以便在每个页面加载之间传递状态(如果上一篇文章中存在)。在这里,我将其设计为循环-如果到达末尾,单击“下一步”,则从第一个位置单击“上一步”,则跳到最后一步。您可以通过注释掉相应的行(检查注释)来禁用此功能
你的相册
$count
不会在请求之间保持状态您需要养成帮助您解决问题的习惯。您将获得积分,并鼓励其他人帮助您。@LawrenceCherone您能告诉我一个解决方案吗?解决方案是保持状态,首先查询值,然后在请求中传递当前分页r存储在会话中,即将$count
更改为$\u会话['count']
。您也可以在javascript中完成这一切,并在重新加载页面时保存。我只想在单击“下一步”按钮时显示下一个元素
<?php
$current_index = isset($_POST['current_index'])? intval($_POST['current_index']) : 0;
$fp = fopen('imgname.csv', 'r');
$line = fread($fp, filesize('imgname.csv'));
fclose($fp);
$item = explode("\n", $line);
$sizecsv = sizeof($item);
$current_index = isset($_POST['current_index'])? intval($_POST['current_index']) : 0;
if ($current_index >= $sizecsv) $current_index = ($sizecsv - 1);
if ($current_index < 0) $current_index = 0;
if (isset($_POST['next'])) {
$current_index++;
// If reached end of the list, then clicked next, the index starts from firt again.
// If you dont need this feature, then can comment the following line.
if ($current_index >= $sizecsv) $current_index = 0;
}
if (isset($_POST['previous'])) {
$current_index--;
// If the indexin first image, but clicked the Previous button, then the index goes to last element.
// If you dont need this feature, then can comment the following line.
if ($current_index < 0) $current_index = ($sizecsv - 1);
}
if (isset($_POST['first'])) $current_index = 0;
if (isset($_POST['last'])) $current_index = ($sizecsv - 1);
?><!DOCTYPE html>
<html>
<head>
<title>Your Album</title>
</head>
<body>
<form method="post">
<input type="text" name="current_index" value="<?php echo $current_index;?>"/>
<input type="submit" name="first" value="FIRST"/>
<input type="submit" name="previous" value="PREVIOUS"/>
<input type="submit" name="next" value="NEXT"/>
<input type="submit" name="last" value="LAST"/>
<input type="submit" name="dele" value="DELETE"/>
</form>
<br>
<img src="images/<?php echo $item[$current_index];?>" width="250px"/>
<br>, Image No: <?php echo ($current_index + 1);?>
</body>
</html>