-1

I've looked into everything. I don't why I'm getting a Parse error: syntax error, unexpected 'if' (T_IF) at this part

if(is_array($comment_contents) || is_object($comment_contents)){

This is my whole code

<?php
            if(is_array($home_contents) || is_object($home_contents)){
                foreach($home_contents as $object_home){
                    echo'<div class="col-sm-offset-2">
                        <blockquote>
                            <p>' . $object_home->post_content .'</p>
                            <small>' . $object_home->poster_name . ' posted in <cite title="Source Title">'. $object_home->group_name .'<br/><br/></cite></small>
                            <form method="POST" action="<'. base_url('Home/getComment') .'>">
                                <input type="text" class="form-control hidden" name="postid" id="search" value="'. $object_home->post_id .'">
                                <button type="button" class="btn btn-default" data-toggle="collapse" aria-pressed="false" autocomplete="off" data-target="#demo"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Comment</button>
                            </form>
                            <div id="demo" class="collapse out">    
                                <div class="form-group">
                                    <label for="comment"></label>'.
                                    if(is_array($comment_contents) || is_object($comment_contents)){
                                        foreach ($comment_contents as $object_comment){
                                            .'<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>'.
                                        }
                                    }.'
                                    <textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>
                                    <p></p>
                                    <div class="pull-right">
                                        <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
                                    </div>
                                </div>
                            </div>
                        </blockquote>
                    </div>';
                }
            }
        ?>

any help would be much appreciated.

3 Answers3

0

you cant use if condition in string so try this code

  if(is_array($home_contents) || is_object($home_contents)){
             $comments="";
            if(is_array($comment_contents) || is_object($comment_contents)){
                                    foreach ($comment_contents as $object_comment){
                                     $comments .=   '<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>';
                                    }
           }
            foreach($home_contents as $object_home){
                echo'<div class="col-sm-offset-2">
                    <blockquote>
                        <p>' . $object_home->post_content .'</p>
                        <small>' . $object_home->poster_name . ' posted in <cite title="Source Title">'. $object_home->group_name .'<br/><br/></cite></small>
                        <form method="POST" action="<'. base_url('Home/getComment') .'>">
                            <input type="text" class="form-control hidden" name="postid" id="search" value="'. $object_home->post_id .'">
                            <button type="button" class="btn btn-default" data-toggle="collapse" aria-pressed="false" autocomplete="off" data-target="#demo"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Comment</button>
                        </form>
                        <div id="demo" class="collapse out">    
                            <div class="form-group">
                                <label for="comment"></label>'.$comments
                                .'
                                <textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>
                                <p></p>
                                <div class="pull-right">
                                    <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
                                </div>
                            </div>
                        </div>
                    </blockquote>
                </div>';
            }
        }
    ?>
Aman Jain
  • 384
  • 1
  • 3
  • 15
0

you can not concatenation with dot to if condition

 '<label for="comment"></label>'.
       if(is_array($comment_contents) || is_object($comment_contents)){

you can do something like thik

 <label for="comment"></label>';
                                if(is_array($comment_contents) || is_object($comment_contents)){
                                    foreach ($comment_contents as $object_comment){
                                        echo'<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>';
                                    }
                                }.'
                             echo'<textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>
kevin
  • 300
  • 2
  • 11
0

You can use conditional statements with strings.

your code modified

codestart <?php
        if(is_array($home_contents) || is_object($home_contents)){
            foreach($home_contents as $object_home){
                echo'<div class="col-sm-offset-2">
                    <blockquote>
                        <p>' . $object_home->post_content .'</p>
                        <small>' . $object_home->poster_name . ' posted in <cite title="Source Title">'. $object_home->group_name .'<br/><br/></cite></small>
                        <form method="POST" action="<'. base_url('Home/getComment') .'>">
                            <input type="text" class="form-control hidden" name="postid" id="search" value="'. $object_home->post_id .'">
                            <button type="button" class="btn btn-default" data-toggle="collapse" aria-pressed="false" autocomplete="off" data-target="#demo"><span class="glyphicon glyphicon-comment"></span>&nbsp;&nbsp;Comment</button>
                        </form>
                        <div id="demo" class="collapse out">    
                            <div class="form-group">
                                <label for="comment"></label>';
                                if(is_array($comment_contents) || is_object($comment_contents)){
                                    foreach ($comment_contents as $object_comment){
                                        echo '<h6>'. $object_comment->comment_content .' by '. $object_comment->full_name .' at '. $object_comment->post_date .'</h6>';
                                    }
                                }
                                echo '
                                <textarea class="form-control" rows="5" id="comment" placeholder="Enter Comment..."></textarea>
                                <p></p>
                                <div class="pull-right">
                                    <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>
                                </div>
                            </div>
                        </div>
                    </blockquote>
                </div>';
            }
        }
    ?>code end
Lucky Chingi
  • 2,248
  • 1
  • 10
  • 15