1

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 );
Smart Singam
  • 103
  • 1
  • 9
  • What's a "datable"? Even the tag on the question says this is "ambiguous" as the term is used for different components in different languages. You need to be more specific. – Neil Lunn Jun 14 '17 at 13:22

1 Answers1

1

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

  • if you have any refer code with using mongodb with datatable – Smart Singam Jun 15 '17 at 06:11
  • i have one doubt see down my qustion..if u know answer pls tell me immediately this is my qustion ->if any one to convert this mysql code to mongodb do...i need to write this code in mongodb with php.. – Smart Singam Jun 18 '17 at 14:03
  • 1
    I gave you an answer to your original question, read the answer and adapt your original code properly, it will be better for you and faster than rewriting the other code from sql to mongo. – Manuel Perez Gomez Jun 20 '17 at 06:13