PHP& MySQL:从数据库中获取图像 [英] PHP & MySQL: fetch images from database
问题描述
我上传了多张图片,所有图片的路径都存储在一起了.
使用explode
我已经将它们分开,现在我希望在轮播中回显它们
我正在使用的代码是:
<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$idd = $_GET['id'];
echo "<header id='myCarousel' class='carousel slide'>";
/* Indicators */
echo"<ol class='carousel-indicators'>";
echo"<li data-target='#myCarousel' data-slide-to='0' ></li>";
echo"<li data-target='#myCarousel' data-slide-to='1'></li>";
echo"<li data-target='#myCarousel' data-slide-to='2'></li>";
echo"</ol>";
$sql = "SELECT * FROM register_office WHERE id='".$idd."'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
/* Wrapper for slides*/
echo"<div class='carousel-inner'>";
echo"<div class='item'>";
while($row = mysqli_fetch_assoc($result))
{
$str= $row["offimage"];
$array = explode('*', $str);
foreach ($array as $item)
{
echo "<div class='fill'>";
echo "<img src=\"http://example.com/abc/" . $item . "\" height=\"500\" width=\"2000\"/>";
echo "</div>";
}
echo"</div>";
echo"</div>";
}
/*Controls*/
echo"<a class='left carousel-control' href='#myCarousel' data-slide='prev'>";
echo"<span class='icon-prev'></span>";
echo"</a>";
echo"<a class='right carousel-control' href='#myCarousel' data-slide='next'>";
echo"<span class='icon-next'></span>";
echo"</a>";
echo"</header>";
?>
,但仅显示一张图像.同样,当我使用next控件时,即使我尝试向前或向后移动,此控件也不会显示任何图像.
这里可能存在一些问题...
1.爆炸()
首先,如果您的$row1["offimage"]
字符串在文件名之间没有星号*
,则您的explode()
可能不起作用.在对OP的回复评论中,您已经给我们提供了$row1["offimage"]
内容的示例,该示例不会用星号*
分隔每个PNG文件:
@lolka_bolka i get this outout, uploads/c376437e2a45598b2f4d89eae4f191e8.pnguploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.pnguploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.png04aaa414c21dc057bc594b896124068e.png
– user3732711 Dec 16 '14 at 12:24
为使脚本能够像您编写的那样正常工作,我们希望看到例如,您的$row1["offimage"]
字符串如下所示,其中带有星号*
分隔每个PNG文件名(及其目录路径) :
uploads/c376437e2a45598b2f4d89eae4f191e8.png*uploads/c376437e2a45598b2f4d89eae4f191e8.png*8069756be5095978123ae51fadbffe3b.png
2. DIV类"item"
作为丹皂"和为开发者Rohit"在这里回答了,要使轮播正常工作,您需要确保每个图像都包装在自己的item
类中.
您上面的代码在while
循环之前echo"<div class='item'>";
.
echo"<div class='item'>";
应该在while循环内 INSIDE -更具体地说,它应该在foreach
循环内-以便轮播中的每个图像都用item
类包装.>
请不要忘记,您希望首先显示的item
或图像(在加载时)也应该具有active
类以及item
类,例如:<div class="item active">
. /p>
3. JavaScript
您没有包含任何JavaScript供我们查看...如果不调用carousel()
函数,将不会发生任何事情,仅显示一个(静态)图像.您可能需要这样的东西:
<script>
$('.carousel').carousel({
interval: 3000
})
</script>
希望其中的一些/全部可以帮助您的旋转木马启动并运行! :)
I have uploaded multiple images and path of all the images have been stored together.
Using explode
I have separated them and now I wish to echo them in a carousel
Code that I am using is:
<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$idd = $_GET['id'];
echo "<header id='myCarousel' class='carousel slide'>";
/* Indicators */
echo"<ol class='carousel-indicators'>";
echo"<li data-target='#myCarousel' data-slide-to='0' ></li>";
echo"<li data-target='#myCarousel' data-slide-to='1'></li>";
echo"<li data-target='#myCarousel' data-slide-to='2'></li>";
echo"</ol>";
$sql = "SELECT * FROM register_office WHERE id='".$idd."'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
/* Wrapper for slides*/
echo"<div class='carousel-inner'>";
echo"<div class='item'>";
while($row = mysqli_fetch_assoc($result))
{
$str= $row["offimage"];
$array = explode('*', $str);
foreach ($array as $item)
{
echo "<div class='fill'>";
echo "<img src=\"http://example.com/abc/" . $item . "\" height=\"500\" width=\"2000\"/>";
echo "</div>";
}
echo"</div>";
echo"</div>";
}
/*Controls*/
echo"<a class='left carousel-control' href='#myCarousel' data-slide='prev'>";
echo"<span class='icon-prev'></span>";
echo"</a>";
echo"<a class='right carousel-control' href='#myCarousel' data-slide='next'>";
echo"<span class='icon-next'></span>";
echo"</a>";
echo"</header>";
?>
but it is showing only one image. Also when I use next control, after that it shows no image even if I try to move forward or back.
There could be a few problems here...
1. explode()
First, your explode()
may not work if your $row1["offimage"]
string doesn't have asterisks *
between the file names. In a reply comment to the OP you've given us an example of the contents of $row1["offimage"]
, which does NOT separate each PNG file by an asterisk *
:
@lolka_bolka i get this outout, uploads/c376437e2a45598b2f4d89eae4f191e8.pnguploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.pnguploads/c376437e2a45598b2f4d89eae4f191e8.png8069756be5095978123ae51fadbffe3b.png04aaa414c21dc057bc594b896124068e.png
– user3732711 Dec 16 '14 at 12:24
For your script to work as you've written it, we would expect to see, for example, your $row1["offimage"]
string looking like this, with an asterisk *
separating each PNG file name (with it's directory path):
uploads/c376437e2a45598b2f4d89eae4f191e8.png*uploads/c376437e2a45598b2f4d89eae4f191e8.png*8069756be5095978123ae51fadbffe3b.png
2. DIV class 'item'
As "Dan Soap" & "Rohit the developer" have answered here, for carousel to work, you need to make sure each image is wrapped in its own item
class.
Your code above has echo"<div class='item'>";
before the while
loop.
echo"<div class='item'>";
should be INSIDE the while loop - more specifically it should be inside your foreach
loop - so that each image in the carousel is wrapped with the item
class.
And don't forget, the item
or image you wish to have shown first (on load) should have active
class too, as well as the item
class, as such: <div class="item active">
.
3. JavaScript
You haven't included any JavaScript for us to look at... without calling the carousel()
function, nothing will happen, only one (static) image will show. You may need something like this:
<script>
$('.carousel').carousel({
interval: 3000
})
</script>
Hopefully one / some / all of these can help get your carousel up and running! :)
这篇关于PHP& MySQL:从数据库中获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!