louismoore18
louismoore18

Reputation: 907

php echo not displaying

This code

<?php  echo "Likes: ".$r['votes_up']."&nbsp;"; echo "Dislike: ".$r['votes_down'].""; ?>        

Wont post the values from the table for 'votes_up' 'votes_down'
I cant get my head round this! Ive got this exact code working on a different page but it wont on this.
Heres the entire code ....

    <div class="message">
<?php 
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die(mysql_error());     
    while($r = mysql_fetch_array($sql))  {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." &nbsp; $posted";?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
        Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2"><?php echo "".$r['message'].""; }?></div> 
<?php  echo "Likes: ".$r['votes_up']."&nbsp;"; echo "Dislike: ".$r['votes_down'].""; ?>        
</div>
<br/>
<hr>

Can anyone help? its driving me insane

Upvotes: 0

Views: 187

Answers (4)

JConstantine
JConstantine

Reputation: 3931

Your curly bracket is being placed before the last echo statement, therefore the $r variable is out of scope.

Move the } to later in your page like so

<div class="message">
<?php 
    $sql = mysql_query("SELECT * FROM threads WHERE id = '" . mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());     
    while($r = mysql_fetch_array($sql))
    {
        $posted = date("jS M Y h:i",$r['posted']);
        echo $r['author']." ".$posted;
?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $r['message']; ?>">
    Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2">
    <?php
        echo $r['message']; 
    ?>
</div> 
    <?php
        echo "Likes: ".$r['votes_up']."&nbsp;";
        echo "Dislike: ".$r['votes_down'];
    } 
    ?>        
</div>
<br/>
<hr>

Also notice the call to mysql_real_Escape_string in the $sql var. this will prevent nasty sql injections

Upvotes: 1

Jules
Jules

Reputation: 7213

This is because the $r['...'] variable is out of the scope where you place it.

Better way to do it (assuming you only fetch one row:

<?
   $sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die (mysql_error());
   $row = mysql_fetch_assoc($sql);
?>

<div> ... </div>
<?php echo "Likes: " . $row['votes_up'] . " &nbsp;  Dislike: " . $row['votes_down']; ?>

Upvotes: 0

Niko Efimov
Niko Efimov

Reputation: 2213

You seem to have closed the WHILE loop here:

<div class="message2"><?php echo "".$r['message'].""; }?></div>

Notice the curly bracket.

Therefore, votes_up and votes_down have no values.

Upvotes: 2

jenovachild
jenovachild

Reputation: 1781

Line 8 of your code

<div class="message2"><?php echo "".$r['message'].""; }?></div> 

Why is that closing curly brace in there before the closing ?> ?

Upvotes: 2

Related Questions