SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Scripting X's Avatar
    Join Date
    May 2001
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    showing the $row count when you use a while( statement...

    Hi, this is my first post on Sitepoint and I am going to use this post to check how brainy u lot are

    Can you help me please with a problem I am having....

    I will show you the code so that you have an idea what I mean...

    Code:
    <?php
    mysql_select_db("scripts_general", $link);
    $query = "SELECT * FROM news ORDER BY date DESC LIMIT 5";
    $result = mysql_query($query) or die (mysql_error());
    while($row=mysql_fetch_array($result)){
    $id = $row['id'];
    ?>
    Now lets say I want the count of how many comments are in each news article, here's what I do...

    Code:
    <?php
    
    mysql_select_db("scripts_general", $link);
    $result = mysql_query("SELECT * FROM comments WHERE id = '$id'", $link);
    $total = mysql_num_rows($result);
    
    ?>
    in the correct place I echo it out, by doing

    Code:
    <?php echo "$total"; ?>
    However this doesn't work at all, and I do not know why, the only reason I can think as to why it does not work is because of the while($row=mysql_fetch_array($result)){ statement, does anyone know how I could show how many results were in the comments table instead of this error I am getting...

    P.S. I am not getting a parse error or anything like that, all I am recieving is either one $row from the news table, or if I take out the WHERE statement, I get just the total count overall displayed on each news article...

    SOMEBODY PLEASE COME TO MY RESCUE !

    Thanks, and good luck,
    Chris.
    cgi://www.scriptingx.com - Everything CGI

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should post PHP specific questions in the PHP forum in future The fairies will come and move this soon

    In your first code snippet you are missing a closing curly brace "}" to end your while loop. All "{" must have a matching "}" to enclose the control strucure.

    Also, do you realise that every time that while loop is executed you over-write the value of $id with the value from the next row in the result set? You could alter this by placing all the values into an array (ie, make $id an array) like so:
    $id[] = $row['id'];

    Other than that, I can't see a problem with the code. I would write the complete script like so:
    PHP Code:
    code:
    <?php
    mysql_select_db
    ("scripts_general"$link);
    $query "SELECT * FROM news ORDER BY date DESC LIMIT 5";
    $result mysql_query($query) or die (mysql_error());
    $total mysql_num_rows($result);

    while(
    $row=mysql_fetch_array($result)){
       
    $id $row['id'];
    }
    ?>
    ...and later ...
    <?php echo $total ?>

  3. #3
    SitePoint Enthusiast Scripting X's Avatar
    Join Date
    May 2001
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    :( it doesn't work...

    Ok, it doesn't work... what i'll do is give you the entire script so that you can see what I am trying to archieve here...

    Code:
    <?php
    mysql_select_db("scripts_general", $link);
    $query = "SELECT * FROM news ORDER BY date DESC LIMIT 5";
    $result = mysql_query($query) or die (mysql_error());
    $total = mysql_num_rows($result);
    
    while($row=mysql_fetch_array($result)){
       $id[] = $row['id'];
    
    $result = mysql_query("SELECT * FROM comments WHERE id = '$id'", $link);
    $total = mysql_num_rows($result);
    
    ?>
    
    <table BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%" BGCOLOR="#000000">
          <tr>
            <td>
              <table WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2">
                <tr>
                  <td COLSPAN="1" BGCOLOR="#CCCDD0"><B> News</B>: Submitted by <A HREF="mailto:<?php echo "$row[email]"; ?>"><?php echo "$row[user]"; ?></A> <A HREF="http://www.scriptingx.com/forums/bb_profile.php?mode=view&user=<?php echo "$row[dbuser]"; ?>"><IMG SRC="http://www.scriptingx.com/images/user_profile.gif" BORDER="0" ALT="View <?php echo "$row[user]"; ?>'s Profile"></A><BR>
    <?php
    
    $title = $row['title'];
    $smilie = ereg_replace(":)", "<IMG SRC=\"forums/images/smiles/icon_smile.gif\">", $title);
    echo "<B>Title</B>: * $smilie";
    
    ?>
    
    </td>
                </tr>
                <tr>
                <td BGCOLOR="#EDEDF1" valign="top">
    
    <?php
    
    $message = $row['news'];
    $smilie = ereg_replace(":)", "<IMG SRC=\"forums/images/smiles/icon_smile.gif\" ALT=\"Smile !\">", $message);
    echo "$smilie";
    
    ?>
    
    </A>
    </td></tr>
    <tr>
    <td BGCOLOR="#CCCDD0">
    <table border="0" width="100%" cellspacing="0" cellpadding="0">
    <tr>
    <td width="75%"><B>Date Submitted</B>: <?php echo "$row[date]"; ?></td>
    <td width="25%" align="right"><A HREF="printable.php?topic=main&id=<?php echo "$row[id]"; ?>"><IMG SRC="http://www.scriptingx.com/images/print.gif" ALT="Printer friendly version" BORDER="0"></A> <A HREF="comments.php?id=<?php echo "$row[id]"; ?>"><IMG SRC="http://www.scriptingx.com/images/comments/comments.gif" ALT="View comments to this article, or comment on it yourself" BORDER="0"></A> <?php echo $total ?></td>
    </td></td></td></tr></tr></tr></table></table></table>
    
    <BR>
    
    <? } ?>
    Ok...I hope that helps some what...maybe u can help me fix it or something...

    Chris.
    cgi://www.scriptingx.com - Everything CGI


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
  •