php 自动创建目录_使用PHP创建自动投资组合

php 自动创建目录

So far our have required us to write new HTML code in order to present an image in the gallery, or at least the thumbnail thereof. This means that every time that we upload a new image to the site, we would have to write more HTML to add it to our page.

到目前为止,我们的要求我们编写新的HTML代码 ,以便在画廊中显示图像或至少显示其缩略图。 这意味着,每次我们将新图像上传到网站时,我们都必须编写更多HTML才能将其添加到页面中。

But our random image lesson showed that we can read images from a directory… so it follows that we should be able to automate the entire process of presenting a gallery of images with PHP.

但是我们的随机图像课程表明,我们可以从目录中读取图像…因此,我们应该能够自动化使用PHP呈现图像库的整个过程。

Let’s take the core of the random image code:

让我们看一下随机图像代码的核心:

<?php $imagedir = dir('assets/images');
$count = 1;
$pattern="/\.(gif|jpg|jpeg|png)$/";
while(($file = $imagedir->read())) {
	if (preg_match($pattern, $file)) {
		$imagearray[$count] = $file;
		$count++; 
	} 
} ?>

This creates our array as before. But this time, we’re not getting a random image from $imagearray, but the first image. Doing that alone would be easy:

像以前一样创建数组。 但是这一次,我们没有从$imagearray获得随机图像,而是第一张图像。 一个人做会很容易:

<img src="assets/images/<?php echo $imagearray[1]; ?>" alt>

Rather than leaving the alt attribute value blank, you should fill it with something. Ideally, you could use a variation of the string manipulation techniques I introduced in the self-made pages lesson to create values based on the file name of the image.

而不是离开alt属性值为空,你应该用什么填充它。 理想情况下,您可以使用我在自制页面课程中介绍的各种字符串处理技术来基于图像的文件名创建值。

We want to make what selects the image a variable, so let’s create one called $pic and set it to a default value of 1 if no value for it exists:

我们要使选择图像的变量成为变量,所以我们创建一个称为$pic的变量,如果不存在它的值,则将其设置为默认值1

<?php if (!$pic) { $pic = 1; } ?> 
<img src="assets/images/<?php echo $imagearray[$pic]; ?>" alt>

Now we want to allow the user to change the value of $pic. The easiest way to do that is via clicking on a link which passes a GET variable back to the page. First, we’ll add a line of code to the section above:

现在,我们要允许用户更改$pic的值。 最简单的方法是单击链接,该链接将GET变量传递回页面。 首先,我们将在上面的部分中添加一行代码:

<?php $pic = $_GET['pic'];
if (!$pic) { $pic = 1; } ?> 
<img src="assets/images/<?php echo $imagearray[$pic]; ?>" alt>

Then create our link, which will increment whatever value $pic has by 1:

然后创建我们的链接,它将使$pic任何值增加1

<a href="?pic=<?php echo ($pic + 1); ?>">Next</a>

This will advance our images through the array with each click on the Next link. (As always, one of the easiest ways to understand this is to look at the source code delivered from the browser). There’s just one problem: we can keep clicking the Next button past the actual number of images we have.

每次单击“ 下一步”链接,这将使图像通过阵列前进。 (一如既往,最简单的方法之一就是查看浏览器提供的源代码)。 仅有一个问题:我们可以在实际拥有的图像数之后继续单击“ 下一步”按钮。

接口设计规则第1条 (Interface Design Rule No. 1)

If an interface element can’t be used, or if it isn’t appropriate at a certain point, it shouldn’t be seen. To solve this issue, we’ll make the appearance of the Next button conditional:

如果不能使用接口元素,或者在某个时候它不合适,则不应看到它。 要解决此问题,我们将使“ 下一步”按钮的出现成为条件:

<?php if ($pic < $count - 1) { ?>
	<a href="?pic=<?php echo ($pic + 1); ?>">Next</a>
?php } ?>

翻译自: https://thenewcode.com/214/Create-an-Automatic-Portfolio-With-PHP

php 自动创建目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值