PHP & MySQL: fetch images from database

后端 未结 8 892
夕颜
夕颜 2021-02-08 13:06

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 c

相关标签:
8条回答
  • 2021-02-08 13:38
         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

    0 讨论(0)
  • 2021-02-08 13:39

    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.

    0 讨论(0)
  • 2021-02-08 13:42
    $sql = "SELECT * FROM register_office WHERE id='".$idd."'";
    

    This can only select 1 item from database.

    0 讨论(0)
  • 2021-02-08 13:45

    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>";
            }
    ?>
    
    0 讨论(0)
  • 2021-02-08 13:46

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

    0 讨论(0)
  • 2021-02-08 13:48

    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! :)

    0 讨论(0)
提交回复
热议问题