Page 2 of 2

Re: Array problems in PHP 4...

Posted: Mon Aug 17, 2009 1:04 pm
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?

Re: Array problems in PHP 4...

Posted: Mon Aug 17, 2009 1:38 pm
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;
}

Re: Array problems in PHP 4...

Posted: Mon Aug 17, 2009 2:34 pm
by Wolf_22
Yep, you're right red. That got it! Thanks! :P