I'm new to php and mysql programming and new to this forum. I have a major problem with my website project. I've searched the whole internet for a solution, with no success. I hope here are some experts who can help me with my problem.
I want to learn, how to create a posting system, which does display image albums. By scrolling through posts, the posts should display a complete album each. It should looks like this example:
+-------------------------+
| Post1: Title1...........|
| img1....................|
| img2....................|
| img3....................|
+-------------------------+
+-------------------------+
| Post2: Title2...........|
| img4....................|
| img5....................|
| img6....................|
+-------------------------+
+-------------------------+
| Post3: Title3...........|
| img7....................|
| img8....................|
| img9....................|
+-------------------------+
IMPORTANT: display the images as images, not as a text list.
What I got so far:
MYSQL: (2 Tables for a many-to-one relationship)
CREATE TABLE posts (
id_post int(11) not null AUTO_INCREMENT PRIMARY KEY,
post_title varchar(100),
post_descr varchar(100)
);
CREATE TABLE images (
id_img int(11) not null AUTO_INCREMENT PRIMARY KEY,
img_file varchar(100),
img_title text(100),
post_id int(11) not null REFERENCES posts(id_post)
);
PHP: (display.php)
<!DOCTYPE html>
<html>
<body>
<?php
$db = mysqli_connect("localhost", "root", "", "post_images");
$result = mysqli_query($db, "SELECT * FROM posts");
while ($row = mysqli_fetch_array($result)) {
echo "<div class=\"post_container\">";
echo $row['post_title'];
echo "<div class=\"image_container\">";
$resultx = mysqli_query($db, "SELECT img_file, img_title FROM images JOIN posts ON posts.id_post=images.post_id");
if(mysqli_num_rows($resultx) > 0) {
while ($rowx = mysqli_fetch_array($resultx)) {
echo "<img src='../folder_image_uploads/".$rowx['img_file']."' >";
echo $rowx['img_title'];
}
}
echo "</div>";
echo "</div>";
}
?>
</body
</html>
What do I have to do, to get the output like in my example? - is my many-to-one relationship wrong? - do i have to use a many-to-many relationship? If so, how? - is my mysqli_query selection wrong?
I red in a other forum (and other image gallery topic, but different) a guy who said, he is not sure, but he thinks it is only possible to display 1 image in a php script, in a html tag ('img') and it would work with a separate table, but what am I doing wrong ?