Need help displaying a comment

Discussion in 'PHP' started by co.ador, Jul 12, 2009.

  1. #1
    It is not displaying anything right now drop some input thanks.

    <?php
    $sql = "SELECT * FROM comments ";
        $sql .= " WHERE appetizers_id= '$id '";
        $sql .= " ORDER BY created ASC";
    	
    
    echo '<div id="comments">
      <?php foreach($sql as $comment): ?>
        <div class="comment" style="margin-bottom: 2em;">
    	    <div class="author">
    	      <?php echo htmlentities($fullname); ?> wrote:
    	    </div>
          <div class="body">
    				<?php echo strip_tags($cons); ?>
    			</div>
    			<div class="body">
    				 <?php echo strip_tags( $pros); ?>
    			</div>
    	    <div class="meta-info" style="font-size: 0.8em;">
    	      <?php echo datetime_to_text($created); ?>
    	    </div>
        </div>
      <?php endforeach; ?>
      <?php if(empty($sql)) { echo "No Comments."; } ?>
    </div>';
    ?>
    
    PHP:
     
    co.ador, Jul 12, 2009 IP
  2. zahidraf

    zahidraf Well-Known Member

    Messages:
    450
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    check that id should not be empty and then print the query and manualy run it in the mysql and see the results .
     
    zahidraf, Jul 12, 2009 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    First you need to query the SQL statement, which it seems you didn't. Also you need to rewrite your HTML/PHP code which is all mixed up together.
     
    ThePHPMaster, Jul 12, 2009 IP
  4. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have done what you told me guys but still is not working. any other suggestion?

    <?php
    $sql = "SELECT * FROM comments ";
        $sql .= " WHERE appetizers_id= '$id '";
        $sql .= " ORDER BY created ASC";
    	$result = mysql_query($sql) or die ("Error in query: $query. ".mysql_error()); 
    
    // see if any rows were returned 
    if (mysql_num_rows($result) > 0) {
    
    echo '<div id="comments">
      <?php foreach($sql as $comment): ?>
        <div class="comment" style="margin-bottom: 2em;">
    	    <div class="author">
    	      <?php echo htmlentities($fullname); ?> wrote:
    	    </div>
          <div class="body">
    				<?php echo strip_tags($cons); ?>
    			</div>
    			<div class="body">
    				 <?php echo strip_tags( $pros); ?>
    			</div>
    	    <div class="meta-info" style="font-size: 0.8em;">
    	      <?php echo datetime_to_text($created); ?>
    	    </div>
        </div>
      <?php endforeach; ?>
      <?php if(empty($sql)) { echo "No Comments."; } ?>
    </div>';}
    ?>
    PHP:
     
    co.ador, Jul 12, 2009 IP
  5. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Did you not read my other post on your other thread

    You are running the query now but you need to loop through it as i've shown, your foreach loop should be using $result as this is the resource you need to loop through, $sql is simply a String.
     
    wd_2k6, Jul 12, 2009 IP
  6. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thank you wd_2k6
    Right now it is displaying and inserting. I will clean the <?php open and closing tags ?> so it is not so confusing.

    Next i will be working in a rating system, it will be alot of fun..
     
    co.ador, Jul 12, 2009 IP
  7. co.ador

    co.ador Peon

    Messages:
    120
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is the loop printing all the message working perfectly but now it keep repeating the same message everytime the page is refreshed in firefox is there a way to avoid that? by redirecting the page to avoid repetition?

    The following message appears when I refresh the browser:

    How can it be avoided so it just save the data and then continue without repeating the previous message over?

    this is the while loop and the mysql query statement to call the comments from the database
    $rResult = mysql_query(
        sprintf(
            "SELECT * FROM comments WHERE shoename_id = %d ORDER BY created ASC",
            $id
        )
    );
    if(mysql_num_rows($rResult) > 0)
    {
        while($aComment = mysql_fetch_assoc($rResult))
        {
            printf(
                '
                <div class="comment" style="margin-bottom: 2em;">
                    <div class="author">
                       <strong>%s</strong> wrote:
                    </div>
                    <div class="body">
                        <strong>Pros:</strong>%s
                    </div>
                    <div class="body">
                        <strong>Cons:</strong>%s
                    </div>
                    <div class="meta-info" style="font-size: 0.8em;">
                        <strong>Time:</strong>%s
                    </div>
                </div>
                ',
                $aComment['fullname'],
                $aComment['pros'],
                $aComment['cons'],
                $aComment['created']
            );
        }
    }
    PHP:
     
    co.ador, Jul 12, 2009 IP