So Close.. Help with echoing Total Comments to just the User

Need some help with the script?

So Close.. Help with echoing Total Comments to just the User

Postby Heya Im Me » Sun Jun 17, 2007 10:28 pm

I've been trying to figure this one out for a while but I'm at the point where I'm not progressing forward so I figured I'd ask for some help.

I tried making a function to read the comments table, match the logged user's username from the table and then count all of the comments they have made but I just can't get it. For some reason I can't pull the user's actual username into my code.

The point I'm at now is:

Code: Select all
function totalcomments() {
global $sql;

echo "Total: ".count($sql->select(array('table' => 'comments', 'where' => array("author =".Username Here))));
}


Where you see Username Here is where I need the username that is logged in to appear. I know the code works because if I manually put the user's name into the code but obviously that isn't a practical solution (or one at all). I just can't get the actual username to appear in there on its own once the person is logged in.

Can anyone fit the last puzzle piece into this one?

As always, thanks in advance for preventing me from pulling all of my hair out :D
Heya Im Me
 
Posts: 189
Joined: Wed Feb 01, 2006 4:06 am

Postby FI-DD » Sun Jun 17, 2007 11:07 pm

Code: Select all
function totalcomments($user_input) {
global $sql;

$count = 0;
foreach($sql->select(array('table' => 'comments')) as $row){
   if($row['author'] == $user_input) $count++;
}

echo "Total: ".$count;


Then use totalcomments($member['username']); where you want to show the result.
Last edited by FI-DD on Mon Jun 25, 2007 5:47 pm, edited 1 time in total.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby Heya Im Me » Sun Jun 17, 2007 11:14 pm

Would have absolutely never figured that out. Thanks so much FI-DD!
Heya Im Me
 
Posts: 189
Joined: Wed Feb 01, 2006 4:06 am

Postby Heya Im Me » Sat Jun 23, 2007 7:51 pm

Hm, I just noticed the same bug as the registration mod. This code will count the logged in user's comments if they don't have a space in their username. If a username has a space in it, the number of comments will be put out as 0 regardless of the amount of comments that user has made. Any ideas? If not, can I somehow not allow users to register names with spaces at all?
Heya Im Me
 
Posts: 189
Joined: Wed Feb 01, 2006 4:06 am

Postby FI-DD » Mon Jun 25, 2007 5:47 pm

It's working now, check the code above.
User avatar
FI-DD
Admin
 
Posts: 2971
Joined: Thu Sep 22, 2005 11:27 am
Location: Germany

Postby typeman » Thu Feb 04, 2010 1:44 am

Would anyone be able to tell me which file to put this code in?

Thanks :)
typeman
 
Posts: 102
Joined: Sat Oct 17, 2009 7:38 am

Postby Torstein » Thu Feb 04, 2010 12:11 pm

I would guess inc/functions.inc.php
Torstein
 
Posts: 292
Joined: Thu Aug 03, 2006 11:19 pm

Postby typeman » Sat Feb 06, 2010 6:58 pm

Torstein wrote:I would guess inc/functions.inc.php


Perfect, thank you :)
typeman
 
Posts: 102
Joined: Sat Oct 17, 2009 7:38 am


Return to Help



Who is online

Users browsing this forum: No registered users and 1 guest