Forum Moderators: coopster

Message Too Old, No Replies

Function to trigger a function?

Trying to count comments on a blog

         

skeddy

12:51 pm on Mar 27, 2005 (gmt 0)

10+ Year Member



Hello,

I've been writing a blog system for a whiler now, I've just gout round to adding in the ability to add comments to a blog entry.

I'm trying to display the number of comments for a particular entry, and so far the only way I can do this is to have the following lines of code repoeted after each "do, while".

I've currently go the blog page to spit out the following


<?php do {?>
<?php require("path/to/swearfilter.php");?>
<div class="title"><?php echo nl2br($output['title']);?><br />
<?php echo date("l F j, Y",$output['date']);?></div>
<div class="blog">
<?php echo nl2br(strip_tags(substr($output['news'],0,500)));?>.....<br />
<br />
</div>
<div class="posted"><? echo "<a href=\"main.php?section=post&id=" . nl2br($output["id"]). "\">Read More?</a>" ;?>
(<?php echo strlen($output['news']);?>) Bytes more to read<br />
<img src="images/tiny/read.gif" border="0" />
<?php echo nl2br($output['hits']);?> Reads<br />
<a href="javascript:popUp('../sections/comments.php?id=<?php echo nl2br($output["id"]);?>')">Comments</a>
( <?php
$comments = mysql_query("SELECT * FROM comments WHERE blog_id='$output[id]'");
$numcoms = mysql_numrows($comments);
echo "$numcoms \n";
?> )
</div><br />
<?php } while ($output = mysql_fetch_array($getnews));?>

What I would like to do is replace the following line with a function instead:


<?php
$comments = mysql_query("SELECT * FROM comments WHERE blog_id='$output[id]'");
$numcoms = mysql_numrows($comments);
echo "$numcoms \n";
?>

So far, I've tried using the following function with no sucess


function numcom() {
$result = mysql_query("SELECT * FROM comments WHERE blog_id='$output[id]'");
$num_rows = mysql_num_rows($result);
echo "$num_rows \n";
}

and I've been calling that as <?php echo numcom()?>

I think the reason that it's not working as a function is becuase I'm telling the function to pick up the $output['id'].

If I change that to an id of one of my blogs, say 104, then it works perfectly going all the way down the page, only displaying the number of comments if the blog is 104.

Any thoughts?

dreamcatcher

4:46 pm on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Try passing the value directly into the function and use return. So create your function like this:

function numcom($id) {
$result = mysql_query("SELECT * FROM comments WHERE blog_id='$id'");
$num_rows = mysql_num_rows($result);
return $num_rows;
}

Then call it using:

echo numcom($output[id]);

dc