-1

I am trying to fetch data from a Mysql database (specifically images) and then display them in a carousel, like for example on the amazon.com homepage.

I am a beginner in CSS. I haven't used it much.

I have looked at a earlier question, but mine is not a list of images together. I just through the data from the database.

Any help will be appreciated.

Community
  • 1
  • 1
chepkeitany
  • 11
  • 1
  • 3

2 Answers2

0

From what I understood in your comment below, your problem is strictly related to CSS. The images are showing vertically because the <li> aren't floated next to each other.

Here's the code:

<html>
    <head>
        <title>Title</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript" src="/path/to/jquery.jcarousel.js"></script>
        <style type="text/css">
        .jcarousel {position:relative; overflow:hidden;}
        .jcarousel ul {width: 20000em; position:relative; list-style:none; margin:0; padding:0;}
        .jcarousel li {float:left;}
        </style>
    </head>
    <body>
        <div class="jcarousel">
            <ul>
                <li><img src="image1.jpg" alt="" /></li>
                <li><img src="image2.jpg" alt="" /></li>
            </ul>
        </div>
        <script type="text/javascript">
            $(function(){
                $('.jcarousel').jcarousel();
            });
        </script>
    </body>
</html>
Wissam El-Kik
  • 2,469
  • 1
  • 17
  • 21
  • I have no problem with php...my challenge is that the images are display vertically in my web page instead of horizontally with the carousel controls previous, left, slide etc. – chepkeitany Jan 27 '15 at 17:25
  • If you have no problem with PHP, then you shouldn't be tagging the question with "php". If the carousel is displayed vertically instead of vertically, then the problem is a CSS, HTML or JS problem. Please create a JSFiddle to show us the current code: http://jsfiddle.net/ – Wissam El-Kik Jan 27 '15 at 20:05
  • @user3231736 You shouldn't be writing PHP code in JSFiddle. JSFiddle is only used for HTML, CSS and Javascript. In your case, you could open the webpage you're talking about, click on a right-click button and select "View Page Source", then copy all the HTML code and paste it in JSFiddle. The good thing is that I checked your code and it seems that you forgot to include the CSS file of jCarousel :) There's no CSS at all. I'm gonna edit the answer above. – Wissam El-Kik Jan 28 '15 at 08:50
  • Does carousel have default css file? – chepkeitany Jan 28 '15 at 09:19
  • No, there's no default file CSS, but there's a minimum of CSS styles that you need to include: http://sorgalla.com/jcarousel/docs/reference/installation.html#style-the-carousel I shared them in the answer above. – Wissam El-Kik Jan 28 '15 at 09:27
  • Thank you very much...now that works...what I need to find out now is adding the controls... – chepkeitany Jan 28 '15 at 10:55
  • Look into: http://sorgalla.com/jcarousel/docs/ The configuration seems really bad, because you might need an extra plugin to add arrows ... I would recommend [bxslider](http://bxslider.com/) and [flex slider](http://www.woothemes.com/flexslider/) for having all the features you can imagine ... and for free! – Wissam El-Kik Jan 28 '15 at 11:16
0

I know this is an old question but it might be useful for future users. Here is a simple solution to implement a Bootstrap carousel to display images from database:

Fetch all imageID from the database and insert it in an array (note that I store the images as BLOB in my database:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <script src="jquery-3.1.0.min.js"></script>
    <script src="bootstrap/js/bootstrap.js"></script>
    <style>
        .myCarousel{
            height: 100%;
            width: 700px ;
            margin-top: 5px;
        }
        .slideimage{

            width: 700px;
            height: 320px !important;
        }
    </style>
</head>
<body>


<?php

include ('DBconnection.php');

$ImageArray = array();

$queryImages = "SELECT * FROM imagetable ";

$result = mysqli_query($conn,$queryImages);

if(mysqli_num_rows($result) >0){

    while ($row = mysqli_fetch_array($result)){

        $ImageArray[] = $row;
    }
}

?>
                         <div id="myCarousel"  class="myCarousel carousel
                                    slideCarousel" data-ride="carousel" data-interval="5000" >

                                        <ol class="carousel-indicators">

                                        <?php

                                            // creating indicators - note that at least one must be set as active

                                            for($j=0;$j<count($ImageArray);$j++){

                                                    if($j==0){
                                                        echo ' <li data-target="#myCarousel" data-slide-to="'.$j.'" class="active"></li>';

                                                    }else{

                                                        echo ' <li data-target="#myCarousel" data-slide-to="'.$j.'"></li>';

                                                    }
                                            }
                                        ?>


                                        </ol>
                                        <div class="carousel-inner" role="listbox">



                                    <?php

                                          for($j=0;$j<count($ImageArray);$j++){
                                                 //If it is the first image set it as active
                                                  if($j==0){
                                                      echo '<div class="item active">

                                                            <!--Using image id and url  -->

                                                          <img class="slideimage" src="GetImageID.php?id=' .$ImageArray[$j]['ImageID'].'" /> 

                                                           <!-- or using base64_encode

                                                           <img src="data:image/jpeg;base64,'.base64_encode($ImageArray[$j]['Image']).'"/>

                                                           -->
                                                            </div>';
                                                  }

                                                  else{
                                                      echo '<div class="item">
                                                              <img class="slideimage" src="GetImageID.php?id=' .$ImageArray[$j]['ImageID'].'" /> 
                                                            </div>';
                                                  }
                                          }
                                            ?>


                                        </div>


                                        <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                            <span class="sr-only">Previous</span>
                                        </a>
                                        <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                                            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                            <span class="sr-only">Next</span>
                                        </a>
                                    </div>
</body>
</html>

The GetImageID.php:

include ('DBconnection.php');

$imageid = $_GET['id'];

$query = "Select Image from imagetable WHERE ImageID = '$imageid'";

$result = mysqli_query($conn,$query);

$row = mysqli_fetch_array($result);

header("Content-type: image/jpeg");

echo $row['Image'];