Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Ranking System

  1. #1
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Ranking System

    Hey all, im creating a ranking system for my friends website, the main code for it is:
    PHP Code:
    <?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?

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Db Query:
    PHP Code:
    function dbquery($query) {
        
    $result = @mysql_query($query);
        if (!
    $result) {
            echo 
    mysql_error();
            return 
    false;
        } else {
            return 
    $result;
        }

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


  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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.
    Last edited by CFMaBiSmAd; 11-23-2007 at 11:51 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Code:
    SELECT * FROM `users` WHERE 1 ORDER BY `rank` ASC
    Then use mysql_fetch_assoc() in a while() loop to retrieve & display the rows.

  • #7
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    but i want to use a foreach!

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

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Because it's not an array.

  • #9
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Well i changed it to Mysql_Fetch_Array(), and foreach() still doesn't work
    Last edited by Jesuspwnt; 11-27-2007 at 08:03 PM.

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Show the code you're trying to use, and print_r() the result.

  • #11
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    code:

    PHP 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

  • #12
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,108
    Thanks
    2
    Thanked 326 Times in 318 Posts
    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.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #13
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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.

  • #14
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by Jesuspwnt View Post
    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.

  • #15
    New Coder
    Join Date
    Nov 2007
    Location
    Kent, United Kingdom
    Posts
    67
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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!!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •