|
Posted by josh.23.french on 10/22/06 01:20
Here's the code i have:
$db = array(); //main array
$db['main'] = array(); //table `main`
$db['main'][] = array('id'=>0,
'username'=>'joshfrench','userpass'=>'password','userlevel'=>'admin');
//row
$db['main'][] = array('id'=>1,
'username'=>'bob_smith','userpass'=>'psswrd','userlevel'=>'user');
//row
function trim_value(&$value){
$value = strtolower(trim($value));
}
function fdb_select($fields = "*", $table, $where = false, $limit =
false){
global $db;
$retv = $db[$table];
//LIMIT RESULTS TO FIELDS
if(strrpos($fields, ",") === false){$field=array($fields);}else{$field
= explode(",", $fields);};
array_walk($field, "trim_value");
$all_fields = array_keys($retv[0]);
if($fields == '*'|| is_null($fields)){$field = $all_fields;};
$field = array_uintersect($all_fields, $field, "strcasecmp");
$retvi = array();
foreach($retv as $num => $row){
foreach($row as $fname => $fvalue){
$fname1 = strtolower($fname);
if(in_array($fname1, $field)){
$retvi[$num][$fname]=$fvalue;
};
};
};
//DONE LIMITING TO FIELDS
if($where != false){
//UP TO HERE IT WORKS FINE
//LIMIT TO WHERE CLAUSE
$retvj = array();
foreach($retvi as $num => $row){
//IN THE ROW
foreach($where as $k => $v){
//IF ROW MEETS ALL REQUIREMENTS
if($row[$k] == $v){
$retvj[] = $retvi[$num]; //Keep that row
};
};
};
//DONE LIMITING TO WHERE CLAUSE
};
$retvi = (isset($retvj)) ? $retvj:$retvi;
//SEND A FINAL ASSOCIATIVE ARRAY
return $retvi;
};
print_r(fdb_select("*", "main", array('id'=>"1",
"username"=>"joshfrench")));
the previous code outputs this:
Array
(
[0] => Array
(
[id] => 0
[username] => joshfrench
[userpass] => password
[userlevel] => admin
)
[1] => Array
(
[id] => 1
[username] => bob_smith
[userpass] => psswrd
[userlevel] => user
)
)
if i make the last option array('id'=>'1'), it does what i want.
HELP!!
Navigation:
[Reply to this message]
|