2
在此代码过滤器,搜索和分页不起作用我已经尝试过自己,但它没有工作,所以请检查代码给我任何解决方案或任何有关此查询的参考。甚至没有任何与此相关的代码文件或文档这将有助于我追求的理念数据表过滤器,搜索未运行mongodb php
<?php
mb_internal_encoding('UTF-8');
$database = 'test';
$collection = 'user';
/**
* MongoDB connection
*/
try{
// Connecting to server
$m = new MongoClient();
}catch(MongoConnectionException $connectionException){
print $connectionException;
exit;
}
$m_collection = $m->$database->$collection;
$input = $fields = $totalRecords = $data = array();
$input = & $_REQUEST;
$fields = array('id', 'name', 'email', 'gender,');
// Input method (use $_GET, $_POST or $_REQUEST)
/**
* Handle requested DataProps
*/
// Number of columns being displayed (useful for getting individual column search info)
$iColumns = & $input['iColumns'];
// Get mDataProp values assigned for each table column
$dataProps = array();
for ($i = 0; $i < $iColumns; $i++) {
$var = 'mDataProp_'.$i;
if (!empty($input[$var]) && $input[$var] != 'null') {
$dataProps[$i] = $input[$var];
}
}
$searchTermsAny = array();
$searchTermsAll = array();
if (!empty($input['sSearch'])) {
$sSearch = $input['sSearch'];
for ($i=0 ; $i < $iColumns ; $i++) {
if ($input['bSearchable_'.$i] == 'true') {
if ($input['bRegex'] == 'true') {
$sRegex = str_replace('/', '\/', $sSearch);
} else {
$sRegex = preg_quote($sSearch, '/');
}
$searchTermsAny[] = array(
$dataProps[$i] => new MongoRegex('/'.$sRegex.'/i')
);
}
}
}
// Individual column filtering
for ($i=0 ; $i < $iColumns ; $i++) {
if ($input['bSearchable_'.$i] == 'true' && $input['sSearch_'.$i] != '') {
if ($input['bRegex_'.$i] == 'true') {
$sRegex = str_replace('/', '\/', $input['sSearch_'.$i]);
} else {
$sRegex = preg_quote($input['sSearch_'.$i], '/');
}
$searchTermsAll[ $dataProps[$i] ] = new MongoRegex('/'.$sRegex.'/i');
}
}
$searchTerms = $searchTermsAll;
if (!empty($searchTermsAny)) {
$searchTerms['$or'] = $searchTermsAny;
}
$totalRecords =$m_collection->count();
$cursor = $m_collection->find($searchTerms, $fields);
/**
* Paging
*/
if (isset($input['iDisplayStart']) && $input['iDisplayLength'] != '-1') {
$cursor->limit($input['iDisplayLength'])->skip($input['iDisplayStart']);
}
/**
* Ordering
*/
if (isset($input['iSortCol_0'])) {
$sort_fields = array();
for ($i=0 ; $i<intval($input['iSortingCols']) ; $i++) {
if ($input[ 'bSortable_'.intval($input['iSortCol_'.$i]) ] == 'true') {
$field = $dataProps[ intval($input['iSortCol_'.$i]) ];
$order = ($input['sSortDir_'.$i]=='desc' ? -1 : 1);
$sort_fields[$field] = $order;
}
}
$cursor->sort($sort_fields);
}
foreach ($cursor as $doc)
{ $name = '<a href="profile.php?secure='.$doc['_id'].' " style = "color:red;">'.$doc['name'].'</a>';
$data[] = array($name, $doc['email'], $doc['gender]);
}
/**
* Output
*/
$json_data = array(
"draw"=> intval($input['draw']),
"recordsTotal" =>intval ($totalRecords),
"recordsFiltered" => intval($totalRecords),
"data" => $data
);
echo json_encode($json_data);
And also i need to Join two tables as given below.
表1
表2
谢谢您的回答立即搜索工作正常,但我不能让显示条目和下一个按钮在那里收集12文件,但它可以在数据表中显示(**显示1到1的1 ** **),如果我可以设置25它可以显示12doc ...可以帮助我得到显示条目和下一个按钮.. –
这两行'$ resultSet = $收藏 - >找到(); $ iTotal = count($ resultSet);'是为了维持整个分页 –
现在它可以正常工作....谢谢#mayank我可以修改这个代码像$ resultSet = $ collection-> find(); $ iTotal = $ resultSet-> count();正常工作 –