PHP & MySQL Position

Discussion in 'PHP' started by AT-XE, Nov 30, 2008.

  1. #1
    Greeting everyone,
    I have a little php problem here, I have a query like this:

    $qu = mysql_query("SELECT * FROM table_name ORDER BY rank DESC;");
    $qu = mysql_fetch_array($qu);
    
    PHP:
    So I actually fetch the result of the query, now let's say I want to add medals or certificates to the top 3 people, how can I do it?

    Thanks in advance,
    -AT-XE

    P.S: It would be really helpful if I could get the result like this:

    
    if($me['id'] == $qu['id'][1]) {
    echo gold();
    elseif($me['id'] == $qu['id'][2]) {
    echo silver();
    elseif($me['id'] == $qu['id'][3]) {
    echo bronze();
    }
    PHP:
     
    AT-XE, Nov 30, 2008 IP
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    try this
    
    $qu = mysql_query("SELECT * FROM table_name ORDER BY rank DESC [B]LIMIT 3[/B];") or die(mysql_error()); //show only 3 member
    
    //create medal array
    $medal = array('gold', 'silver', 'bronze');
    
    //create variable like pointer
    $i = 0;
    while ($row = mysql_fetch_array($qu))
    {
    	echo $medal[$i].'<br />'.;
    	$i++;
    }
    PHP:
     
    misbah, Nov 30, 2008 IP
  3. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply misbah, but the point is that I want to check if the user I am logged in with has won any medals, the id of the user can be found with the variable $me['id']. If the user has won any medals then other functions (gold(), silver() and bronze()) will print the medal images :)

    Thanks anyways.
     
    AT-XE, Dec 1, 2008 IP
  4. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #4
    look at this
    $qu = mysql_query("SELECT * FROM table_name ORDER BY rank DESC;");
    PHP:
    did you mean this is rank table?
    can you give me detail the field this table...
    and variable $me['id'], this is variable get from database too?
     
    misbah, Dec 1, 2008 IP
  5. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually the site is a game and the table is the users table. There are awards for more than one thing, for example, for the most violent player (the player with the most attacks sent), the query here would select the top 3 most violent players:

    $attout_result = mysql_query("SELECT player,attacksout FROM table_name ORDER BY attout DESC LIMIT 3");
    PHP:
    I have let's say a seperate page called "My Awards" that will show if I (a player) have any awards, we can have my player id with the variables $id or $me['id'].

    So I want to check if I have got any ranks which will give me medals, ex gold silver and bronze. So we need some code to check if I am in the top 3 and if I am, it will print an image depending on my position (1st - Gold, 2nd - Silver, 3rd - Bronze), there images are output by the functions gold(), silver() and bronze(), it should call those functions only if I won a medal.

    So we need a way to check in which position I am (ex. If I am 3rd) and output the medal using the functions above (ex. If I am 3rd I should get bronze).

    The player id in the users table can be found in the field id, the player name under the player field.
     
    AT-XE, Dec 2, 2008 IP
  6. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #6
    if table_name = users table...
    you can do this
    $attout_result = mysql_query("SELECT id,player,attacksout FROM users ORDER BY attout DESC LIMIT 3");
    
    $position = array();
    while ($row = mysql_fetch_array($attout_result))
    {
        $position[] = $row['id'];
    }
    switch ($me['id'])
    {
        case $position[0]:
            gold();
            break;
        case $position[1]:
            silver();
            break;
        case $position[2]:
            bronze();
            break;
    }
    PHP:
     
    misbah, Dec 2, 2008 IP
    AT-XE likes this.
  7. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Great, thank you very much!
    +Rep Added!
     
    AT-XE, Dec 4, 2008 IP