zackcez Posted March 21, 2008 Share Posted March 21, 2008 ok i got it down to this code: <?php mysql_connect("db174.perfora.net", "dbo239605978", "NOPASS?") or die(mysql_error()); mysql_select_db("db239605978") or die(mysql_error()); if (isset($_GET['s'])) { $search = $_GET['s']; $query = "SELECT * FROM news WHERE id LIKE '%$search%' ORDER BY id DESC LIMIT 5"; $result2 = mysql_query("SELECT newsid FROM comments WHERE newsid='$row['id']'"); } else { $query = "SELECT * FROM news ORDER BY id DESC LIMIT 5"; } $result = mysql_query($query) or die(mysql_error()); $count = mysql_num_rows($result2); while($row = mysql_fetch_array($result)){ echo "<div class=\"box\">"; echo "<a name=\"" . $row['name'] . "\"></a><h1><a href=\"index.html\">" . $row['title1'] . " <span class=\"white\">" . $row['title2'] . "</span></a></h1>"; echo "<p class=\"post-by\">Posted by <a href=\"index.html\">" . $row['auther'] . "</a></p>"; echo "<p>" . $row['story'] . "</p>"; echo "<p class=\"post-footer align-right\">"; echo "<a href=\"index.html\" class=\"readmore\">Read more</a>"; echo "<a href=\"index.html\" class=\"comments\">Comments ($count)</a></p>"; echo "</div>"; } ?> That should be a bit closer to my problem...but i get an error yet: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /homepages/20/d239047801/htdocs/news.php on line 7 I know what the error means I just don't know any other way to use the code... Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/ Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 Change line 7 to: $result2 = mysql_query("SELECT newsid FROM comments WHERE newsid='" . $row['id'] . "'"); or: $result2 = mysql_query("SELECT newsid FROM comments WHERE newsid='$row[id]'"); Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497955 Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 Also a better way to count rows is this: $result2 = mysql_query("SELECT COUNT(newsid) AS total FROM comments WHERE newsid='" . $row['id'] . "'"); $values2 = mysql_fetch_assoc($result2); $count = $values2['total']; Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497957 Share on other sites More sharing options...
zackcez Posted March 21, 2008 Author Share Posted March 21, 2008 doesn't work, I've inserted one comment into the table for that newsid and the count comment comes up blank yet...there's most-likely an error in my php somewhere else that I didn't notice...:s but thanks for that error help Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497959 Share on other sites More sharing options...
zackcez Posted March 21, 2008 Author Share Posted March 21, 2008 that doesn't work either.... :s Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497965 Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 mysql_num_rows is perfectly find for a count because either way you need to run a query + return a count via working said resource. Do u get an error now? Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497966 Share on other sites More sharing options...
zackcez Posted March 21, 2008 Author Share Posted March 21, 2008 There's no error i just doesn't show the number of comments on that story: phpbbboards.org it should say "Comments(1)" Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497968 Share on other sites More sharing options...
Jeremysr Posted March 21, 2008 Share Posted March 21, 2008 Oh, don't you want to count the number of comments for each article? Shouldn't the counting code go inside the while loop? You're trying to use $row on line 7 but it doesn't exist yet. Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497971 Share on other sites More sharing options...
zackcez Posted March 22, 2008 Author Share Posted March 22, 2008 Thank you very much, Problem solved. Link to comment https://forums.phpfreaks.com/topic/97301-count-comments-s/#findComment-497999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.