php 在php中更改图像src?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9218744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change image src in php?
提问by user1200640
I beginner in php and I am making small project which will help me learn more php. any way on webserver I have uploaded pictures which in this format:
我是 php 初学者,我正在做一个小项目,这将帮助我学习更多的 php。我在网络服务器上以任何方式上传了以下格式的图片:
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
and so on....
等等....
in the body of my page I have:
在我的页面正文中,我有:
<?php
$imageNumber = 1;
?>
<img src="'$imageNumber'.'.jpg'">
why is this code is not working ?
also I want to create a function that every time the user click a button the $imageNumber
get incremented by 1
为什么这段代码不起作用?我还想创建一个函数,每次用户单击按钮时,$imageNumber
都会增加1
回答by Kato
You can only use PHP variables inside PHP tags.
您只能在 PHP 标签内使用 PHP 变量。
<?php
$imageNumber = 1;
echo '<img src="'.$imageNumber.'.jpg'">';
?>
Or
或者
<?php
$imageNumber = 1;
?>
<img src="<?php echo $imageNumber ?>.jpg">
For the function, use javascript. jQuery would be simplest, but I've included a raw version too:
对于该功能,请使用 javascript。jQuery 将是最简单的,但我也包含了一个原始版本:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
</head>
<body>
<img id="the_image" src="1.jpg">
<input type="button" id="the_button" value="change" />
<script type="text/javascript">
var maxNumImages = 5;
// for the function, use javascript. jQuery would be simplest:
jQuery(function($) {
$('#the_button').click(function() {
var num = ($('#the_image').attr('src').match(/(\d+).jpg$/)||[false])[1];
if( num !== false ) {
if( num == maxNumImages ) { num = 0; }
$('#the_image').attr('src', (++num)+'.jpg');
}
});
});
</script>
<img id="the_image2" src="1.jpg">
<input type="button" id="the_button2" value="change" />
<script>
// or in old fashioned (i.e. boring,sad,pathetic,vanilla) js:
var maxNumImages = 5;
var button = document.getElementById('the_button2');
button.onclick = function() {
var image = document.getElementById('the_image2');
console.log(image.src);
var num = (image.src.match(/(\d+).jpg$/)||[false])[1];
console.log(num);
if( num !== false ) {
if( num == maxNumImages ) { num = 0; }
image.src = (++num)+'.jpg';
}
}
</script>
</body>
</html>
回答by Wouter J
I hope this is not your only code??
我希望这不是你唯一的代码??
- You forget an
echo
(orprint
) - Strange single/dubbel quotes
- strange dot
- 你忘记了
echo
(或print
) - 奇怪的单引号/双引号
- 奇怪的点
You are searching for something like:
您正在寻找类似的东西:
<img src="<?php echo $imageNumber; ?>.jpg">
And to increase a number use ++
并增加一个数字使用 ++
$i = 0;
echo ++$i; // echo's 1 (increase the number and print it)
echo $i++; // echo's 1 (print it and then increase the number)
echo $i; // echo's 2 (print the number)
And for a click on a button you can better use JS:
单击按钮,您可以更好地使用 JS:
<img src="1.jpg" id="img-id">
<button id="increaseImg">Increase img</button>
<script>
var img = document.getElementById('img-id'), // get the img tag
btn = document.getElementById('increaseImg'), // get the button tag
i = 1; // the number
btn.onclick = function() {
img.src = ++i + '.jpg'; // each click increase i and change the img src
};
</script>
回答by MC Emperor
PHP code only works insidethe PHP opening and closing tags (<?php ?>
).
PHP 代码仅在PHP 开始和结束标记 ( <?php ?>
) 内有效。
The following block works:
以下块有效:
<?php
$imageNumber = 1;
?>
<img src="<?php echo $imageNumber; ?>.jpg" />
Everything inside the PHP tags will be interpreted by PHP, the rest remains unaffected.
With the line <?php echo $imageNumber; ?>
, PHP will echo('send to the browser') the variable $imageNumber. Thus, the browser the receives this:
PHP 标签内的所有内容都将由 PHP 解释,其余部分不受影响。
使用该行<?php echo $imageNumber; ?>
,PHP 将回显('发送到浏览器')变量 $imageNumber。因此,浏览器接收到:
<img src="1.jpg" />
回答by Farray
$variables don't magically work outside of a PHP code block.
$variables 不会在 PHP 代码块之外神奇地工作。
The following approaches would do what you're trying to do:
以下方法可以完成您想要做的事情:
<?php
$imageNumber = 1;
echo '<img src="' . $imageNumber . '.jpg">';
?>
Or
或者
<?php
$imageNumber = 1;
?>
<img src="<?php echo $imageNumber; ?>.jpg">
回答by Zenexer
<?php
$imageNumber = 1;
?>
<img src="<?php echo $imageNumber; ?>.jpg" />
OR
或者
<?php
$imageNumber = 1;
echo "<img src='$imageNumber.jpg' />";
?>
OR
或者
<?php
$imageNumber = 1;
echo '<img src="' . $imageNumber . '.jpg" />';
?>
Don't use PHP for the click button thing. Use JavaScript.
不要将 PHP 用于单击按钮的事情。使用 JavaScript。
回答by circusdei
<?php
$imageNumber = 1;
?>
<img src="<?php echo $imageNumber;?>.jpg">
回答by thetaiko
Try this. PHP variables will only be parsed inside the php tags.
尝试这个。PHP 变量只会在 php 标签内解析。
<?php
$imageNumber = 1;
echo "<img src=\"{$imageNumber}.jpg\">";
?>
回答by j08691
You have set the variable $imageNumber properly, however you then need to output it with echo:
您已经正确设置了变量 $imageNumber,但是您需要使用 echo 输出它:
<?php
$imageNumber = 1;
?>
<img src="<?php echo $imageNumber; ?>.jpg">
回答by Harry Forbess
<img src="<?PHP echo $imageNumber ?>.jpg">
回答by Александр Сонич
First you need Array on images, than calculate this array, for each keyitem of array create template your ".jpg alt="echo url " />
首先你需要图像上的数组,然后计算这个数组,为数组的每个关键项创建模板你的“.jpg alt="echo url" />
`
`
$images= array("1", "2", "3", "4", "5", "6");
shuffle($images);
foreach ($images as $img) {
echo "<img src='imagesDirectory/$img.jpg'> <br>";
}
`
`