Hi, I want to use the galleria plugin to show images on a site. Does anyone know how to get it to read from a mysql database table?
Thanks..............
Hi, I want to use the galleria plugin to show images on a site. Does anyone know how to get it to read from a mysql database table?
Thanks..............
As pointed out, the gallery needs a server-side step to turn rows from MySQL into the HTML (or JSON) that Galleria expects. With PHP you can follow two simple, safe patterns: 1) render the gallery markup server-side (a container of linked images), or 2) expose a JSON endpoint and use Galleria's dataSource. ' follow-up shows that either approach works; below are minimal, practical examples plus a short troubleshooting checklist.
Server-rendered HTML (embed on the page):
<?php
$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8mb4','dbuser','dbpass',[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$stmt = $pdo->query('SELECT filename, thumb, title, caption FROM gallery ORDER BY id');
echo '<div id="galleria">';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$image = htmlspecialchars($row['filename'], ENT_QUOTES, 'UTF-8');
$thumb = htmlspecialchars($row['thumb'] ?: $row['filename'], ENT_QUOTES, 'UTF-8');
$title = htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8');
$caption = htmlspecialchars($row['caption'], ENT_QUOTES, 'UTF-8');
echo '<a href="'.$image.'"><img src="'.$thumb.'" data-title="'.$title.'" data-description="'.$caption.'"></a>';
}
echo '</div>';
?> JSON endpoint + client-side load:
<?php
header('Content-Type: application/json; charset=utf-8');
$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8mb4','dbuser','dbpass',[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$stmt = $pdo->query('SELECT filename, thumb, title, caption FROM gallery ORDER BY id');
$data = [];
while ($r = $stmt->fetch(PDO::FETCH_ASSOC)) {
$data[] = ['image'=>$r['filename'],'thumb'=>$r['thumb']?:$r['filename'],'title'=>$r['title'],'description'=>$r['caption']];
}
echo json_encode($data);
?> Then on the page (after including jQuery and Galleria core/theme):
Galleria.loadTheme('/path/to/galleria.classic.min.js');
Galleria.run('#galleria',{ dataSource: '/images.json.php' }); Troubleshooting checklist:
Galleria.run. htmlspecialchars for output to avoid XSS. Jump to Post— JorgeM 958Does anyone know how to get it to read from a mysql database table?
You will need servers-side scripting to read from the database. Do you know PHP, ASP.NET, Java, etc..?
Does anyone know how to get it to read from a mysql database table?
You will need servers-side scripting to read from the database. Do you know PHP, ASP.NET, Java, etc..?
Yeah, I know a bit of PHP. I'm unclear how to use it with galleria though. Do I query the db inside the galleria code?
I got it to work!
Glad you resolved it even though you didnt get much help on this one.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.