例如
public $inputs=array(
array( 'sysname'=>'pt_name','dbname' => 'users.name','label' => 'user (name/ID)','value' => '',
'type' => 'text','rules' => 'required','attr'=>'class="autocomplete"'),
array( 'sysname'=>'pt_dob','dbname' => 'users.dob','label' => 'Patient Dob','value' => '',
'type' => 'text','rules' => 'required','attr'=>'class="dob ac" Disabled'),
array( 'sysname'=>'pt_gender','dbname' => 'users.gender','label' => 'gender','value' => 'male,female',
'type' => 'dropdown','rules' => 'required','attr'=>'class="ac" Disabled'),
array( 'sysname'=>'visit_date','dbname' => 'visits.date','label' => 'Date','value' => '',
'type' => 'text','rules' => 'required','attr'=>'class="datepicker"'),
array( 'sysname'=>'visit_time','dbname' => 'visits.time_booked','label' => 'Time','value' => '',
'type' => 'text','rules' => 'required','attr'=>'class="timepicker"'),
array( 'sysname'=>'visit_type','dbname' => 'visits.type','label' => 'Visit type','value' => 'visit,schedule',
'type' => 'dropdown','rules' => 'required','attr'=>'')
);
如何搜索此数组仅用于其sysname中包含pt_的数组?
这个想法是我在同一个表中有很多类型的行,所以不是运行mysql查询来分别获取每个类型的例子:
$pt=db->query("select * from table where sysname like 'pt_%'")->result();
$visit=db->query("select * from table where sysname like 'visit_%'")->result();
我想在一个地方获取所有内容并在php中拆分以减少数据库负载。
所以我该怎么做?
。将我的查询分开是值得的还是更好的。答案 0 :(得分:1)
执行几个查询很好,您的数据库可以轻松处理。如果您针对几十种类型(每种类型只有几行)进行了数十次查询,那么将该逻辑移植到PHP可能值得研究。
我建议将systype
放在一个单独的列中,其中包含an index。这将大大加快您的查询速度并加载您的数据库。更好的是,您可以将该列设为ENUM。
public $inputs=array(
array( 'systype'=>'pt', 'sysname'=>'pt_name','dbname' => 'users.name','label' => 'user (name/ID)','value' => '',
'type' => 'text','rules' => 'required','attr'=>'class="autocomplete"'),
...
$pt=db->query("select * from table where systype = 'pt'")->result();
$visit=db->query("select * from table where systype = 'visit'")->result();
答案 1 :(得分:1)
array_filter和PHP风格的closure *对此非常简单:
function buildFilter($key, $needle) {
return function($array) use($key, $needle) {
return (strpos($array[$key], $needle) !== FALSE);
};
}
$matches = array_filter($inputs, buildFilter('sysname', 'pt_'));
var_dump($matches);