How to ckeck and display an image if there is one

I would like to ask for some help because I am stuck with this problem. I have a MySQL database, and I'm trying to create some kind of blog system. The problem that arises for me is this: I would like to check if there is an image tag if it does not display it. The system works perfectly without images, but it would be nice if I became new, where I ruined it. Here is my code:

function outputStory($article, $only_snippet = FALSE){
global $conn;


if($article){
    $sql = "SELECT ar.*, usr.name FROM cms_articles ar LEFT OUTER JOIN cms_users usr ON ar.author_id = usr.user_id WHERE ar.article_id = " . $article;
    $result = mysql_query($sql, $conn);

if($row = mysql_fetch_array($result)){
    echo "<h2>" . htmlspecialchars($row['title']) . "</h2>\n";
    echo"<h5><div class='byLine'>:" . htmlspecialchars($row['name']) . "</div>";
    echo "<div class='pubdate'>";
    if($row['is_published'] == 1){
        echo date("F j, Y",strtotime($row['date_published']));  
    } else {
        echo "No articles are published yet!";
    }
    echo "</div></h5>\n";
    if ($only_snippet){
        echo "<p>\n";
        echo nl2br(trimBody($row['body']));

       // I think I messed this statement alot but don't know how to make it work properly :S

        if(hasPostImage() == true){
            $getPostImage = "SELECT * FROM cms_images";
            $getImgResult = mysql_query($getPostImage,$conn);
            $rowImg = mysql_fetch_array($getImgResult);
            echo"<img src='".$rowImg['img_src']."' alt='".$rowImg['img_desc']."'>";

        } else {
            echo '<img style="display:none">';  
        }
        echo "</p>\n";
        echo "<h4><a href=\"viewarticle.php?article=" . $row['article_id'] . "\">More...</a></h4><br>\n";
    } else {
        echo "<p>\n";
        echo nl2br($row['body']);
        echo "</p>\n";  
    }
  }
 }
}

I hope the question is not stupid or something else, and thanks for the help in advance.

+4
source share
1 answer

, - , $_FILES, , , PHP , . ()

getimagesize(), ,

0

Source: https://habr.com/ru/post/1538002/


All Articles