*

Recent

Author Topic: Message Count  (Read 7128 times)

soulwebsites

  • Jr. Member
  • **
  • Posts: 71
  • Force: +0/-0
    • Jamsite
Message Count
« on: February 23, 2012, 04:06:43 PM »
Hi I have been having a go at trying to make a message count function so I can display it next to the users avatar on the main page but im failing miserably

Here is my code:

This is called messagecount.php
Code: [Select]
<?
function messagecount () {
global $domain, $db, $userid, $mes;

$mes = $db->query(sprintf('Select * from DD_Messages WHERE to_userid='.$userid.' and status="1"')) ;



echo $mes;
}
?>

In Template.php
Code: [Select]
<?php include ("templates/$template/messagecount.php"); ?>
I have tried variations and quite a few googles but the one thing I found that I thought would work "Select Count" doesnt seem to work due to invalid function/table name  ::)

Thanks for any help.... ;D

Dillon

  • Developer
  • Sr. Member
  • ******
  • Posts: 340
  • Force: +3/-0
    • Arcade Freak
Re: Message Count
« Reply #1 on: February 23, 2012, 07:53:43 PM »
Im not quite understanding what it is your trying to count and on template you cant just include the file you have to call the function too.

Adam LaCombe

  • Sr. Member
  • ****
  • Posts: 433
  • Force: +17/-0
    • My Blog
Re: Message Count
« Reply #2 on: February 24, 2012, 12:51:25 AM »
So yeah just echo the function out like this as spagetiokillers said:

<?php
echo messagecount();

////// or --> //////

$message_count = messagecount();
echo $message_count;
?>

Please use <?php and not short tags ( <? ) also btw.

soulwebsites

  • Jr. Member
  • **
  • Posts: 71
  • Force: +0/-0
    • Jamsite
Re: Message Count
« Reply #3 on: February 26, 2012, 10:48:28 AM »
Ok I have made a few changes

I have now put this in the functions.php
Code: [Select]
function messagecount () {
 global $userid;
$message_count = mysql_query('SELECT COUNT(Status) FROM dd_messages WHERE to_userid='.$userid.' AND status=\'1\'');

}

Then this in template.php
Code: [Select]
<?php 
$message_count messagecount();
echo 
$message_count; ?>

i didnt even realise I was missing the php from <?php thanks Adam

Spagetiokillers I am trying do a query which looks for your userID in the to_userid column then looks for how many messages have the value 1 returning X value so if a user has 5 unread messages it will display a 5 next to the avatar

I have ran the sql query in my webhost sql admin and it brings back exactly what I need.

any ideas?
« Last Edit: February 26, 2012, 10:54:42 AM by soulwebsites »

Dillon

  • Developer
  • Sr. Member
  • ******
  • Posts: 340
  • Force: +3/-0
    • Arcade Freak
Re: Message Count
« Reply #4 on: February 26, 2012, 04:25:00 PM »
Your problem is in the function your missing the return and setting the to_userid. I made it actually display something however it seems to be displaying an array and i cant get it to display a count for some reason. This will hopefully help you get there tho.

Code: [Select]
function messagecount () {
 global $userid;
$message_count = mysql_query('SELECT COUNT(*) FROM dd_messages WHERE to_userid="$userid" AND status=\'1\'');
return $message_count;
}

Adam LaCombe

  • Sr. Member
  • ****
  • Posts: 433
  • Force: +17/-0
    • My Blog
Re: Message Count
« Reply #5 on: March 01, 2012, 10:52:28 AM »
Im on my way home now so im mobile but i think you need to wrap that query in a ' mysql_result($query, 0) '
If im wrong ill figure it out in hopefully a few hours if traffic aint bad.

soulwebsites

  • Jr. Member
  • **
  • Posts: 71
  • Force: +0/-0
    • Jamsite
Re: Message Count
« Reply #6 on: March 03, 2012, 06:02:37 AM »
Thanks spagetiokillers, Adam and Google  ;D

after adding a return and a result + the below it worked

the other part of my problem was the "to_user" when I just put $userid it didn't work however if i just put 1 it displayed my message count fine so I knew as usual i had done something wrong  ::)

here is the working code

Code: [Select]
function messagecount () {
 global $userid, $usrdata;

$userid = $usrdata['userid'];
$message_count = "SELECT COUNT(*) FROM dd_messages WHERE to_userid='$userid' AND status='0'";
$message_count = mysql_query($message_count) or exit (mysql_error());
$message_count = mysql_result($message_count,0);

return $message_count;

thanks again..... ;D

Adam LaCombe

  • Sr. Member
  • ****
  • Posts: 433
  • Force: +17/-0
    • My Blog
Re: Message Count
« Reply #7 on: March 04, 2012, 09:17:47 AM »
You need to use "$suserid".
You can also use $_SESSION['userid']

But if you use $_SESSION['userid'] in a query you need to wrap curly brackets around it.

soulwebsites

  • Jr. Member
  • **
  • Posts: 71
  • Force: +0/-0
    • Jamsite
Re: Message Count
« Reply #8 on: March 06, 2012, 01:05:42 PM »
thanks adam ive changed it to use session  ;D

Adam LaCombe

  • Sr. Member
  • ****
  • Posts: 433
  • Force: +17/-0
    • My Blog
Re: Message Count
« Reply #9 on: March 07, 2012, 04:49:09 PM »
Welcome.

Doing it that way is fine for what you're using it for. but.. say you try to echo $_SESSION['userid'] and the user isn't signed in then it would display a error.

So in other cases you would want to check to make sure the variable isn't empty or not set by doing the following...

Code: [Select]
<?php
if(!empty($_SESSION['userid']))
{
    
//the user is signed in so you can echo user info
    
echo $_SESSION['userid'];
}
else
{
    
//user is not signed in so you can echo a error or do nothing
    
echo "Sorry, but you need to be logged in!";
}
?>


soulwebsites

  • Jr. Member
  • **
  • Posts: 71
  • Force: +0/-0
    • Jamsite
Re: Message Count
« Reply #10 on: March 10, 2012, 04:13:46 AM »
after the count was working when I called the function instead of just calling I changed so it had the "if(!isset($suserid)) *** else ***" wrapped round it to stop it showing for non logged in users is that ok or should I still change to match your code?

Is one more secure that the other?

Dillon

  • Developer
  • Sr. Member
  • ******
  • Posts: 340
  • Force: +3/-0
    • Arcade Freak
Re: Message Count
« Reply #11 on: March 10, 2012, 04:32:36 PM »
after the count was working when I called the function instead of just calling I changed so it had the "if(!isset($suserid)) *** else ***" wrapped round it to stop it showing for non logged in users is that ok or should I still change to match your code?

Is one more secure that the other?


Yours will work just as good. theyre both the same basically.