Skip to content
Advertisement

tweetImage not showing an image

Here is my line 51,

<img src="'.BASE_URL.$tweet->tweetImage.'"/>

Here is my code.

<?php
class Tweet extends User
{
    
    function _construct($pdo)
    {
        $this->pdo = $pdo;
    }
    public function tweets($user_id)
    {
        $stmt = $this->pdo->prepare("SELECT * FROM `tweets` LEFT JOIN `users` ON `tweetBy` = `user_id` WHERE `tweetBy` = :user_id AND `retweetID` = '0' OR `tweetBy` = `user_id` AND `retweetBy` != :user_id");
        $stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
        $stmt->execute();
        $tweets = $stmt->fetchAll(PDO::FETCH_OBJ);
        
        foreach($tweets as $tweet)
        {
            $likes = $this->likes($user_id, $tweet->tweetID); 
            $retweet = $this->checkRetweet($tweet->tweetID, $user_id);
            $user = $this->userData($tweet->retweetBy);
            echo '<div class="all-tweet">
<div class="t-show-wrap">   
 <div class="t-show-inner">
 '.((!empty($tweet->retweetMsg) && $tweet->tweetID === $retweet['tweetID'] or $tweet->retweetID > 0) ? '  
    <div class="t-show-banner">
        <div class="t-show-banner-inner">
            <span><i class="fa fa-retweet" aria-hidden="true"></i></span><span>'.$user->screenName.' Retweeted</span>
        </div>
    </div>' 
    : '').'
    '.((!empty($tweet->retweetMsg) && $tweet->tweetID === $retweet['tweetID'] OR $tweet->retweetID > 0) ? '<div class="t-show-head">
                      <div class="t-show-img">
                            <img src="'.BASE_URL.$user->profileImage.'"/>
                        </div>
                <div class="t-s-head-content">
                    <div class="t-h-c-name">
                        <span><a href="'.BASE_URL.$user->username.'">'.$user->screenName.'</a></span>
                        <span>@'.$user->username.'</span>
                        <span>'.$retweet['postedOn'].'</span>
                    </div>
                    <div class="t-h-c-dis">
                        '.$this->getTweetLinks($tweet->retweetMsg).'
                    </div>
                </div>
            </div>
            <div class="t-s-b-inner">
                <div class="t-s-b-inner-in">
                    <div class="retweet-t-s-b-inner">
                    '.((!empty($tweet->tweetImage)) ? '
                        <div class="retweet-t-s-b-inner-left">
                            <img src="'.BASE_URL.$tweet->tweetImage.'"/>    
                        </div>' : '').'
                        <div class="retweet-t-s-b-inner-right">
                            <div class="t-h-c-name">
                                <span><a href="'.BASE_URL.$tweet->username.'">'.$tweet->screenName.'</a></span>
                                <span>@'.$tweet->username.'</span>
                                <span>'.$tweet->postedOn.'</span>
                            </div>
                            <div class="retweet-t-s-b-inner-right-text">        
                                '.$tweet->status.'
                            </div>
                        </div>
                    </div>
                </div>
            </div>' : '
    <div class="t-show-popup">
        <div class="t-show-head">
            <div class="t-show-img">
                <img src="'.$tweet->profileImage.'"/>
            </div>
            <div class="t-s-head-content">
                <div class="t-h-c-name">
                    <span><a href="'.$tweet->username.'">'.$tweet->screenName.'</a></span>
                    <span>@'.$tweet->username.'</span>
                    <span>'.$tweet->postedOn.'</span>
                </div>
                <div class="t-h-c-dis">
                    '.$this->getTweetLinks($tweet->status).'
                </div>
            </div>
        </div>'.
        ((!empty($tweet->tweetImage)) ? 
         '<!--tweet show head end-->
        <div class="t-show-body">
          <div class="t-s-b-inner">
           <div class="t-s-b-inner-in">
             <img src="'.$tweet->tweetImage.'" class="imagePopup"/>
           </div>
          </div>
        </div>
        <!--tweet show body end-->
        ' : '').'
        
     </div>').'
    <div class="t-show-footer">
        <div class="t-s-f-right">
            <ul> 
                <li><button><i class="fa fa-share" aria-hidden="true"></i></button></li>    
                <li>'.((isset($retweet['retweetID']) ? $retweet['retweetID'] === $tweet->tweetID : '') ? 
                '<button class="retweeted" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-retweet" aria-hidden="true"></i><span class = "retweetsCount">'.$tweet->retweetCount.'</span></button>' : 
                '<button class="retweet" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-retweet" aria-hidden="true"></i><span class = "retweetsCount">'.(($tweet->retweetCount > 0) ? $tweet->retweetCount : '').'</span></button>').'
                </li>
                <li>'.((isset($likes['likeOn']) ? $likes['likeOn'] === $tweet->tweetID : '') ? 
                                    '<button class="unlike-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-heart" aria-hidden="true"></i><span class="likesCounter">'.(($tweet->likesCount > 0) ? $tweet->likesCount : '' ).'</span></button>' : 
                                    '<button class="like-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"><i class="fa fa-heart-o" aria-hidden="true"></i><span class="likesCounter">'.(($tweet->likesCount > 0) ? $tweet->likesCount : '' ).'</span></button>').'
                                </li>                   
                    <li>
                    <a href="#" class="more"><i class="fa fa-ellipsis-h" aria-hidden="true"></i></a>
                    <ul> 
                      <li><label class="deleteTweet">Delete Tweet</label></li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</div>
</div>
</div>';
        }
    } 
    public function getTrendByHash($hashtag){
        $stmt = $this->pdo->prepare("SELECT * FROM `trends` WHERE `hashtag` LIKE :hashtag LIMIT 5");
        $stmt->bindValue(":hashtag", $hashtag.'%');
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_OBJ);
    }
    
    public function getMention($mention)
    {
        $stmt = $this->pdo->prepare("SELECT `user_id`,`username`,`screenName`,`profileImage` FROM `users` WHERE `username` LIKE :mention OR `screenName` LIKE :mention LIMIT 5");
        $stmt->bindValue(':mention', $mention.'%');
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_OBJ);
    }
    
    public function addTrend($hashtag)
    {
        preg_match_all("/#+([a-zA-Z0-9_]+)/i", $hashtag, $matches);
        if($matches)
        {
            $result = array_values($matches[1]);
        }
        $sql = "INSERT INTO `trends` (`hashtag`, `createdOn`) VALUES (:hashtag, CURRENT_TIMESTAMP)";
        foreach ($result as $trend) {
            if($stmt = $this->pdo->prepare($sql)){
                $stmt->execute(array(':hashtag' => $trend));
            }
        }
    }
    public function getTweetLinks($tweet)
    {
        $tweet = preg_replace("/(https?://)([w]+.)([w.]+)/", "<a href='$0' target='_blank'>$0</a>", $tweet);
        $tweet = preg_replace("/#([w]+)/", "<a href='http://localhost/twitter/hashtag/$1'>$0</a>", $tweet);
        $tweet = preg_replace("/@([w]+)/", "<a href='http://localhost/twitter/$1'>$0</a>", $tweet);
        return $tweet;
    }
    
    public function getPopupTweet($tweet_id){
        $stmt = $this->pdo->prepare("SELECT * FROM `tweets`,`users` WHERE `tweetID` = :tweet_id AND `tweetBy` = `user_id`");
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_OBJ);
    }
    
    public function retweet($tweet_id, $user_id, $get_id, $comment)
    {
        $stmt = $this->pdo->prepare("UPDATE `tweets` SET `retweetCount` = `retweetCount`+1 WHERE `tweetID` = :tweet_id AND `tweetBy` = :get_id");
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->bindParam(":get_id", $get_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $stmt = $this->pdo->prepare("INSERT INTO `tweets` (`status`,`tweetBy`,`retweetID`,`retweetBy`,`tweetImage`,`postedOn`,`likesCount`,`retweetCount`,`retweetMsg`) SELECT `status`,`tweetBy`,`tweetID`,:user_id,CURRENT_TIMESTAMP`tweetImage`,`postedOn`,`likesCount`,`retweetCount`,:retweetMsg FROM `tweets` WHERE `tweetID` = :tweet_id");
        $stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
        $stmt->bindParam(":retweetMsg", $comment, PDO::PARAM_STR);
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->execute();
    }
    
    public function checkRetweet($tweet_id, $user_id){
        $stmt = $this->pdo->prepare("SELECT * FROM `tweets` WHERE `retweetID` = :tweet_id AND `retweetBy` = :user_id OR `tweetID` = :tweet_id and `retweetBy` = :user_id");
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    }
    
    public function addLike($user_id, $tweet_id, $get_id)
    {
        $stmt = $this->pdo->prepare("UPDATE `tweets` SET `likesCount` = `likesCount` +1 WHERE `tweetID` = :tweet_id");
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $this->create('likes', array('likeBy' => $user_id, 'likeOn' => $tweet_id));
    }
    
        public function unlike($user_id, $tweet_id, $get_id)
    {
        $stmt = $this->pdo->prepare("UPDATE `tweets` SET `likesCount` = `likesCount` -1 WHERE `tweetID` = :tweet_id");
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $this->create('likes', array('likeBy' => $user_id, 'likeOn' => $tweet_id));
    }
    
    public function likes($user_id, $tweet_id){
        $stmt = $this->pdo->prepare("SELECT * FROM `likes` WHERE `likeBy` = :user_id AND `likeOn` = :tweet_id");
        $stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->execute();
        return $stmt->fetch(PDO::FETCH_ASSOC);
    } 
}
?><br><br>

When I execute it I GET THIS
When i replace ‘tweetImage’ with ‘profileImage’in line 51.My profileImage successfully gets uploaded in place of blank thumbnail.I want the tweetImage uploaded by the user to be uploaded in place of the blank thumbnail.I can’t seem to figure out much as i am one week new to PHP.

Here is my “home.php”,It is my main page and profileImage and tweetImage are defined here.

<?php
    include 'core/init.php';
    $user_id = $_SESSION['user_id'];
    $user = $getFromU->userData($user_id);
    if($getFromU->loggedIn() === false)
    {
        header('Location: index.php');
    }
    
    if(isset($_POST['tweet']))
    {
        $status = $getFromU->checkinput($_POST['status']);
        $tweetImage = '';
        
        if(!empty($status) or !empty($_FILES['file']['name'][0]))
        {
            if(!empty($_FILES['file']['name'][0]))
            {
                $tweetImage = $getFromU->uploadImage($_FILES['file']);
            }
            
            if(strlen($status) > 140)
            {
                $error = "The text of your tweet is too long!";
            }
            $getFromU->create('tweets' ,array('status' => $status, 'tweetBy' => $user_id, 'tweetImage' => $tweetImage, 'postedOn' => date('Y-m-d H:i:s')));
            preg_match_all("/#+([a-zA-Z0-9_]+)/i", $status, $hashtag);
            
            if(!empty($hashtag))
            {
                $getFromT->addTrend($status);
            }
        }
        else
        {
            $error = "Try adding an image/text then click tweet!!";
        }
    }
?>

<!DOCTYPE HTML> 
 <html>
    <head>
        <title>Tweety</title>
          <meta charset="UTF-8" />
          <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css"/>  
          <link rel="stylesheet" href="assets/css/style-complete.css"/> 
          <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>     
    </head>
    <!--Helvetica Neue-->
<body>
<div class="wrapper">
<!-- header wrapper -->
<div class="header-wrapper">

<div class="nav-container">
    <!-- Nav -->
    <div class="nav">
        
        <div class="nav-left">
            <ul>
                <li><a href="#"><i class="fa fa-home" aria-hidden="true"></i>Home</a></li>
                <li><a href="i/notifications"><i class="fa fa-bell" aria-hidden="true"></i>Notification</a></li>
                <li><i class="fa fa-envelope" aria-hidden="true"></i>Messages</li>
            </ul>
        </div><!-- nav left ends-->

        <div class="nav-right">
            <ul>
                <li>
                    <input type="text" placeholder="Search" class="search"/>
                    <i class="fa fa-search" aria-hidden="true"></i>
                    <div class="search-result">         
                    </div>
                </li>

                <li class="hover"><label class="drop-label" for="drop-wrap1"><img src="<?php echo $user->profileImage;?>"/></label>
                <input type="checkbox" id="drop-wrap1">
                <div class="drop-wrap">
                    <div class="drop-inner">
                        <ul>
                            <li><?php echo $user->username;?></a></li>
                            <li><a href="settings/account">Settings</a></li>
                            <li><a href="includes/logout.php">Log out</a></li>
                        </ul>
                    </div>
                </div>
                </li>
                <li><label class="addTweetBtn">Tweet</label></li>
            </ul>
        </div><!-- nav right ends-->

    </div><!-- nav ends -->

</div><!-- nav container ends -->

</div><!-- header wrapper end -->

<script type="text/javascript" src = "assets/js/search.js"></script>
<script type="text/javascript" src = "assets/js/hashtag.js"></script>

<!---Inner wrapper-->
<div class="inner-wrapper">
<div class="in-wrapper">
    <div class="in-full-wrap">
        <div class="in-left">
            <div class="in-left-wrap">
        <div class="info-box">
            <div class="info-inner">
                <div class="info-in-head">
                    <!-- PROFILE-COVER-IMAGE -->
                    <img src="<?php echo $user->profileCover;?>"/>
                </div><!-- info in head end -->
                <div class="info-in-body">
                    <div class="in-b-box">
                        <div class="in-b-img">
                        <!-- PROFILE-IMAGE -->
                            <img src="<?php echo $user->profileImage;?>"/>
                        </div>
                    </div><!--  in b box end-->
                    <div class="info-body-name">
                        <div class="in-b-name">
                            <div><a href="<?php echo $user->username;?>"><?php echo $user->screenName;?></a></div>
                            <span><small><a href="<?php echo $user->username;?>"><?php echo $user->username;?></a></small></span>
                        </div><!-- in b name end-->
                    </div><!-- info body name end-->
                </div><!-- info in body end-->
                <div class="info-in-footer">
                    <div class="number-wrapper">
                        <div class="num-box">
                            <div class="num-head">
                                TWEETS
                            </div>
                            <div class="num-body">
                                10
                            </div>
                        </div>
                        <div class="num-box">
                            <div class="num-head">
                                FOLLOWING
                            </div>
                            <div class="num-body">
                                <span class="count-following"><?php echo $user->following;?></span>
                            </div>
                        </div>
                        <div class="num-box">
                            <div class="num-head">
                                FOLLOWERS
                            </div>
                            <div class="num-body">
                                <span class="count-followers"><?php echo $user->followers;?></span>
                            </div>
                        </div>  
                    </div><!-- mumber wrapper-->
                </div><!-- info in footer -->
            </div><!-- info inner end -->
        </div><!-- info box end-->

    <!--==TRENDS==-->
      <!---TRENDS HERE-->
    <!--==TRENDS==-->

    </div><!-- in left wrap-->
        </div><!-- in left end-->
        <div class="in-center">
            <div class="in-center-wrap">
                <!--TWEET WRAPPER-->
                <div class="tweet-wrap">
                    <div class="tweet-inner">
                         <div class="tweet-h-left">
                            <div class="tweet-h-img">
                            <!-- PROFILE-IMAGE -->
                                <img src="<?php echo $user->profileImage;?>"/>
                            </div>
                         </div>
                         <div class="tweet-body">
                         <form method="post" enctype="multipart/form-data">
                            <textarea class="status" name="status" placeholder="Type Something here!" rows="4" cols="50"></textarea>
                            <div class="hash-box">
                                <ul>
                                </ul>
                            </div>
                         </div>
                         <div class="tweet-footer">
                            <div class="t-fo-left">
                                <ul>
                                    <input type="file" name="file" id="file"/>
                                    <li><label for="file"><i class="fa fa-camera" aria-hidden="true"></i></label>
                                    <span class="tweet-error"><?php if(isset($error)){echo $error;}else if(isset($imageError)){echo $imageError;}?></span>
                                    </li>
                                </ul>
                            </div>
                            <div class="t-fo-right">
                                <span id="count">140</span>
                                <input type="submit" name="tweet" value="tweet"/>
                        </form>
                            </div>
                         </div>
                    </div>
                </div><!--TWEET WRAP END-->

            
                <!--Tweet SHOW WRAPPER-->
                 <div class="tweets">
                    <?php $getFromT->tweets($user_id);?>
                 </div>
                <!--TWEETS SHOW WRAPPER-->

                <div class="loading-div">
                    <img id="loader" src="assets/images/loading.svg" style="display: none;"/> 
                </div>
                <div class="popupTweet"></div>
                <!--Tweet END WRAPER-->
            <script type="text/javascript" src = "assets/js/like.js"></script>
            <script type="text/javascript" src = "assets/js/retweet.js"></script>
            </div><!-- in left wrap-->
        </div><!-- in center end -->

        <div class="in-right">
            <div class="in-right-wrap">

            <!--Who To Follow-->
              <!--WHO_TO_FOLLOW HERE-->
            <!--Who To Follow-->

            </div><!-- in left wrap-->

        </div><!-- in right end -->

    </div><!--in full wrap end-->

</div><!-- in wrappper ends-->
</div><!-- inner wrapper ends-->
</div><!-- ends wrapper -->
</body>

</html>

THIS IS WHAT I TRIED:

  1. Removed base url before tweet image ; like this '.$tweet->tweetImage.' OR <img src="'.$tweet->tweetImage.'" class="imagePopup"/>
  2. Tried “Inspect element on the blank image thumbnail” to find the error at line 51.
  3. I tried searching for similar question on stackoverflow but wasn’t able to find one.

    EDIT:
    YOU CAN LOOK AT THIS TO FIND SOMETHING HELPFUL

Any suggestion , remark, answer would be appreciated!

Advertisement

Answer

In your db table, the tweets.tweetImage column is a datetime. All of the rows are empty except for the last one which is a time stamp.

Change it to the file path or uri and it should work after that.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement