Counting Rows in PHP

Associate
Joined
1 May 2006
Posts
810
Location
Bristol, UK
Happy new year everyone! :D
Ok so I'm 12 hours early :p

Anywho,
I've just started making my website Linky Linky, and I'm already having trouble :p

See the following text under each blog post "() Comments - Click Here To View Comments"
In between the brackets I want to insert the number of comments relating to said blog post. The best I've managed to come up with is inserting the total number of comments from the comments table.

The table layout is blog containing all the blog posts and blogcomments containing all blog comments (genius eh?:p)
blog:
uid - unique identifier
title - self explanitory
text - self explanitory
timestamp - self explanitory

blogcomments:
uid - unique identifier
bid - equal to uid in the blog table
poster - self explanitory
text - self explanitory
timestamp - self explanitory

Thanks guys!
 
I've tried both of these and they both keep coming up with errors :(
I think the problem is getting the $uid value.

The query to get the blog posts is:
Code:
$query_Blog = "SELECT * FROM blog ORDER BY uid DESC";
This is then used in a Repeat Region 5 times per page.

Am I just being stupid here? I cant seem to get my head around this :p

Edit: I was being stupid, I now have the $uid variable :p
 
Last edited:
Hokai,

I'm using the following code in the cell that I want to output $CRows to:
Code:
  <?php 
  $uid = $row_Blog['uid']
  $query = mysql_query("SELECT * FROM blogcomments WHERE bid = $uid", $Database) or die(mysql_error());
  $CRows = mysql_num_rows($query);
  ?>
However, I keep getting the following error:
Code:
Parse error: parse error, unexpected T_VARIABLE in C:\www\jasonsummers\blog.php on line 75

I've tested the output of $uid and it throws out the correct number, so I dont think that is the problem :confused:
Any suggestions appreciated :)
 
It still doesn't want to play ball :(

It might be worth mentionning that I'm using dreamweaver so that might be screwing it up :p

I'll post the whole file, see if any of you top blokes can make sense of it:
Code:
<?php require_once('Connections/Database.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Blog = 5;
$pageNum_Blog = 0;
if (isset($_GET['pageNum_Blog'])) {
  $pageNum_Blog = $_GET['pageNum_Blog'];
}
$startRow_Blog = $pageNum_Blog * $maxRows_Blog;

mysql_select_db($database_Database, $Database);
$query_Blog = "SELECT * FROM blog ORDER BY uid DESC";
$query_limit_Blog = sprintf("%s LIMIT %d, %d", $query_Blog, $startRow_Blog, $maxRows_Blog);
$Blog = mysql_query($query_limit_Blog, $Database) or die(mysql_error());
$row_Blog = mysql_fetch_assoc($Blog);

if (isset($_GET['totalRows_Blog'])) {
  $totalRows_Blog = $_GET['totalRows_Blog'];
} else {
  $all_Blog = mysql_query($query_Blog);
  $totalRows_Blog = mysql_num_rows($all_Blog);
}
$totalPages_Blog = ceil($totalRows_Blog/$maxRows_Blog)-1;

$queryString_Blog = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Blog") == false && 
        stristr($param, "totalRows_Blog") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Blog = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Blog = sprintf("&totalRows_Blog=%d%s", $totalRows_Blog, $queryString_Blog);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style1 {font-size: 12px}
-->
</style>
</head>

<body>
<table align="center" width="400">
<tr>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, 0, $queryString_Blog); ?>">First</a></div></td>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, max(0, $pageNum_Blog - 1), $queryString_Blog); ?>">Previous</a></div></td>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, min($totalPages_Blog, $pageNum_Blog + 1), $queryString_Blog); ?>">Next</a></div></td>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, $totalPages_Blog, $queryString_Blog); ?>">Last</a></div></td>
</tr></table><br><hr>
<?php do { ?>
<table width="650" align="center">
  <tr>
  <td width="500"><strong><?php echo $row_Blog['title']; ?></strong></td>
  <td><div align="right"><?php echo $row_Blog['timestamp']; ?></div></td>
  </tr>
  <tr>
    <td colspan="2"><?php echo $row_Blog['text']; ?></td>
  </tr>
  <td>
  <?php 
  $uid = $row_Blog['uid']
  $query = mysql_query("SELECT COUNT(*) FROM blogcomments WHERE bid = $uid", $Database) or die(mysql_error());
  $CRows = mysql_num_rows($query);
  ?>
  <span class="style1">(<?php echo $CRows ?>) Comments - Click <a href="index.php?page=blog2&post=<?php echo $row_Blog['uid']; ?>">Here</a> To View Comments</span></td>
    <td><div align="right" class="style1">Add Comment</div></td>
  </tr>
  <tr><td colspan="2"><hr></td></tr>
</table>
<?php } while ($row_Blog = mysql_fetch_assoc($Blog)); ?>
<table align="center" width="400">
<tr>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, 0, $queryString_Blog); ?>">First</a></div></td>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, max(0, $pageNum_Blog - 1), $queryString_Blog); ?>">Previous</a></div></td>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, min($totalPages_Blog, $pageNum_Blog + 1), $queryString_Blog); ?>">Next</a></div></td>
<td width="100"><div align="center"><a href="<?php printf("%s?pageNum_Blog=%d%s", $currentPage, $totalPages_Blog, $queryString_Blog); ?>">Last</a></div></td>
</tr></table>
</body>
</html>
<?php
mysql_free_result($Blog);
?>

Thanks a bundle :)
 
Augmented said:
Missing a semicolon at the end of the first line ($uid = $row_Blog['uid']).
*bangs head against wall* lol
Augmented I could kiss you mate!

Sure enough, after whacking in the semicolon everything worked fine. I'm such a dumbass.

Thanks to all of you for your help.

I'm off down the pub. Best wishes for 2007 guys!
 
Back
Top Bottom