whatsmyip
2017-12-21 23:19:12 +08:00
你自己已经回答了。key, value 对
因为数据量不多,加上每个页面基本都需要,所以每次操作都是同时取出
select key, value from option;
修改的话只能是一个一个修改了。
之前写过的代码
```
class OptionManager
{
/*
* do add option
*/
public static function addOption($option)
{
$key = $option->get('key');
$value = $option->get('value');
$remark = $option->get('remark');
$key_values = array( 'key' => '?', 'value' => '?', 'remark' => '?' );
$builder = new SQLBuilder();
$builder->insert('cr_option', $key_values);
$sql = $builder->build();
$params = array( $key, $value, $remark);
$count = (new MysqlPDO())->execute($sql, $params);
return $count==1;
}
/*
*/
public static function getOptions($rule)
{
$selected_rows = array('key', 'value', 'remark');
$where_arr = array();
$builder = new SQLBuilder();
$builder->select('cr_option', $selected_rows);
$builder->where($where_arr);
$sql = $builder->build();
$params = array();
$options = (new MysqlPDO())->executeQuery($sql, $params);
if($rule->get('result_form')=='assoc'){
$options = self::list2kv($options);
}
return $options;
}
/*
* convert list to key-value
*/
private static function list2kv($options)
{
$options_kv = array();
foreach($options as $option){
$options_kv[$option['key']] = $option['value'];
}
return $options_kv;
}
/*
*/
public static function removeOption($option)
{
$key = $option->get('key');
$where_arr = array('key' => '?');
$builder = new SQLBuilder();
$builder->delete('cr_option');
$builder->where($where_arr);
$sql = $builder->build();
$params = array( $key );
$count = (new MysqlPDO())->execute($sql, $params);
return $count==1;
}
/*
*/
public function updateOption($option)
{
$key = $option->get('key');
$value = $option->get('value');
$remark = $option->get('remark');
$new_key_values = array( 'value' => '?', 'remark' => '?' );
$where_arr = array( 'key'=>'?' );
$builder = new SQLBuilder();
$builder->update('cr_option', $new_key_values);
$builder->where($where_arr);
$sql = $builder->build();
$params = array($value, $remark, $key);
$cnt = (new MysqlPDO())->execute($sql, $params);
return $cnt==1;
}
}
```