我的index.php文件中的代码:
include('connect.php');
$query = 'SELECT * FROM forumPosts LEFT JOIN PostImages ON
forumPosts.DATETIME = PostImages.ImageDATETIME ORDER BY replyIndex
ASC';
$statement = $db->prepare($query);
$statement->execute();
$posts = $statement->fetchAll();
$statement->closeCursor();
echo "<table>";
foreach ($posts as $post){
if ($post['post_type'] == "r"){
$post_id = $post['post_id'];
echo "<tr bgcolor='beige'><td>reply</td><td>".
$post['post_title'] . "</td ><td>". $post['post_body'] . "
<img src='getImage.php?id=".$post['ID']."'>". "</td><td>".
$post['DATETIME']. "</td><td>". $post['replyIndex']. "</td>
<td>".$post['post_type']."</td>";
getImage.php文件:
<?php
include('connect.php');
$ID = $_GET['id'];
$query = "SELECT * FROM PostImages WHERE ID=:ID";
$statement = $db->prepare($query);
$statement->bindvalue(':ID', $ID);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);
header("Content-type: image/jpeg");
echo $row['image'];
知道如何查看图片吗? 下面的代码是图像的存储方式。
<?php
if ($image_size == FALSE){
//echo "image_size = false";
header('Location: index.php');
} else {
$query = "INSERT INTO PostImages (name, image, ImageDATETIME)
VALUES (:image_name, :image, :DATETIME)";
$statement = $db->prepare($query);
$statement->bindvalue(':image_name', $image_name);
$statement->bindvalue(':image', $image);
$statement->bindvalue(':DATETIME', $DATETIME);
$statement->execute();
$statement->closeCursor();
}
?>
答案 0 :(得分:2)
这是一个解决方案:
include('connect.php');
$query = 'SELECT * FROM forumPosts LEFT JOIN PostImages ON
forumPosts.DATETIME = PostImages.ImageDATETIME ORDER BY replyIndex
ASC';
$statement = $db->prepare($query);
$statement->execute();
$posts = $statement->fetchAll();
$statement->closeCursor();
echo "<table>";
foreach ($posts as $post){
if ($post['post_type'] == "r"){
$post_id = $post['post_id'];
echo "<tr bgcolor='beige'><td>reply</td><td>".
$post['post_title'] . "</td >";
echo "<td>". $post['post_body'];
if (!empty($post['ID'])) {
echo "<img src='getImage.php?id=".$post['ID']."'>";
}
echo "</td><td>".
$post['DATETIME']. "</td><td>". $post['replyIndex']. "</td>
<td>".$post['post_type']."</td>";
说明:由于您要使用PostImages表加入forumPosts,因此需要检查是否存在只能来自PostImages列的列数据。所以我添加了if
语句来检查ID
是否存在。假设这来自PostImages表。
附录:将if
语句放在表格中间没有任何问题。这就是动态生成HTML结构的方式。唯一的考虑因素是您不会生成无效的HTML,例如省略结束标记(如</td>
)。但是,如果图像不存在,则if
语句只会省略<img>
标记,并且不会使表格的HTML无效。
答案 1 :(得分:0)
要扩展FirstOne的答案,您可以使用基本的逻辑检查:
if (!empty($row['image']) {
echo $row['image'];
} else {
echo 'empty';
}
在其他内容中,您可以找出在空$row['image']
变量的情况下要执行或显示的内容。