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.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    php variable array

    How can i do something like this:

    $ranks[$rank]

    I have a populated $ranks array that looks like this:

    PHP Code:
    $ranks = array(
    'Recruit'            =>    '1',
    'Private'            =>     '2',
    'Private 1st Class'        =>    '3',
    'Gunner Silver'            =>    '4',
    'Gunner Gold'            =>    '5',
    'Corporal Silver'        =>    '6',
    'Corporal Gold'            =>    '7',
    'Lance Corporal Silver'        =>    '8',
    'Lance Corporal Gold'        =>    '9',
    'Master Corporal Silver'    =>    '10',
    'Master Corporal Gold'        =>    '11',
    'Sergeant Silver'        =>    '12'
    ); 
    Then $rank is supposed to be one of the values on the left hand side of the above array. I want the number attributed to the text returned in $rank. Any ideas?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,044
    Thanks
    10
    Thanked 93 Times in 91 Posts
    PHP Code:
    <?
    $rank 
    'Recruit';
    echo 
    $ranks[$rank];
    ?>
    are you saying that the above does not work ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    Im thinking the issue is the way i included it into my script. Here is the line im getting the error on:

    PHP Code:
    $vbulletin->db->query("INSERT INTO assimxnamerank (pid,
    name, rank) VALUES ("
    .$value.",".$nickname.",".$ranks[$rank].")"); 
    Parse error: parse error, unexpected T_STRING
    and here are the surrounding lines:

    PHP Code:
    $num_rows mysql_num_rows($result);
            if (
    $num_rows == 0)
            {
            
    $vbulletin->db->query("INSERT INTO assimxnamerank (pid,
    name, rank) VALUES ("
    .$value.",".$nickname.",".$ranks[$rank].")");
            }
            else
            {
                
    $vbulletin->db->query("UPDATE assimxnamerank SET
    `name`='"
    .$nickname."',`rank`='".$ranks[$rank]."' WHERE
    `pid`='"
    .$value."'");    
                
            } 

    *EDIT*
    When I change

    PHP Code:
    $vbulletin->db->query("INSERT INTO assimxnamerank (pid,
    name, rank) VALUES ("
    .$value.",".$nickname.",".$ranks[$rank].")"); 
    to
    PHP Code:
     echo $ranks[$rank]; 
    I get the same error, so the error has to be on the lines above it, but I dont see it.
    Last edited by thesavior; 01-01-2007 at 04:21 AM.

  • #4
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,092
    Thanks
    2
    Thanked 23 Times in 23 Posts
    I'm not in a position to test it right now, but I think the following will work:

    Code:
    $vbulletin->db->query("INSERT INTO assimxnamerank 
    (pid, name, rank) 
    VALUES (\"$value\",\"$nickname\",\"$ranks[$rank]\")");
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #5
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    thanks alot, that did it.


  •  

    Posting Permissions

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