I'm not sure if it is possible or not but what I am looking for a way making auto "set" for mysql results.
function test(){
$pDatabase = Database::getInstance();
$site = new Template("sites.tpl");
$query = 'SELECT * FROM sites';
$result = $pDatabase->query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_array($result)) {
$site->set("id",$row['id']);
$site->set("category",$row['category']);
$site->set("name",$row['name']);
$site->set("html",$row['html']);
$site->set("css",$row['css']);
$site->set("js",$row['js']);
$site->set("php",$row['php']);
$site->set("details",$row['details']);
$site->set("link",$row['link']);
}
mysql_free_result($result);
}
Maybe there is a better way doing all that $site->set ? I mean my code looks way too big and pointless. Any other ways ?
If you want to call $site->set
on all key/value pairs in $row
while ($row = mysql_fetch_array($result)) {
foreach($row as $key => $value) {
$site->set($key,$value);
}
}
Change to this... Assuming your site->set function will always take the exact table column names.
while ($row = mysql_fetch_array($result)) {
foreach($row as $k=>$v){
$site->set($k,$v);
}
}
You could also alter teh $site->set function and do the loop there. And then just do this...
while ($row = mysql_fetch_array($result)) {
$site->set($row);
}
And the function. Just an outline. Not really sure what you have going on in the actual function. But this is just an idea
function set($arrorkey, $value=null){
// If passed value is an array, do this...
if(is_array($arrorkey)){
foreach($arrorkey as $k=>$v){
$_SESSION[$k] = $v;
//Or database binding or whatever you're actually doing inside here
}
} else {
// If passed value, is a column, and value pair...do this.
$_SESSION[$arrorkey]=$value;
//Or database binding or whatever you're actually doing inside here
// This is just an example
}
return;
}