...

View Full Version : Ranking System



Jesuspwnt
11-23-2007, 12:42 PM
Hey all, im creating a ranking system for my friends website, the main code for it is:

<?php
$types = array("Gu"=>"Guest", "Ga"=>"Gamer", "D"=>"Donor", "RO"=>"Recruiting Officer", "TO"=>"Training Officer", "RHM"=>"Right Hand Man", "CO"=>"Commanding Officer", "A"=>"Admin", "SA"=>"Server Admin");
echo "<table><tr><td>Username</td><td>User's Rank</td></tr>";
foreach($types as $rank){
$rankq = dbquery("SELECT * FROM fusionusers WHERE user_rank='".$rank."'");
$num = mysql_num_rows($rankq);
IF($num > 0){
$ranka = dbarray($rankq);
echo "<table><tr><td>Username</td><td>User's Rank</td></tr>";
foreach($ranka as $s){
echo "<tr>\n<td><a href=p
rofile.php?lookup=".$s['user_id'].">".$s['user_name']."</a></td>\n";
echo "<td>".$s['user_rank']."</td></tr>";
}
}
}
?>

When i load the rank.php page it only shows the first char of each result... :s

why is this?

CFMaBiSmAd
11-23-2007, 04:34 PM
My guess would be either that your database only contains the first letter or your dbarray() function is only returning the first letter.

What have you done to troubleshoot this, such as examining the data in the database directly to see what it actually contains?

If the data is correct in the database, then to get help with what your dbarray() function is doing incorrectly, you would need to post the code for it.

Jesuspwnt
11-23-2007, 11:07 PM
Db Query:

function dbquery($query) {
$result = @mysql_query($query);
if (!$result) {
echo mysql_error();
return false;
} else {
return $result;
}
}

Db Array:

function dbarray($query) {
$result = @mysql_fetch_assoc($query);
if (!$result) {
echo mysql_error();
return false;
} else {
return $result;
}
}

CFMaBiSmAd
11-23-2007, 11:48 PM
Your dbarray() is returning one row from the result set (not an array of row(s), which is what the foreach() loop is expecting.) The existing code is probably putting empty values in for $s['user_id'], $s['user_name'], and $s['user_rank'] and generating notice and warning messages (check your web server log for errors and/or turn on full php error reporting to get php help you.)

I am not sure what you intended the function code to do, return one row or return an array of all row(s) from the result set, so you will need to either modify it or the code that uses it to do what it was you were planning on having it do.

BTW. mysql_fetch_assoc() does not set mysql_error(), so echoing mysql_error() in the dbarray() function won't tell you anything. mysql_fetch_assoc() either returns the row it fetched from the result set or a FALSE value if there were no (more) rows to fetch.

Jesuspwnt
11-24-2007, 11:31 AM
ok, what i require the script to do is pull all of the users from the db with their ranks, id's and username's.

then display it in order of rank

eg:

Server Admin
Admin
etc
etc etc

like this:

<tr><td><a href=profile.php?lookiup=1>Jesuspwnt</a> | Server Admin</td></tr>

and continue as such.

oh and by the way, the database holds the FULL information

Inigoesdr
11-24-2007, 09:00 PM
SELECT * FROM `users` WHERE 1 ORDER BY `rank` ASC
Then use mysql_fetch_assoc() in a while() loop to retrieve & display the rows.

Jesuspwnt
11-25-2007, 12:10 AM
but i want to use a foreach!

please tell me why it won't work with a foreach

Inigoesdr
11-25-2007, 12:41 AM
Because it's not an array.

Jesuspwnt
11-27-2007, 07:55 PM
Well i changed it to Mysql_Fetch_Array(), and foreach() still doesn't work

Inigoesdr
11-27-2007, 07:58 PM
Show the code you're trying to use, and print_r() the result.

Jesuspwnt
11-27-2007, 08:02 PM
code:


$types = array("Gu"=>"Guest", "Ga"=>"Gamer", "D"=>"Donor", "RO"=>"Recruiting Officer", "TO"=>"Training Officer", "RHM"=>"Right Hand Man", "CO"=>"Commanding Officer", "A"=>"Admin", "SA"=>"Server Admin");
echo "<table><tr><td>Username</td><td>User's Rank</td></tr>";
foreach($types as $rank){
$rankq = dbquery("SELECT * FROM fusionusers WHERE user_rank='".$rank."'");
$num = mysql_num_rows($rankq);
IF($num > 0){
$ranka = dbarray($rankq);
echo "<table><tr><td>Username</td><td>User's Rank</td></tr>";
foreach($ranka as $s){
echo "<tr>\n<td><a href=profile.php?lookup=".$s['user_id'].">".$s['user_name']."</a></td>\n";
echo "<td>".$s['user_rank']."</td></tr>";
}
}
}

i used print_r() and the same thing was shown when i was using echo

CFMaBiSmAd
11-27-2007, 08:19 PM
If you actually read the php manual for the mysql_fetch_xxxxxx() functions, you will learn that each call to the function only returns one row from the result set. You can also learn that same information from the examples in the php manual for the mysql_fetch_xxxxx() functions you are attempting to use, where they use a where() loop to iterate over all the rows in the result set.

You were also previously told in this thread what the code was doing that was the problem -

Your dbarray() is returning one row from the result set (not an array of row(s), which is what the foreach() loop is expecting.)

Jesuspwnt
11-27-2007, 08:31 PM
but the problem with a while function is that if you're using ID's like in my case primary int keys, and one of those is deleted, then your script is kinda screwed.
Also a fetch_array function returns an array of results. i've used it many times before.

arnyinc
11-27-2007, 08:43 PM
but the problem with a while function is that if you're using ID's like in my case primary int keys, and one of those is deleted, then your script is kinda screwed.

I don't follow that at all. It will just print the records are there. It doesn't matter if you delete one.

Jesuspwnt
11-27-2007, 09:54 PM
yes it will, first you count how many results there are

eg.
$num = mysql_num_rows(mysql_query("SELECT * FROM users"));
$i = 1;
WHILE($i <= $num){
//do stuff here
$i++;
}


that will count a missing result as one there!
so say if we have 5 results and 2 were deleted so there WERE 7, and say number 2 and 3 were missing, it would stop at 5 instead of 7, since the actual information in the sql could be

id name
1 test
3 test3
4 test4
6 test6
7 test7

it woould stop at 5 not 7!!

Jesuspwnt
11-27-2007, 10:17 PM
yes it will, first you count how many results there are

eg.
$num = mysql_num_rows(mysql_query("SELECT * FROM users"));
$i = 1;
WHILE($i <= $num){
//do stuff here
$i++;
}


that will count a missing result as one there!
so say if we have 5 results and 2 were deleted so there WERE 7, and say number 2 and 3 were missing, it would stop at 5 instead of 7, since the actual information in the sql could be

id name
1 test
3 test3
4 test4
6 test6
7 test7

it woould stop at 5 not 7!!

CFMaBiSmAd
11-27-2007, 10:24 PM
Jesuspwnt, your understanding of how databases and your code work is not correct and three different people in this thread have pointed that out.

If the code in your last post functioned the way you described, then something inside of the while loop was responsible for those results, because the while loop itself does work for all the rows retrieved in a result set.

If the mysql_fetch_xxxxx() functions returned an array with all the rows in a result set (at one time) as you are maintaining, then your dbarray() function and the rest of your code would work, but they clearly do not, otherwise you would not have needed to post in a forum.

Heed the advice given so far. Read the examples in the php manual for the functions you are using.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum