PHP & MySQL: fetch images from database

99封情书 提交于 2019-12-20 19:47:09

问题


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.


回答1:


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/c376437e2a45598b2f4d89eae4f‌​191e8.png8069756be5095978123ae51fadbffe3b.pnguploads/c376437e2a45598b2f4d89eae4f‌​191e8.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/c376437e2a45598b2f4d89eae4f‌​191e8.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! :)




回答2:


this is all the codes in the link:

  1. I fetch 4 images in ACTIVE corousel holder and 4 in ITEM. So, there are 2 slides which contains 4 images inside it.
  2. I use PDO to handle it. it's so simple scripting with little errors and easy to debug.

CONNECT TO database with PDO:

<?php
$dsn = 'mysql:dbname=database_name;host=localhost';
$user = 'username';
$password = 'password';
try {$mydb = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING)); }
catch (PDOException $e){echo 'Database Server Goes Down or Not Available!';}
?>

then, fetch the images:

<div class="item active">
                                            <?php
                                            $queryz = $mydb->prepare("SELECT * FROM table_name WHERE item=:item ORDER BY id ASC LIMIT 4,8");
                                            $queryz->execute();
                                            $data = $queryz->fetchAll();
                                            foreach ($data as $row){
                                                $id=$row['id'];
                                                $imagez=$row['imagez'];
                                                ?>
                                                <div class="col-md-3 col-sm-6 hero-feature">
                                                    <div class="thumbnail">
                                                        <?php
                                                        if ($imagez){?>
                                                        <img src="<?php echo htmlspecialchars($row['imagez']); ?>" alt="" class="img-thumbnail"/>
                                                        <?php }else{?>
                                                        <img src="../myimage/defaultimg.jpg" alt="" class="img-thumbnail"/>
                                                        <?php }?>
                                                        <div class="caption">
                                                            <h5 class="pull-left"><b><?php echo htmlspecialchars($row['prod_name']); ?></b></h5>
                                                            <h5 class="pull-right">Rp.<?php echo htmlspecialchars($row['price']); ?></h5>
                                                            <h6>discz: <b><?php echo htmlspecialchars($row['discz']); ?>%</b></h6>
                                                            <p><?php echo htmlspecialchars($row['notez']); ?></p>
                                                            <p><a href="../data.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Beli</a> <a href="../senddata.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-default">Info &raquo;</a>
                                                            </p>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php } ?>
                                            </div>
                                            <div class="item">
                                            <?php
                                            $queryz = $mydb->prepare("SELECT * FROM table_name WHERE item=:item ORDER BY RAND() LIMIT 4,8");
                                            $queryz->execute();
                                            $data = $queryz->fetchAll();
                                            foreach ($data as $row){
                                                $id=$row['id'];
                                                $imagez=$row['imagez'];
                                                ?>
                                                <div class="col-md-3 col-sm-6 hero-feature">
                                                    <div class="thumbnail">
                                                        <?php
                                                        if ($imagez){?>
                                                        <img src="<?php echo htmlspecialchars($row['imagez']); ?>" alt="" class="img-thumbnail"/>
                                                        <?php }else{?>
                                                        <img src="../myimage/defaultimg.jpg" alt="" class="img-thumbnail"/>
                                                        <?php }?>
                                                        <div class="caption">
                                                            <h5 class="pull-left"><b><?php echo htmlspecialchars($row['prod_name']); ?></b></h5>
                                                            <h5 class="pull-right">Rp.<?php echo htmlspecialchars($row['price']); ?></h5>
                                                            <h6>discz: <b><?php echo htmlspecialchars($row['discz']); ?>%</b></h6>
                                                            <p><?php echo htmlspecialchars($row['notez']); ?></p>
                                                            <p><a href="../data.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Beli</a> <a href="../senddata.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-default">Info &raquo;</a>
                                                            </p>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php } ?>
                                            </div>

Then, you focus on how to fetch the image. the code is like in this following:

                                    <

div class="item active">
                                            <?php
                                            $queryz = $mydb->prepare("SELECT * FROM table_name WHERE item=:item ORDER BY id ASC LIMIT 4,8");
                                            $queryz->execute();
                                            $data = $queryz->fetchAll();
                                            foreach ($data as $row){
                                                $id=$row['id'];
                                                $imagez=$row['imagez'];
                                                ?>
                                                <div class="col-md-3 col-sm-6 hero-feature">
                                                    <div class="thumbnail">
                                                        <?php
                                                        if ($imagez){?>
                                                        <img src="<?php echo htmlspecialchars($row['imagez']); ?>" alt="" class="img-thumbnail"/>
                                                        <?php }else{?>
                                                        <img src="../myimage/defaultimg.jpg" alt="" class="img-thumbnail"/>
                                                        <?php }?>
                                                        <div class="caption">
                                                            <h5 class="pull-left"><b><?php echo htmlspecialchars($row['prod_name']); ?></b></h5>
                                                            <h5 class="pull-right">Rp.<?php echo htmlspecialchars($row['price']); ?></h5>
                                                            <h6>discz: <b><?php echo htmlspecialchars($row['discz']); ?>%</b></h6>
                                                            <p><?php echo htmlspecialchars($row['notez']); ?></p>
                                                            <p><a href="../data.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-primary"><span class="glyphicon glyphicon-shopping-cart"></span> Beli</a> <a href="../senddata.php?pcode=<?php echo htmlspecialchars($row['cdz']); ?>" class="btn btn-default">Info &raquo;</a>
                                                            </p>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php } ?>
                                            </div>

The PATH OF THE IMAGES stored in:

$imagez=$row['imagez'];

which has the same directory with

../myimage/defaultimg.jpg"

Note: I'm so sorry for not expert in explanation, but that's I wrote the code in the web page. But, I suggest you to copy the code and change what you need to then try again.




回答3:


Change your echo statement with :

echo "<div class='fill' style='background-image:url(http://devoirtechnologies.in/localhealthorganization/".$item.");'></div>";

and also set the width and height of this div. Because default height of the div is 0 (zero).




回答4:


Try this it will help :

Use this :

<img src="<?php echo $row["offimage"]; ?>"></img>

instead of

$str= $row["offimage"];

if you want to display the image.

Code :

<?php
    $con=mysqli_connect("localhost","root","","db");
    // Check connection
    if (mysqli_connect_errno())     {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $parentid = $_GET['country'];
    $id = $_GET['fid'];

    $sql = "SELECT * FROM register_office WHERE id='".$id."'";
    $result = mysqli_query($con, $sql);
    if (mysqli_num_rows($result) > 0) 
        {
            echo "<div class='item active'>";
               do
                    {
                     $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>";
                            }   
                  } while($rs = mysqli_fetch_array($result));
                echo "</div>";

        }
    else 
        {
            echo "0 results";
        }
?>



回答5:


If you see only one image it means that your explode contains an array with only one item. For test replace
$array = explode("*", $str); with $array = explode("*", "iamge1.png*iamge2.png"); and you should see two images.

Code:

<?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'>";

            while($row = mysqli_fetch_assoc($result)) 
            {
                // $str = $row["offimage"];
                $str = "iamge1.png*image2.png";

                $array =  explode('*', $str);
                foreach ($array as $item) 
                {
                    echo "<div class='item'>";
                    echo "<div class='fill'>";
                    echo "<img src=\"http://example.com/abc/" . trim($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>";
        }
?>



回答6:


if you want from jquery create a div and then append using .html( img html tag in quotes )




回答7:


$sql = "SELECT * FROM register_office WHERE id='".$idd."'";

This can only select 1 item from database.




回答8:


     while($row = mysqli_fetch_assoc($result)) 
        {  echo"<div class='item'>";                                    
           echo "<div class='fill'>";
           echo "<img src=\"http://example.com/abc/" .$row['offimage'] . "\" height=\"500\" width=\"2000\"/>"; 
           echo "</div>"; echo"</div>";
        }   

Used like that it will work



来源:https://stackoverflow.com/questions/27504281/php-mysql-fetch-images-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!