我不确定是否可能,但我正在寻找一种方法来制作汽车"设置"对于mysql结果。
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);
}
也许有更好的方法来做所有$ site->设置?我的意思是我的代码看起来太大而且毫无意义。还有其他方法吗?
答案 0 :(得分:3)
如果您想在$site->set
$row
while ($row = mysql_fetch_array($result)) {
foreach($row as $key => $value) {
$site->set($key,$value);
}
}
答案 1 :(得分:1)
改变这个...... 假设您的site-> set函数将始终采用精确的表列名称。
while ($row = mysql_fetch_array($result)) {
foreach($row as $k=>$v){
$site->set($k,$v);
}
}
您还可以更改teh $ site->设置功能并在那里进行循环。然后就这样做......
while ($row = mysql_fetch_array($result)) {
$site->set($row);
}
和功能。只是一个大纲。不确定你在实际功能中发生了什么。但这只是一个想法
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;
}