i am trying to populate an image holder

All WYSIWYG Web Builder support issues that are not covered in the forums below.
Forum rules
IMPORTANT NOTE!!

DO YOU HAVE A QUESTION OR PROBLEM AND WANT QUICK HELP?
THEN PLEASE SHARE A "DEMO" PROJECT.



PLEASE READ THE FORUM RULES BEFORE YOU POST:
http://www.wysiwygwebbuilder.com/forum/viewtopic.php?f=12&t=1901

MUST READ:
http://www.wysiwygwebbuilder.com/getting_started.html
WYSIWYG Web Builder FAQ
Post Reply
kniggles
 
 
Posts: 4
Joined: Mon Feb 22, 2021 10:18 pm

i am trying to populate an image holder

Post by kniggles »

Hi, all,
i am trying to fill the conveyer belt with all the images from a mysql database, i am getting stuck, has anyone got any tips?
User avatar
Pablo
 
Posts: 23249
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: i am trying to populate an image holder

Post by Pablo »

I'm sorry, there is no standard option to load images from a MySQL database.
This will require a custom script.
User avatar
BaconFries
 
 
Posts: 5877
Joined: Thu Aug 16, 2007 7:32 pm

Re: i am trying to populate an image holder

Post by BaconFries »

As mentioned by Pablo this would require a "Custom" script. So with some help from AI the following was generated. Note:this is for reference only and just to point you in one direction.
From AI:
"To display images in a conveyor belt (carousel) gallery from a MySQL database, you can follow these steps. I'll outline how to set up the database, retrieve the images, and create a carousel using HTML, CSS, and JavaScript. We'll use PHP to interact with the MySQL database and serve the image data."

### Step 1: Set Up the MySQL Database

1. **Create the Database and Table**:

```sql

Code: Select all

   CREATE DATABASE image_gallery;
   USE image_gallery;

   CREATE TABLE images (
       id INT AUTO_INCREMENT PRIMARY KEY,
       image_url VARCHAR(255) NOT NULL
   );
   
```

2. **Insert Image Data**:

```sql

Code: Select all

   INSERT INTO images (image_url) VALUES
   ('image1.jpg'),
   ('image2.jpg'),
   ('image3.jpg');
   
```

### Step 2: Create PHP Script to Fetch Images

1. **Create a PHP Script to Fetch Images from the Database** (`fetch_images.php`):

```php

Code: Select all

<?php
   $servername = "localhost";
   $username = "root"; // Replace with your database username
   $password = ""; // Replace with your database password
   $dbname = "image_gallery";

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);

   // Check connection
   if ($conn->connect_error) {
       die("Connection failed: " . $conn->connect_error);
   }

   $sql = "SELECT image_url FROM images";
   $result = $conn->query($sql);

   $images = array();

   if ($result->num_rows > 0) {
       while ($row = $result->fetch_assoc()) {
           $images[] = $row['image_url'];
       }
   }

   $conn->close();
   echo json_encode($images);
   ?>
   
```
### Step 3: Create the HTML, CSS, and JavaScript for the Carousel

1. **Create the HTML Structure** (`index.html`):

```html
<!DOCTYPE html>
<html>
<head>
<title>Image Carousel</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>

Code: Select all

<div class="carousel-container">
           <div class="carousel" id="carousel"></div>
           <button class="prev" onclick="moveSlide(-1)">&#10094;</button>
           <button class="next" onclick="moveSlide(1)">&#10095;</button>
       </div>

       <script src="script.js"></script>
</body>
</html>
```

2. **Style the Carousel with CSS** (`styles.css`):

```css

Code: Select all

body {
       font-family: Arial, sans-serif;
   }

   .carousel-container {
       position: relative;
       max-width: 600px;
       margin: auto;
       overflow: hidden;
   }

   .carousel {
       display: flex;
       transition: transform 0.5s ease-in-out;
   }

   .carousel img {
       max-width: 100%;
       display: block;
   }

   .prev, .next {
       position: absolute;
       top: 50%;
       transform: translateY(-50%);
       background-color: rgba(0, 0, 0, 0.5);
       color: white;
       border: none;
       padding: 10px;
       cursor: pointer;
       z-index: 1;
   }

   .prev {
       left: 0;
   }

   .next {
       right: 0;
   }
   
```

3. **Create the JavaScript for Carousel Functionality** (`script.js`):

```javascript
let currentIndex = 0;

Code: Select all

   function fetchImages() {
       fetch('fetch_images.php')
           .then(response => response.json())
           .then(images => {
               const carousel = document.getElementById('carousel');
               images.forEach(imageUrl => {
                   const img = document.createElement('img');
                   img.src = imageUrl;
                   carousel.appendChild(img);
               });
           })
           .catch(error => console.error('Error fetching images:', error));
   }

   function moveSlide(direction) {
       const carousel = document.getElementById('carousel');
       const images = carousel.querySelectorAll('img');
       currentIndex = (currentIndex + direction + images.length) % images.length;
       const offset = -currentIndex * 100;
       carousel.style.transform = `translateX(${offset}%)`;
   }

   document.addEventListener('DOMContentLoaded', fetchImages);
   
```
### Explanation

1. **Database Setup**:
- Create a database and table to store the image URLs.
- Insert image URLs into the database.

2. **PHP Script**:
- Connect to the MySQL database and fetch image URLs.
- Encode the image URLs as JSON and send them as a response.

3. **HTML, CSS, and JavaScript**:
- HTML: Create the structure for the carousel and navigation buttons.
- CSS: Style the carousel and navigation buttons.
- JavaScript: Fetch image URLs from the PHP script, create image elements, and handle the carousel functionality.
Final word from AI
By following these steps, you'll have a responsive conveyor belt gallery that displays images from a MySQL database. If you have any further questions or need additional assistance, feel free to ask!
Post Reply