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();正常工作 –