phpmongodbdatatableserver-side-scripting

PHP with mongodb datatable error


I Got error in the datatable in mongodb please give solution... If Anyone have reference source code send me it will helpful for me....................................................................................................................................................................................

Notice: Undefined index: iColumns in D:\xampp\htdocs\mongo\test3.php on line 47
Notice: Undefined index: sEcho in D:\xampp\htdocs\mongo\test3.php on line 129

 {"sEcho":0,"iTotalRecords":9,"iTotalDisplayRecords":9,"aaData":[{"_id":

 {"$id":"5936e5e783b236680c00002a"},"name":"Mitra","age":21,"gender":"M","course":"BTECH","marks":77}

Source Code:

    <?php

mb_internal_encoding('UTF-8');

$database   = 'university';
$collection = 'students';

/**
 * MongoDB connection
 */
try{
        // Connecting to server
        $m = new MongoClient(  );
    }catch(MongoConnectionException $connectionException){
        print $connectionException;
        exit;
    }

$m_collection = $m->$database->$collection;

/**


    * Define the document fields to return to DataTables (as in http://us.php.net/manual/en/mongocollection.find.php).
 * If empty, the whole document will be returned.
 */
$fields = array();

// Input method (use $_GET, $_POST or $_REQUEST)
$input = & $_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];
    }
}

/**
 * Filtering
 * NOTE this does not match the built-in DataTables filtering which does it
 * word by word on any field. It's possible to do here, but concerned about efficiency
 * on very large collections.
 */
$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;
}

$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);
}

/**
 * Output
 */
$output = array(
    "sEcho" => intval($input['sEcho']),
    "iTotalRecords" => $m_collection->count(),
    "iTotalDisplayRecords" => $cursor->count(),
    "aaData" => array(),
);

foreach ( $cursor as $doc ) {
    $output['aaData'][] = $doc;
}

echo json_encode( $output );

Solution

  • Those are not errors, are Notices:

    $iColumns = $input['iColumns'];
    

    $input has no key named iColumns.

    "sEcho" => intval($input['sEcho']),
    

    Same thing here: $input has no key named sEcho

    Make sure that $_REQUEST contains both keys. Or you could disable Error Reporting for notices.

    EDIT: You use: $input = & $_REQUEST; you should provably use $_GET or $_POST depending how you send the date to the server and ensure iColumns and sEcho are sent. If your frontend has a form and you are posting the data, you could validate the input fields so those two variables are required OR give them a default value.

    If you want to default it (for example) to 10:

    $iColumns = isset($input['iColumns']) ? isset($input['iColumns']) : 10;
    

    and (not sure what sEcho index is used for:

    $sEcho = isset($input['sEcho'] ? intval($input['sEcho']) : 10;
    $output = array(
        "sEcho" => $sEcho,
        "iTotalRecords" => $m_collection->count(),
        "iTotalDisplayRecords" => $cursor->count(),
        "aaData" => array(),
    );
    

    That should not give you any notices.

    Other Option, disable reporting of notices:

    [root@server ]$ vi /etc/php.ini
    error_reporting = E_ALL & ~E_NOTICE
    

    Or, in the top of your php file: error_reporting(E_ALL & ~E_NOTICE);