counting replies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

CoderGoblin - I see we have a smartass here. ;)

pleigh - What isn't working? Shows us your revised code. Is this site somewhere that we can see what is happening?
Last edited by smpdawg on Wed Mar 02, 2005 7:38 am, edited 1 time in total.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

this gives me headache.....errors:
Undefined variable: row in c:
and
mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c
please be advised that the two codes i've posted earlier are two different funtions, i embedded the replies funtion to the adminpagination function, and to view the result of the viewing page, i only called the adminpagination function, not the replies function for it is embedded in the adminpagination
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Then instead of getting $postID by accessing the $row variable, pass it into the function that is getting the number of comments.
Last edited by smpdawg on Wed Mar 02, 2005 7:45 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

smpdawg wrote:CodeGoblin - I see we have a smartass here.
Just following your logic through... Seriously though I have found it can make a lot of difference on response times even when doing a count (although I use postgres).

On to the answer...

Code: Select all

$row&#1111;0] = '<font size=1>'.$row&#1111;0].'</font>';
           echo "<tr><td width=20% class=tableborder align=center><table><tr><td><b><a href="" class="under">".strtolower($row&#1111;3])."</a><b></td></tr></table></td>
         <td width=50% class=tableborder><table><tr><td><a href="reportview.php?pid=&#123;$row&#1111;2]&#125;" class="under" style="font-weight: bold;">$row&#1111;1]</a></td></tr></table></td>
         <td width=10% class=tableborder><table><tr><td>".replies()."</td></tr></table></td>
         <td width=20% class=tableborder><table><tr><td>$row&#1111;0]</td></tr></table></td></tr>";
Pass the row[2] variable through to your replies function...

Code: Select all

<td width=10% class=tableborder><table><tr><td>".replies($row&#1111;2])."</td></tr></table></td>
The replies function now becoming

Code: Select all

function replies($postID)
&#123;
  $rcQuery = "SELECT count(postID) AS rowcount FROM comments WHERE postID=$postID";
  $rcResult = @mysql_query($rcQuery);
  if ($rcResult) &#123;
     return (@mysql_result($rcResult,0,'rowcount');
  &#125; else &#123;
     die("Error with $rcQuery");
  &#125;
&#125;
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Agreed, accessing a single field name is faster. In this case it was laziness that made me use the *. ;)

While hate is a strong word, I will say that I am not fond of PostGres. I can't stand how difficult PostGres makes it to dump a database to a file and create a new database from it. pg_dump. ugh. I have made dumps of databases and then had PostGres die in the middle of trying to reload it because it didn't like the data in its own dumps. Don't get me wrong, I still use PostGres for some of my work but I do it with a frown.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

wait....got mixed up....but thanks guys for your patience :oops:

coder --> i did what you shared to me but returned a parse error somewhere here

Code: Select all

if ($rcResult) 
  &#123;
     return (@mysql_result($rcResult,0,'rowcount');
  &#125;
spmdawg -->yes, i believe so because my adminpagination function will, as i can see, will generate the $_GET['pid']
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Sorry my fault, extra bracket...

Code: Select all

return @mysql_result($rcResult,0,'rowcount');
Off topic....

Postgres has many advantages especially when compared with older versions of MySQL (views being one), but several disadvantages. Lack of last_id and server support being a couple.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

hey guys, it did work, but an error occured
Warning: Missing argument 1 for replies() in c:\inetpub\wwwroot\mysample\functions\library.php on line 322
Error with SELECT count(postID) AS rowcount FROM comments WHERE postID=
to hightlight the line

Code: Select all

function replies($postID)
&#123;
     ......
&#125;
last one, and im on it.... :wink:
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

It would appear that when you called to the replies function, you did not send the parameter. Look back in the other function where replies() was called inside of that table.

See coders second code block.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Now you can see why I put the debug line in...

We are not getting a value for $postID hence not being placed in the SQL.

We now need to go back to where the function is called...

Code: Select all

<td width=10% class=tableborder><table><tr><td>".replies($row&#1111;2])."</td></tr></table></td>

Code: Select all

$query = "SELECT DATE_FORMAT(postupdate, '%M %D, %Y - %l:%i %p'), title, postID, firstname FROM posts AS p, users AS you
         WHERE p.userID = you.userID ORDER BY postupdate DESC LIMIT $start, $display";      
   $result = @mysql_query ($query); 
.
.
.
.
while ($row = mysql_fetch_array($result, MYSQL_NUM))
&#123;
should be returning the postID as $row[2].
to check we can put...

Code: Select all

while ($row = mysql_fetch_array($result, MYSQL_NUM))
&#123; 
var_dump($row);
Run the code and at a guess the 3rd array value $row[2] is empty.

You need to fix this within the DB itself.
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Pleigh is definitely missing the change in what is your first code example from that last post. The right error messages are being displayed for that to be the case as demonstrated by PHP complaining about a missing parameter as that would only have happened if the call to replies looks like this. replies()
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

tried some experimentations, but still get the same error....
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post by smpdawg »

Show us the updated code.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Show code again time. Please show the I showed in my previous post.

Did you try the var_dump ? If so please also show that output.
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

Post by pleigh »

ok...

Code: Select all

//function for administrator pagination
function adminpagination()
&#123;
$display = 10;
	
	
	if (isset($_GET&#1111;'np'])) 
	&#123; 
		$num_pages = $_GET&#1111;'np'];
	&#125; 
	else 
	&#123; 
		$query = "SELECT * FROM posts"; 
		$query_result = mysql_query ($query);
		$num_records = @mysql_num_rows ($query_result);
		
		if ($num_records > $display) 
		&#123; 
			$num_pages = ceil ($num_records/$display);
		&#125; 
		else 
		&#123;
			$num_pages = 1;
		&#125;
	&#125;
	
	
	if (isset($_GET&#1111;'s'])) 
	&#123; 
		$start = $_GET&#1111;'s'];
	&#125; 
	else 
	&#123;
		$start = 0;
	&#125;
			
	// Make the query.
	$query = "SELECT DATE_FORMAT(postupdate, '%M %D, %Y - %l:%i %p'), title, postID, firstname FROM posts AS p, users AS u
			WHERE p.userID = u.userID ORDER BY postupdate DESC LIMIT $start, $display";		
	$result = @mysql_query ($query); 
	$num = mysql_num_rows ($result); 
	
	if ($num > 0) 
	&#123; 	
		if ($num_pages > 1) 
		&#123;			
			
			$current_page = ($start/$display) + 1;
			
			
			if ($current_page != 1) 
			&#123;
				echo '<a href="report.php?s=' . ($start - $display) . 
				'&np=' . $num_pages . '" class=under>Previous</a> ';
			&#125;
			
			
			for ($i = 1; $i <= $num_pages; $i++) 
			&#123;
				if ($i != $current_page) 
				&#123;
					echo '<a href="report.php?s=' . (($display * ($i - 1))) . 
					'&np=' . $num_pages . '" class=under>' . $i . '</a> ';
				&#125; 
				else 
				&#123;
					echo $i . ' ';
				&#125;
			&#125;
			
			
			if ($current_page != $num_pages) 
			&#123;
				echo '<a href="report.php?s=' . ($start + $display) . 
				'&np=' . $num_pages . '" class=under>Next</a>';
			&#125;
									
		&#125; 
		
		
		echo "<tr align=center class=tablehead><td width=20% class=tableborder><table><tr><td><b>NAME</b></td></tr></table></td>
			<td width=50% class=tableborder><table><tr><td><b>TOPIC</b></td></tr></table></td>
			<td width=10% class=tableborder><table><tr><td><b>REPLIES</b></td></tr></table></td>
			<td width=20% class=tableborder><table><tr><td><b>DATE</b></td></tr></table></td></tr>";
		
		
		while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
		&#123;	
			//puta
			
					
			$row&#1111;0] = '<font size=1>'.$row&#1111;0].'</font>';
  			echo "<tr><td width=20% class=tableborder align=center><table><tr><td><b><a href="" class="under">".strtolower($row&#1111;3])."</a><b></td></tr></table></td>
			<td width=50% class=tableborder><table><tr><td><a href="reportview.php?pid=&#123;$row&#1111;2]&#125;" class="under" style="font-weight: bold;">$row&#1111;1]</a></td></tr></table></td>
			<td width=10% class=tableborder><table><tr><td>".replies($row&#1111;2])."</td></tr></table></td>
			<td width=20% class=tableborder><table><tr><td>$row&#1111;0]</td></tr></table></td></tr>";  
			
		&#125;		
	
		mysql_free_result ($result); 	
	
	&#125; 
	else 
	&#123; 
		echo '<h3>There are no records to show.</h3>'; 
	&#125;
	
	mysql_close(); 
&#125;

//funtion for counting replies
function replies($postID)
&#123;
  $rcQuery = "SELECT count(postID) AS rowcount FROM comments WHERE postID=$postID";
  $rcResult = @mysql_query($rcQuery);
  if ($rcResult) 
  &#123;
     return (@mysql_result($rcResult,0,'rowcount'));
  &#125; 
  else 
  &#123;
     die("Error with $rcQuery");
  &#125;
&#125;
Post Reply