FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need help.... Please !





salman_500
hey all,

you see i have this script..

Code:
$mostdownloadedsql = mysql_query("SELECT * FROM templates ORDER BY 'downloaded_times' DESC LIMIT 1") or die (mysql_error());

while ($mostdownloaded = mysql_fetch_array($mostdownloadedsql)) {
echo $mostdownloaded['name'];
}


while writing this i had in mind that it will get the name of the record with the highest number of downloads.....

but you see i have 3 test records in the table :

1 , 2 , 3 :

1 has the value 6
2 has the value 7
3 has the value 14

i put these values for testing purposes....

for as long as the digits are in a single figure... it works fine... but when it goes into 2 figures the script the script only considers the first digit....

so like if i do by "ASC" i get record 3.... and viceversa.... so any thing that im puttin in wrong???


oh oh....... one more thing....

how can i show total number of downloads.... like if the 3 records values in the "downloaded_times" add up to 27.... how can i get it to show this value......


thnx !!!
shamil
It should work unless type of downloaded_times field is not number. So change type to INT or NUMBER.

You can use below sql to get total.
Code:
SELECT sum(downloaded_times) as total FROM templates
Good luck.
salman_500
shamil wrote:
It should work unless type of downloaded_times field is not number. So change type to INT or NUMBER.

You can use below sql to get total.
Code:
SELECT sum(downloaded_times) as total FROM templates
Good luck.



kooool !!!!

thnx !!!! all fixed !!! Very Happy

tyvm !
salman_500
ok the above problem was solved...... but i have another query.....


Code:
$users_online = 1;

while ($sql_row = @mysql_fetch_array ($sql_res))
{
   if ((time() - $sql_row["tstamp"]) < (300))
   {    
      $users_online++;      
   }   
}


well you see i use this bit of code to verify the number of user online... but it has a bug..... it does not ignore similar ip's...... so what i want to do is make it so that it does not count the records which have ip's that have already been counted.....

what i tried is this :

Code:

// $u_ip  is array for user ip
// ip is the column name in database.....

if (((time() - $sql_row["tstamp"]) < (300)) &&  (!$sql_row["ip"] == $u_ip))      


but this didnt seem to work...... it makes no difference at all...

plz help... Thnx !
Kaneda
salman_500 wrote:
ok the above problem was solved...... but i have another query.....


Code:
$users_online = 1;

while ($sql_row = @mysql_fetch_array ($sql_res))
{
   if ((time() - $sql_row["tstamp"]) < (300))
   {    
      $users_online++;      
   }   
}


well you see i use this bit of code to verify the number of user online... but it has a bug..... it does not ignore similar ip's...... so what i want to do is make it so that it does not count the records which have ip's that have already been counted.....

what i tried is this :

Code:

// $u_ip  is array for user ip
// ip is the column name in database.....

if (((time() - $sql_row["tstamp"]) < (300)) &&  (!$sql_row["ip"] == $u_ip))      


but this didnt seem to work...... it makes no difference at all...

plz help... Thnx !


You can do that in one line with a SELECT statement. Something like:

Code:
$result = mysql_query('SELECT COUNT(DISTINCT ip) FROM templates WHERE tstamp > ' . (time() - 300));


(I avoid using MySQL's own NOW() to find the minimum timestamp, since I don't know how you stored the timestamp in the first place)

To simply get the list of ips:
Code:
$result = mysql_query('SELECT DISTINCT ip FROM templates WHERE tstamp > ' . (time() - 300));


These should be a good deal faster than doing the calculations in PHP. DISTINCT only gets the ips that are... distinct, i.e., it will leave out duplicates. COUNT(DISTINCT <columnname>) works the same way - it counts the number of distinct values.

The reason your own version doesn't work is that you're checking if an array($u_ip) equals a value ($sql_row['ip']), rather than whether the value is in the array. You should rather use something like:

Code:
in_array($u_ip,$sql_row['ip'])


You should also be aware that if you're using the DATETIME type or the TIMESTAMP type for the tstamp field, those types are not integers, hence, you can't simply subtract a number of seconds/milliseconds from them (so, the above code won't work either) - and you can't compare to time()'s output.

I generally avoid those types and use an INT to store a Unix timestamp (as returned by time()) - that's easier to work with, although you can't use all the mySQL internal datetime functions then.

In any event, the SELECT DISTINCT query is faster.
salman_500
ohh...thnx !!!

it works like a charm !!!

Laughing Very Happy Razz Laughing Smile Surprised Wink Twisted Evil Arrow Exclamation Idea

tyvm !
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.