如何在帖子中显示相册? [仅使用PHP和MYSQL] [英] How to display image albums in posts? [using PHP and MYSQL only]

查看:61
本文介绍了如何在帖子中显示相册? [仅使用PHP和MYSQL]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php和mysql编程的新手,并且是该论坛的新手.我的网站项目存在重大问题.我一直在整个互联网上搜索解决方案,但没有成功.我希望这里有一些专家可以帮助我解决我的问题.

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:

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>

我该怎么做才能获得示例中的输出? -我的多对一关系错了吗? -我必须使用多对多关系吗?如果是这样,怎么办? -我的mysqli_query选择错误吗?

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?

我在另一个论坛(和其他图片画廊主题,但有所不同)上冲了一个人,他说,他不确定,但他认为只能在php脚本中的html标签中显示1张图片( 'img'),它将与单独的表一起使用,但是我在做什么错了?

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 ?

推荐答案

您的第一个SQL查询很好,但对于第二个SQL查询,您只想在while循环的外部选择当前帖子(行)的图像,因此第二个SQL查询应该是:

Your first SQL query is fine, but for the second one, you only want to select the images for the current post (row) in the outer while loop, so the second SQL query should be:

"SELECT img_file, img_title FROM images WHERE post_id = $row[id_post]"

已更新,仅显示了post1的img3:

<!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\">";

    if ($row['id_post'] == 1) {
        $resultx = mysqli_query($db, "SELECT img_file, img_title FROM images WHERE post_id = " .$row['id_post']. " AND img_title = 'img3'");
    } else {
        $resultx = mysqli_query($db, "SELECT img_file, img_title FROM images WHERE post_id = " .$row['id_post']);
    }

     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>

我已将第二个SQL查询放在if语句中,该语句检查当前帖子的ID是否等于1.如果是,则SQL查询仅选择其中带有"img3"的行. (如果不是,它会执行选择所有行的上一个SQL查询.)

I've placed the second SQL query in an if statement, which checks if the id of the current post is equal to 1. If it is, then the SQL query only selects the row with "img3" in it. (And if it's not it executes the previous SQL query which selects all the rows.)

当然,仅当您知道帖子的ID和要显示的图像的标题时,此方法才有效.总是只显示第一篇文章的第三张图片的更通用的解决方案是这样的:

Of course this only works if you know the id of the post and the title of the image that you want to display. A more generic solution for always displaying only the third image of the first post would be something like this:

<!DOCTYPE html>
<html>
<body>
<?php
$db = mysqli_connect("localhost", "root", "", "post_images");    

$result = mysqli_query($db, "SELECT * FROM posts ORDER BY id_post");
$rows = mysqli_fetch_all($result,MYSQLI_ASSOC);
foreach ($rows as $key => $value)
   echo "<div class=\"post_container\">";
     echo $row['post_title'];
     echo "<div class=\"image_container\">";

    if ($key == 0) {
        $resultx = mysqli_query($db, "SELECT img_file, img_title FROM images WHERE post_id = " .$row['id_post']. " ORDER BY id_img LIMIT 1 OFFSET 2");
    } else {
        $resultx = mysqli_query($db, "SELECT img_file, img_title FROM images WHERE post_id = " .$row['id_post']);
    }

     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>

在这里,我将第一个SQL查询的整个结果存储在一个数组中,因为然后数组的键与每个帖子的数目减一个相对应.如果$ key = 0,则当前行是第一行,然后我们使用SQL查询,该查询仅从images表中选择第三张图像.如果$ key不为0,我们将使用另一个SQL查询来选择所有图像.

Here I've stored the entire result of the first SQL query in an array, because then the keys of the array correspond with the number of each post minus one. If $key = 0, the current row is the first post and then we use the SQL query that selects only the third image from the images table. If $key isn't 0 we use the other SQL query that selects all of the images.

在新的SQL查询中,LIMIT 1表示仅选择1行,而OFFSET 2表示从第3行开始(计数从0开始,因此offset 2返回第3行).

In the new SQL query, LIMIT 1 means select 1 row only, and OFFSET 2 means start with row 3 (counting starts at 0, so offset 2 returns row 3).

我添加了ORDER BY id_img以确保始终以相同的顺序返回图像,即它们被添加到数据库的顺序. (而且我在第一个查询中对id_post做了同样的操作.)

I've added ORDER BY id_img to make sure images are always returned in the same order, the order in which they were added to the database. (And I've done the same with id_post in the first query.)

这篇关于如何在帖子中显示相册? [仅使用PHP和MYSQL]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆