Reputation: 137
In PHP I am able to upload images to my database and display in a table. However a broken image icon is displayed in postings when there is no image. I was wondering how I could check for an image prior to displaying so I don't get the broken image icon. I have recently determined that the broken image icon is being caused by the image tag in my index.php file. I figured this out when commenting out code in my getImage.php file. I don't believe I can have an if statement in the middle of me echoing out a table. Help would be appreciated.
Code from my index.php file:
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>";
And the getImage.php file:
<?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'];
Any idea how I can check for an image? Code below is how images are stored.
<?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();
}
?>
Upvotes: 1
Views: 682
Reputation: 6778
Here is a solution:
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>";
Explanation: Since you are joining forumPosts with the PostImages table, you need to check if column data exists that could only come from the PostImages column. So I added a if
statement that checks if ID
exists. Assuming this comes from the PostImages table.
Addendum: There is nothing wrong with putting an if
statement in the middle of a table. That is how you dynamically generate the HTML structure. The only consideration should be that you don't generate invalid HTML, such as leaving out a closing tag (like </td>
). But this if
statement will only leave out the <img>
tag if an image doesn't exist, and that won't make the table's HTML invalid.
Upvotes: 2
Reputation: 1159
To extend on FirstOne's answer, you can use a basic logic check:
if (!empty($row['image']) {
echo $row['image'];
} else {
echo 'empty';
}
Inside the else, you could figure out what to do or display during the case of an empty $row['image']
variable.
Upvotes: 0