Show comment counter

Discussion in 'Web Design and Development' started by Cabbit, Jul 4, 2007.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    ok i would like to display the amount of comments each artical has next to the text, how do i go about doing this.

    Its a SQL database and PHP.

    In the database there are 4 feilds id, story_id, name, comment

    so i would have a story, and it has comments so i would like it to display "Comments (3)" but i dont know how to set it up to only display the amount of comments for the artical or story only.
     
  2. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    thanks i little working with that worked perfictly. After origianally being told it was impossible i knew there must be a way cos loads of sites do this, +1000% for my site usibility ^_^
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #4
    Code:
    <?php
    // Database connect //
    $server = mysql_connect("", "", "");
    $connection = mysql_select_db($server);
    //Connect to stories table //
    $sql = mysql_query('SELECT * FROM `storys` ORDER BY `id` DESC');
    $numofrows = @mysql_num_rows($sql);
    $result = mysql_query("SELECT * FROM storys_comments WHERE story_id = id", $server);
    $num_rows = mysql_num_rows($result);
    //Start Loop //
    print '<table width="100%" border="0" cellpadding="4" cellspacing="0" >';
    	print '<TR class="story_top"><td width="60%">Story</td><td width="15%">Comments</td><td width="15%">Author</td><td width="10%">Poster</td></TR>';
    		while($row = mysql_fetch_array($sql, MYSQL_BOTH)) {
    //start colour changing loop//
    			for($i = 0; $i < $numofrows; $i++)	{
    				$row = @mysql_fetch_array($sql);
    				// the ifelse //
    					if($i % 2) 
    						{ 
    							print '<TR class="pink">';
    						} 
    				else 
    							{ 
    								print '<TR class="wight">';
    							}
    //the output//
    	printf("<td><a href=\"story_display.php?id=%s&usrname=$usrname\">%s</a></td><td>%s</td><td><span style=\"TEXT-ALIGN:center\">%s</span></td><td>%s</td>", $row["id"], $row["title"], $row[$num_rows], $row["author"], $row["poster"]);
    		print "</TR>";
    //end colour changing loop//
    													}
    															}
    	print "</table>";
    //end //
    ?>
    Im having problems implementing it here, in this case $num_rows prints out the story id instead of the number of comments the story has.
     

Share This Page