0

I just upgraded my server's PHP version to PHP 7.4.5 and now getting this error : Notice: Trying to access array offset on value of type bool in C:\xampp\htdocs\tagopuss\core\classes\tweet.php on line 69

my line 69

<li>'.(($likes['likeOn'] === $tweet->tweetID) ? '<button class="unlike-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"> <i class="fa fa-heart" area-hidden="true"></i><span class="likesCounter">'.$tweet->likesCount.'</span></button>' : '<button class="like-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"> <i class="fa fa-heart-o" area-hidden="true"></i><span class="likesCounter">'.(($tweet->likesCount > 0) ? $tweet->likesCount : '').'</span></button>' ).'</li>

this is the full code

<?php
   class Tweet extends User {

       function __construct($pdo){
        $this->pdo = $pdo;
       }

       public function tweets($user_id){
        $stmt = $this->pdo->prepare("SELECT * FROM `tweets`,`users` WHERE `tweetBy` = `user_id`");
        $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="post"> 
           <div class="t-show-inner">
           '.(($retweet['retweetID'] === $tweet->retweetID 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>' 
            : '').'

           
          
            <div class="t-show-popup">
              <div class="post-heading">
                <div class="t-show-img">
                  <img src="'.$tweet->profileImage.'"/>
                </div>
                <div class="t-s-head-content">
                  <div class="post-title">
                    <span><a href="'.$tweet->username.'">'.$tweet->screenName.'</a></span>
                    <span>@'.$tweet->username.'</span>
                    <span>'.$tweet->postedOn.'</span>
                  </div>
                  <div class="post-description">
                    '.$this->getTweetLinks($tweet->status).'
                  </div>
                </div>
              </div>'.

               ((!empty($tweet->tweetImage)) ? 
                ' <div class="t-show-body">
                  <div class="t-s-b-inner">
                   <div class="post-description">
                   <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><a href="#"><i class="fa fa-share" aria-hidden="true"></i></a></button></li>  

                  <li>'.(($tweet->tweetID === $retweet['retweetID']) ? '<button class="retweeted" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"> <i class="fa fa-retweet"></i><span class="retweetsCount">'.$tweet->retweetCount.'</span>
                                </button>' : '<button class="retweet" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"> <i class="fa fa-retweet"></i><span class="retweetsCount">'.(($tweet->retweetCount > 0) ? $tweet->retweetCount : '').'</span>
                                </button>').'</li>

                  <li>'.(($likes['likeOn'] === $tweet->tweetID) ? '<button class="unlike-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"> <i class="fa fa-heart" area-hidden="true"></i><span class="likesCounter">'.$tweet->likesCount.'</span></button>' : '<button class="like-btn" data-tweet="'.$tweet->tweetID.'" data-user="'.$tweet->tweetBy.'"> <i class="fa fa-heart-o" area-hidden="true"></i><span class="likesCounter">'.(($tweet->likesCount > 0) ? $tweet->likesCount : '').'</span></button>' ).'</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='_blink'>$0</a>", $tweet);
        $tweet = preg_replace("/#([\w]+)/", "<a href='".BASE_URL."hashtag/$1'>$0</a>", $tweet);
        $tweet = preg_replace("/@([\w]+)/", "<a href='".BASE_URL."$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");
        $stmt->bindParam(":tweet_id", $tweet_id, PDO::PARAM_INT);
        $stmt->execute();

        $stmt = $this->pdo->prepare("INSERT INTO `tweets` (`status`,`tweetBy`,`tweetImage`,`retweetID`,`retweetBy`,`postedOn`,`likesCount`,`retweetCount`,`retweetMsg`) SELECT `status`,`tweetBy`,`tweetImage`,`tweetID`,:user_id,CURRENT_TIMESTAMP,`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();

        $stmt = $this->pdo->prepare("DELETE 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();
       }

       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);
       }


  }
 ?>
Dammzie
  • 11
  • 3
  • The only "array offset" access there is on `$likes['likeOn'] `. Is the `$likes` variable defined? How's your error reporting level, all on or not yet? – Markus AO Jun 18 '20 at 17:11
  • should i upload the full code so you can see it? @MarkusAO – Dammzie Jun 18 '20 at 23:40
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Mike Doe Jun 19 '20 at 07:42
  • Have a look at your code and see if `$likes` is always (or only conditionally) defined. If you post more code, post the section where `$likes` is first defined. – Markus AO Jun 19 '20 at 14:29

1 Answers1

0

It seems that variable $likes return false instead of data (if you are taken data from database it return false in your statement).

In PHP 7.4 you can't use array offset if is not set. (in this case you don't have key 'likeOn' in array $likes. In lower version of PHP it doesn't return issue.

To prevent this you should check $likes variable before use it i.e

if ($likes) {
... your code
}

You can add this comparison

(($likes['likeOn'] === $tweet->tweetID))

to some value above echo. i.e

$buttonTweet = false;
if ($likes) {
   $buttonTweet = (($likes['likeOn'] === $tweet->tweetID));
}

And then use $buttonTweet in echo.

dajmund
  • 82
  • 5
  • please i have uploaded the full code, can you please help me adjust it with the above information you have provided @dajmund – Dammzie Jun 20 '20 at 11:09