SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help displaying a comment with a foreach loop

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

    PHP Code:
    <?php
    $sql 
    "SELECT * FROM comments ";
        
    $sql .= " WHERE shoename_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>'
    ;
    ?>

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't loop over just an sql statement. You need to first execute that sql statement and get a result set.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    SharedLog.com is correct (as usual), you're trying to iterate over a string.

    Try something along the lines of...
    PHP Code:
    <?php
    $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">
                        %s wrote:
                    </div>
                    <div class="body">
                        %s
                    </div>
                    <div class="body">
                        %s
                    </div>
                    <div class="meta-info" style="font-size: 0.8em;">
                        %s
                    </div>
                </div>
                '
    ,
                
    $aComment['fullname'],
                
    $aComment['cons'],
                
    $aComment['pros'],
                
    $aComment['created']
            );
        }
    }
    else
    {
        echo 
    'No Comments';
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aww sivelbullet it works like a charm!!

    Thank you I just ajusting the time field in the table comments so that it looks like this on the comment display

    january - 5 - 2009, 12:34 pm

    more elegant


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •