-3

I have a large php string variable. I want to put php if/else condition inside it, so the edit and delete buttons to be displayed only to the author of the comment and not to everyone, but my code is not working. I used a colon to define the if/else condition, but it doesn’t work. Regular if/else condition also doesn’t work.

Here is my php code:

$user_id = $_SESSION['user_id'];

$if = "if ($user_id == $row['user_id']): ";

$else = "else: ";

$endif = "endif; ";

foreach($result as $row)
{
 $output .= '
<div class="panel panel-default">
  <div class="panel-body">'.$row["comment"].'</div>

  <div class="panel-footer">

     '. $if .'

     <form  class="input-group form-row" action="comment_delete1.php" method="post" enctype = "multipart/form-data">
        <div class="input-group-prepend">
         <input type="hidden" name="comment_id" id="comment_id" value="'.$row["comment_id"].'" />
         <input type="hidden" name="user_id" id="user_id" value="'.$row["user_id"].'" />
         <input type="submit" name="submit" id="submit" class="submit btn btn-default" value="Delete" />
        </div>
      </form>   

      <button type="button" class="btn btn-default edit" id="'.$row["comment_id"].'" >Edit</button>  

    '. $else .'

      <button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>

    '. $endif .'

    </div>
</div>
 ';
}
echo $output;

Brian
  • 448
  • 2
  • 6
  • 18
  • `$if`? `$else`? those aren't conditions, those are variables. The variables will not be evaluated as a construct, they will be evaluated as a part of the string. – Jay Blanchard Feb 24 '20 at 17:24
  • Yes, I put the conditions inside variables. I also did put the conditions directly inside the string, but that didn't work either. – Brian Feb 24 '20 at 17:26
  • Because you're just making it all a string which will not be evaluated. – Jay Blanchard Feb 24 '20 at 17:30
  • So what should I do? – Brian Feb 24 '20 at 17:30
  • See the answer below. – Jay Blanchard Feb 24 '20 at 17:33
  • 1
    You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and others will be encouraged to help you. You need to revisit each of your previous questions to make sure you're doing this. – Jay Blanchard Feb 24 '20 at 17:37

2 Answers2

0

In order to do this correctly the condition must be evaluated:

foreach($result as $row)
{
 $output .= '
<div class="panel panel-default">
  <div class="panel-body">'.$row["comment"].'</div>

  <div class="panel-footer">';

if ($user_id == $row['user_id']) {

     $output .=  '<form  class="input-group form-row" action="comment_delete1.php" method="post" enctype = "multipart/form-data">
        <div class="input-group-prepend">
         <input type="hidden" name="comment_id" id="comment_id" value="'.$row["comment_id"].'" />
         <input type="hidden" name="user_id" id="user_id" value="'.$row["user_id"].'" />
         <input type="submit" name="submit" id="submit" class="submit btn btn-default" value="Delete" />
        </div>
      </form>   

      <button type="button" class="btn btn-default edit" id="'.$row["comment_id"].'" >Edit</button>';

} else {

      $output .= '<button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>
    </div>
</div>
 ';
    }
}
echo $output;

This is really not a good way to do this and you should consider refactoring your code to make this much neater and easier to read.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

Here is the solution:

If we put conditions inside php string or heredoc, php will treat them as string and will display them on the screen as string. So we cannot have conditions inside string.

I put the if else conditions outside the php string variable, and I created two php string variables, one with delete and edit buttons, and the other one without delete and edit buttons. So if the user_id is equal to session user id, the php string variable with delete and edit buttons will be displayed. And, else if the user_id is not equal to session user id, the php string variable without delete and edit buttons will be displayed. The php string variable will be echoed at the end of all conditions.

Here is the php code:

$user_id = $_SESSION['user_id'];

foreach($result as $row) {

   if ($row['user_id'] === $user_id) {

$output .= '
<div class="panel panel-default">
  <div class="panel-body">'.$row["comment"].'</div>

  <div class="panel-footer">
     <form  class="input-group form-row" action="comment_delete1.php" method="post" enctype = "multipart/form-data">
        <div class="input-group-prepend">
         <input type="hidden" name="comment_id" id="comment_id" value="'.$row["comment_id"].'" />
         <input type="hidden" name="user_id" id="user_id" value="'.$row["user_id"].'" />
         <input type="submit" name="submit" id="submit" class="submit btn btn-default" value="Delete" />
        </div>
      </form>   

      <button type="button" class="btn btn-default edit" id="'.$row["comment_id"].'" >Edit</button>  
      <button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>
    </div>
</div>
 ';

    } else if ($row['user_id'] !== $user_id) {

$output .= '
<div class="panel panel-default">
  <div class="panel-body">'.$row["comment"].'</div>

  <div class="panel-footer">
      <button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'" >Reply</button>
  </div>
 </div>
 ';
    }
}

echo $output;

Brian
  • 448
  • 2
  • 6
  • 18