Array problems in PHP 4...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

Okay, it looks like I have it now. The query needed the RLIKE clause -

Code: Select all

$query = "SELECT id, user_login FROM $vtldb->users INNER JOIN $vtldb->usermeta ON $vtldb->users.id = $vtldb->usermeta.user_id WHERE $vtldb->usermeta.meta_value RLIKE 'administrator' ORDER BY id ASC LIMIT 10000";
Now all I have is 1 more little issue and it will finally be complete. I'm having problems getting the correct values from the arr_combine function (all because of PHP 4! arg!). The query has everything I need in it, but my understanding of array manipulation is what's coming into play now--but I'm getting there! Regardless, the returned query gives me the following:

Code: Select all

Array ( [0] => stdClass Object ( [id] => 1 [user_login] => Walter ) [1] => stdClass Object ( [id] => 2 [user_login] => beckka ) [2] => stdClass Object ( [id] => 4 [user_login] => test2 ) )
Those users above are the only ones with administrative privileges. The ids are even accurate! The only thing I need to do now is make an array where the keys are those ids and the values are the usernames. Below is everything I'm using to achieve this to no avail as the array is not accurate:

Code: Select all

function arr_combine($arr1,$arr2){//users then ids
                if(!is_array($arr1))
                        $arr1 = array();
                if(!is_array($arr2))
                        $arr2 = array();
 
                $keys1 = array_keys($arr1);
                $keys2 = array_keys($arr2);
                $vals1 = array_values($arr1);//store all admin login names... WORKS.
                $vals2 = array_values($arr2);//store all id numbers... WORKS.
          $keys  = array_merge($keys1,$keys2);
                $vals  = array_merge($vals1,$vals2);
                
          $ret    = array();
 
                foreach($keys as $key){
                        list($unused,$val) = each($vals);
                        $ret[$key] = $val;
                }
 
            return $ret;
        } 
 
function get_vtl_user_roles(){
    global $vtl_roles,$vtldb;
    $this_role = "'[[:<:]]administrator[[:>:]]'";
    $query = "SELECT id, user_login FROM $vtldb->users INNER JOIN $vtldb->usermeta ON $vtldb->users.id = $vtldb->usermeta.user_id WHERE $vtldb->usermeta.meta_value RLIKE 'administrator' ORDER BY id ASC LIMIT 10000";
    $result = $vtldb->get_results($query);
    print_r($result);
    foreach ($result as $id=>$user) {
        $partA[] = $user->user_login;
        $partB[] = $user->id;
    }
 
    $users_of_this_role = arr_combine($partA,$partB);
    return $users_of_this_role;
}
 
Any ideas?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: Array problems in PHP 4...

Post by redmonkey »

Perhaps I'm missing something important or have misread it but can't you just do something along the lines of....

Code: Select all

function get_vtl_user_roles(){
    global $vtl_roles,$vtldb;
    $this_role = "'[[:<:]]administrator[[:>:]]'";
    $query = "SELECT id, user_login FROM $vtldb->users INNER JOIN $vtldb->usermeta ON $vtldb->users.id = $vtldb->usermeta.user_id WHERE $vtldb->usermeta.meta_value RLIKE 'administrator' ORDER BY id ASC LIMIT 10000";
    $result = $vtldb->get_results($query);
 
    /*
     * old source....
     * foreach ($result as $id=>$user) {
     *    $partA[] = $user->user_login;
     *    $partB[] = $user->id;
     * }
     */
 
     /* new source... */
 
    /* initialise array so we always return something */
    /* even if it's an empty array                    */
    $users_of_this_role = array();
 
    foreach ($result as $key => $user)
    {
        $users_of_this_role[$user->id] = $user->user_login;
    }
 
    /* now longer required..
     * $users_of_this_role = arr_combine($partA,$partB);
     */
 
    return $users_of_this_role;
}
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

Yep, you're right red. That got it! Thanks! :P
Post Reply