Free Arcade Script
FAS Help => V2.x => : soulwebsites 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
<?
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
<?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
-
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.
-
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.
-
Ok I have made a few changes
I have now put this in the functions.php
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
<?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?
-
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.
function messagecount () {
global $userid;
$message_count = mysql_query('SELECT COUNT(*) FROM dd_messages WHERE to_userid="$userid" AND status=\'1\'');
return $message_count;
}
-
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.
-
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
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
-
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.
-
thanks adam ive changed it to use session ;D
-
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...
<?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!";
}
?>
-
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?
-
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.