简体   繁体   中英

using php code inside an echo

My question to you is how do I get the code below to echo its entirety? I have multiples of these that I need to echo using while and I have toyed with it but have yet to figure out what to do. The answers I have seen, I have tried but they just don't work on my code. I need to have all this code here in one bunch but I am having an issue inserting the "like button" section. The issue starts at

$likes = (empty($_POST['like'])) ? : $_POST['like'] ;

and here's the full code

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  echo ' 
<div class="wrapper">
<div class="submissions">
<div class="logo-logo"><h2>Questions.</h2>
<div class="checkboxes">'.$row['formtype'].'
</div>

</div>
<div class="top-submit">
&#8220'. $row["actual_quote"] . '&#8221;
</div>
<div class="poster">- '. $row["poster"].'
 <div class = "like">- '.
  $likes = (empty($_POST['like'])) ? : $_POST['like'] ;
  $dislikes = (empty($_POST['dislike'])) ? : $_POST['dislike'] ;
  $ip = $_SERVER['REMOTE_ADDR'];

  if(isset($_POST['like'])){
    $likes1 = $likes+1;
    $voted1 = $voted+1;
    $query2 = $db->prepare("INSERT INTO voters (voted, ip) VALUES ( :voted, :ip)");
    $query2->bindParam(':voted', $voted1, PDO::PARAM_STR);
    $query2->bindParam(':ip', $ip, PDO::PARAM_STR);  
    $query2->execute();
    header("Location: like.php?");
    $update1 = $db->prepare("INSERT INTO votes (likes) VALUES ( :likes)");
    $update1->bindParam(':likes', $likes1, PDO::PARAM_STR);
    $update1->execute();
  }

  if(isset($_POST['dislike'])){
    $dislikes1 = $dislikes+1;
    $voted1 = $voted+1;
    $query2 = $db->prepare("INSERT INTO voters (voted, ip) VALUES ( :voted, :ip)");
    $query2->bindParam(':voted', $voted1, PDO::PARAM_STR);
    $query2->bindParam(':ip', $ip, PDO::PARAM_STR);  
    $query2->execute();
    header("Location: like.php?");
    $update1 = $db->prepare("INSERT INTO votes (dislikes) VALUES ( :dislikes)");
    $update1->bindParam(':dislikes', $dislikes1, PDO::PARAM_STR);
    $update1->execute();
  }
  $stmt = $db->query("SELECT * FROM voters");
  $stmt->setFetchMode(PDO::FETCH_ASSOC);
  $row3 = $stmt->fetch();

  echo "Likes: $likes <br /> Dislikes: $dislikes<br />";

  if(isset($row3['voted'])){
    if(isset($row3['ip'])){
      echo "You have already voted for this.";
    }
  } else {

    echo "<form action = '' method = 'post'> <input type = 'submit' name = 'like' value  = 'like'> <input type = 'submit' name = 'dislike' value = 'dislike'></form>";

  }'
</div>
<!-- use select to get the items to stay on the page-->

</div>
</div>
</div>
';
}

There may be a very simple solution but I have searched everywhere for it. I have tried using a . at the end but it doesn't like that. Any suggestions?

EDIT I have changed one portion, the whole code starting at $likes and ending after else{} has been put as this:

  <div class = "like">';
  include("like.php");
  echo'</div>

You don't. You stop your echo, do your other code, and start echoing again.

echo 'foo';
bar();
echo 'baz';

You shouldn't use echo in this way.

Try to keep your HTML in variable and concatenate all needed additional HTML using dot after checking all necessary conditions.

$output = '<div>blahblah</div>';
if ($somedatafromDB == true) {
  $output .= '<p>true!!</p>';
} else {
  $output .= '<p>false :/</p>';
}

// and finally
echo $output;

An issue might also be the ternary operator:

Your code is currently

$likes = (empty($_POST['like'])) ? : $_POST['like'] ;

Try to change it to

$likes = (empty($_POST['like'])) ? 0 : $_POST['like'];

You need to specify what you would like to return if the $_POST['like'] is empty.

The ternary operator ( x?y:z ) returns y if x is true, else it returns z . In your case y is missing which might cause an error during execution.

A good practice is ini_set("display_errors", "on"); at the beginning of the script for debugging purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM