为什么这是为foreach()提供的无效参数?

时间:2017-11-05 06:13:39

标签: php html foreach

我不明白为什么,错误说:

  

警告:在第134行的C:\ xampp \ htdocs \ twitter \ core \ ajax \ popuptweets.php中为foreach()提供的参数无效,我没有看到任何错误或者我是盲目的!

popuptweets.php 代码

<?php 
include '../init.php';
if (isset($_POST['showpopup']) && !empty($_POST['showpopup'])) {
    $tweetID  = $_POST['showpopup'];
    $user_id  = $_SESSION['user_id'];
    $tweet    = $getFromT->getPopupTweet($tweetID);
    $user     = $getFromU->userData($user_id);
    $likes    = $getFromT->likes($user_id, $tweetID);
    $retweet  = $getFromT->checkRetweet($tweetID, $user_id);
    $comments = $getFromT->comments($tweetID);
    ?>
    <div class="tweet-show-popup-wrap">
        <input type="checkbox" id="tweet-show-popup-wrap">
        <div class="wrap4">
            <label for="tweet-show-popup-wrap">
                <div class="tweet-show-popup-box-cut">
                    <i class="fa fa-times" aria-hidden="true"></i>
                </div>
            </label>
            <div class="tweet-show-popup-box">
            <div class="tweet-show-popup-inner">
                <div class="tweet-show-popup-head">
                    <div class="tweet-show-popup-head-left">
                        <div class="tweet-show-popup-img">
                            <img src="<?php echo BASE_URL.$tweet->profileImage;?>"/>
                        </div>
                        <div class="tweet-show-popup-name">
                            <div class="t-s-p-n">
                                <a href="<?php echo BASE_URL.$tweet->username;?>">
                                    <?php echo $tweet->screenName;?>
                                </a>
                            </div>
                            <div class="t-s-p-n-b">
                                <a href="<?php echo BASE_URL.$tweet->username;?>">
                                    @<?php echo $tweet->username;?>
                                </a>
                            </div>
                        </div>
                    </div>
                    <div class="tweet-show-popup-head-right">
                          <button class="f-btn"><i class="fa fa-user-plus"></i> Follow </button>
                    </div>
                </div>
                <div class="tweet-show-popup-tweet-wrap">
                    <div class="tweet-show-popup-tweet">
                        <?php echo $getFromT->getTweetLinks($tweet->status);?>
                    </div>
                    <div class="tweet-show-popup-tweet-ifram">
                        <?php if(!empty($tweet->tweetImage)){?>
                        <img src="<?php echo BASE_URL.$tweet->tweetImage;?>"/> 
                        <?php }?>
                    </div>
                </div>
                <div class="tweet-show-popup-footer-wrap">
                    <div class="tweet-show-popup-retweet-like">
                        <div class="tweet-show-popup-retweet-left">
                            <div class="tweet-retweet-count-wrap">
                                <div class="tweet-retweet-count-head">
                                    RETWEET
                                </div>
                                <div class="tweet-retweet-count-body">
                                    <?php echo $tweet->retweetCount;?>
                                </div>
                            </div>
                            <div class="tweet-like-count-wrap">
                                <div class="tweet-like-count-head">
                                    LIKES
                                </div>
                                <div class="tweet-like-count-body">
                                    <?php echo $tweet->likesCount;?>
                                </div>
                            </div>
                        </div>
                        <div class="tweet-show-popup-retweet-right">

                        </div>
                    </div>
                    <div class="tweet-show-popup-time">
                        <span><?php echo $tweet->postedOn;?></span>
                    </div>
                    <div class="tweet-show-popup-footer-menu">
                        <ul>
                         <?php if($getFromU->loggedIn() === true){
                                echo '<li><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.'"><a href="#"><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.'"><a href="#"><i class="fa fa-retweet" aria-hidden="true"></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-o" aria-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" 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>';
                                }else{
                         ?> 
                            <li><button type="buttton"><i class="fa fa-share" aria-hidden="true"></i></button></li>
                            <li><button type="button"><i class="fa fa-retweet" aria-hidden="true"></i><span class="retweetsCount">RETWEET-COUNT</span></button></li>
                            <li><button type="button"><i class="fa fa-heart" aria-hidden="true"></i><span class="likesCount">LIKES-COUNT</span></button></button></li>
                            <?php }?>
                        </ul>
                    </div>
                </div>
            </div><!--tweet-show-popup-inner end-->
            <?php if($getFromU->loggedIn() === true){?>
            <div class="tweet-show-popup-footer-input-wrap">
                <div class="tweet-show-popup-footer-input-inner">
                    <div class="tweet-show-popup-footer-input-left">
                        <img src="<?php echo BASE_URL.$user->profileImage?>"/>
                    </div>
                    <div class="tweet-show-popup-footer-input-right">
                        <input id="commentField" type="text" name="comment"  placeholder="Reply to @<?php echo $tweet->username;?>">
                    </div>
                </div>
                <div class="tweet-footer">
                    <div class="t-fo-left">
                        <ul>
                            <li>
                                <label for="t-show-file"><i class="fa fa-camera" aria-hidden="true"></i></label>
                                <input type="file" id="t-show-file"> 
                            </li>
                            <li class="error-li">
                            </li> 
                        </ul>
                    </div>
                    <div class="t-fo-right">
                    <span id="count">140</span>
                        <input type="submit" id="postComment">
                    </div>
                 </div>
            </div><!--tweet-show-popup-footer-input-wrap end-->
        <?php }?>
        <div class="tweet-show-popup-comment-wrap">
            <div id="comments">
                <?php
                foreach ($comments as $comment) {
                    echo '<div class="tweet-show-popup-comment-box">
                            <div class="tweet-show-popup-comment-inner">
                                <div class="tweet-show-popup-comment-head">
                                    <div class="tweet-show-popup-comment-head-left">
                                         <div class="tweet-show-popup-comment-img">
                                            <img src="'.BASE_URL.$comment->profileImage.'">
                                         </div>
                                    </div>
                                    <div class="tweet-show-popup-comment-head-right">
                                          <div class="tweet-show-popup-comment-name-box">
                                            <div class="tweet-show-popup-comment-name-box-name"> 
                                                <a href="'.BASE_URL.$comment->username.'">'.$comment->screenName.'</a>
                                            </div>
                                            <div class="tweet-show-popup-comment-name-box-tname">
                                                <a href="'.BASE_URL.$comment->username.'">@'.$comment->username.' - '.$comment->commentAt.'</a>
                                            </div>
                                         </div>
                                         <div class="tweet-show-popup-comment-right-tweet">
                                                <p><a href="'.BASE_URL.$tweet->username.'">@'.$tweet->username.'</a> '.$comment->comment.'</p>
                                         </div>
                                        <div class="tweet-show-popup-footer-menu">
                                            <ul>
                                                <li><button><i class="fa fa-share" aria-hidden="true"></i></button></li>
                                                <li><a href="#"><i class="fa fa-heart-o" aria-hidden="true"></i></a></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>
                            <!--TWEET SHOW POPUP COMMENT inner END-->
                            </div>
                            ';
                }
                ?> 
            </div>

        </div>
        <!--tweet-show-popup-box ends-->
        </div>
        </div>

    <?php
   }
?>

以及foreach上的错误对此代码的意义

foreach ($comments as $comment) {
    echo '<div class="tweet-show-popup-comment-box">
        <div class="tweet-show-popup-comment-inner">
            <div class="tweet-show-popup-comment-head">
                <div class="tweet-show-popup-comment-head-left">
                    <div class="tweet-show-popup-comment-img">
                        <img src="'.BASE_URL.$comment->profileImage.'">
                    </div>
                </div>
                <div class="tweet-show-popup-comment-head-right">
                    <div class="tweet-show-popup-comment-name-box">
                        <div class="tweet-show-popup-comment-name-box-name"> 
                            <a href="'.BASE_URL.$comment->username.'">'.$comment->screenName.'</a>
                        </div>
                        <div class="tweet-show-popup-comment-name-box-tname">
                            <a href="'.BASE_URL.$comment->username.'">@'.$comment->username.' - '.$comment->commentAt.'</a>
                        </div>
                    </div>
                    <div class="tweet-show-popup-comment-right-tweet">
                        <p><a href="'.BASE_URL.$tweet->username.'">@'.$tweet->username.'</a> '.$comment->comment.'</p>
                    </div>
                    <div class="tweet-show-popup-footer-menu">
                        <ul>
                            <li><button><i class="fa fa-share" aria-hidden="true"></i></button></li>
                            <li><a href="#"><i class="fa fa-heart-o" aria-hidden="true"></i></a></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>
        <!--TWEET SHOW POPUP COMMENT inner END-->
    </div>
    ';
}

这里的错误在哪里?

2 个答案:

答案 0 :(得分:2)

foreach的参数必须是数组,你需要var_dump($ comments)来查看它是否是数组。在您的情况下,返回结果可能为null,因此它返回foreach的警告。为确保foreach正常工作,您可以使用:

foreach ((array)$comments as $comment) {
}

答案 1 :(得分:0)

我将你的代码与我的代码进行了比较,发现这一行有所不同:别担心,错误就在那里。

angular:
    build: ../angular
    container_name: ng
    ports:
      - "4200:4200"
    volumes:
      - ../angular:/usr/src/app
      - /usr/src/app/node_modules