Secho Error

Secho Error

WaynefWaynef Posts: 9Questions: 0Answers: 0
edited April 2014 in DataTables 1.9
http://debug.datatables.net/ehopuw

Hi I have been trying to integrate datables into my intranet site and i am having trouble with sEcho returning as 0, I am a complete novice with Jscript all my experience is with PHP any help would be greatly appreciated.

Thanks

Wayne

Replies

  • allanallan Posts: 54,914Questions: 1Answers: 8,608 Site admin
    Hi Wayne,

    Thanks for the debug trace. The server is not returning valid JSON. Have a look at this tech note to see how you can view the return data: http://next.datatables.net/manual/tech-notes/1 .

    You want it to return valid JSON, since that is what DataTables needs: http://datatables.net/usage/server-side

    Allan
  • WaynefWaynef Posts: 9Questions: 0Answers: 0
    Hi Allan

    Thanks for your response I have performed the check you suggest in the tech-note and here is the response.

    [code]
    <!DOCTYPE html>










    Notice: Undefined index: sEcho in C:\xampp\htdocs\Testing\TableTest.php on line 159

    {"sEcho":0,"iTotalRecords":"3","iTotalDisplayRecords":"3","aaData":[["1","Test"," 3 "],["2","Test"," 2 "],["3","TEST3"," 4 "]]}
    [/code]

    Could you think what may be causing the problem.

    I have cut down the number of entries in the table being connected too to see if the data in the database was the cause.

    thanks again

    Wayne
  • allanallan Posts: 54,914Questions: 1Answers: 8,608 Site admin
    Very much so. What is your PHP code trying to do with sEcho ?

    Allan
  • WaynefWaynef Posts: 9Questions: 0Answers: 0
    Here is both my HTML and PHP code I am using your PHP server side script.

    I am also now using the browser table created when i tried editor. to see if it is the table layout/data/

    [code]
    <!doctype html>




    example


    $(document).ready(function() {
    $('#example').dataTable(
    { "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "TableTest.php"
    });
    });

    TableTest.php
    Here's another basic page




    Rowid
    Customer
    QTY









    [/code]

    [code]
    <?php
    include 'dbcon.php';
    include 'site_settings.php';
    $databasetable = "ll_current_desp";

    /**
    * Script: DataTables server-side script for PHP 5.2+ and MySQL 4.1+
    * Notes: Based on a script by Allan Jardine that used the old PHP mysql_* functions.
    * Rewritten to use the newer object oriented mysqli extension.
    * Copyright: 2010 - Allan Jardine (original script)
    * 2012 - Kari Söderholm, aka Haprog (updates)
    * License: GPL v2 or BSD (3-point)
    */
    mb_internal_encoding('UTF-8');

    /**
    * Array of database columns which should be read and sent back to DataTables. Use a space where
    * you want to insert a non-database field (for example a counter or static image)
    */
    $aColumns = array( 'id', 'engine', 'version' );

    // Indexed column (used for fast and accurate table cardinality)
    $sIndexColumn = 'id';

    // DB table to use
    $sTable = 'browsers';

    // Database connection information
    $gaSql['user'] = $databaseusername;
    $gaSql['password'] = $databasepassword;
    $gaSql['db'] = 'tstdisplay';
    $gaSql['server'] = 'localhost';
    $gaSql['port'] = 3306; // 3306 is the default MySQL port

    // Input method (use $_GET, $_POST or $_REQUEST)
    $input =& $_GET;

    /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * If you just want to use the basic configuration for DataTables with PHP server-side, there is
    * no need to edit below this line
    */

    /**
    * Character set to use for the MySQL connection.
    * MySQL will return all strings in this charset to PHP (if the data is stored correctly in the database).
    */
    $gaSql['charset'] = 'utf8';

    /**
    * MySQL connection
    */
    $db = new mysqli($gaSql['server'], $gaSql['user'], $gaSql['password'], $gaSql['db'], $gaSql['port']);
    if (mysqli_connect_error()) {
    die( 'Error connecting to MySQL server (' . mysqli_connect_errno() .') '. mysqli_connect_error() );
    }

    if (!$db->set_charset($gaSql['charset'])) {
    die( 'Error loading character set "'.$gaSql['charset'].'": '.$db->error );
    }


    /**
    * Paging
    */
    $sLimit = "";
    if ( isset( $input['iDisplayStart'] ) && $input['iDisplayLength'] != '-1' ) {
    $sLimit = " LIMIT ".intval( $input['iDisplayStart'] ).", ".intval( $input['iDisplayLength'] );
    }


    /**
    * Ordering
    */
    $aOrderingRules = array();
    if ( isset( $input['iSortCol_0'] ) ) {
    $iSortingCols = intval( $input['iSortingCols'] );
    for ( $i=0 ; $i<$iSortingCols ; $i++ ) {
    if ( $input[ 'bSortable_'.intval($input['iSortCol_'.$i]) ] == 'true' ) {
    $aOrderingRules[] =
    "`".$aColumns[ intval( $input['iSortCol_'.$i] ) ]."` "
    .($input['sSortDir_'.$i]==='asc' ? 'asc' : 'desc');
    }
    }
    }

    if (!empty($aOrderingRules)) {
    $sOrder = " ORDER BY ".implode(", ", $aOrderingRules);
    } else {
    $sOrder = "";
    }


    /**
    * 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 tables, and MySQL's regex functionality is very limited
    */
    $iColumnCount = count($aColumns);

    if ( isset($input['sSearch']) && $input['sSearch'] != "" ) {
    $aFilteringRules = array();
    for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
    if ( isset($input['bSearchable_'.$i]) && $input['bSearchable_'.$i] == 'true' ) {
    $aFilteringRules[] = "`".$aColumns[$i]."` LIKE '%".$db->real_escape_string( $input['sSearch'] )."%'";
    }
    }
    if (!empty($aFilteringRules)) {
    $aFilteringRules = array('('.implode(" OR ", $aFilteringRules).')');
    }
    }

    // Individual column filtering
    for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
    if ( isset($input['bSearchable_'.$i]) && $input['bSearchable_'.$i] == 'true' && $input['sSearch_'.$i] != '' ) {
    $aFilteringRules[] = "`".$aColumns[$i]."` LIKE '%".$db->real_escape_string($input['sSearch_'.$i])."%'";
    }
    }

    if (!empty($aFilteringRules)) {
    $sWhere = " WHERE ".implode(" AND ", $aFilteringRules);
    } else {
    $sWhere = "";
    }


    /**
    * SQL queries
    * Get data to display
    */
    $aQueryColumns = array();
    foreach ($aColumns as $col) {
    if ($col != ' ') {
    $aQueryColumns[] = $col;
    }
    }

    $sQuery = "
    SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", $aQueryColumns)."`
    FROM `".$sTable."`".$sWhere.$sOrder.$sLimit;

    $rResult = $db->query( $sQuery ) or die($db->error);

    // Data set length after filtering
    $sQuery = "SELECT FOUND_ROWS()";
    $rResultFilterTotal = $db->query( $sQuery ) or die($db->error);
    list($iFilteredTotal) = $rResultFilterTotal->fetch_row();

    // Total data set length
    $sQuery = "SELECT COUNT(`".$sIndexColumn."`) FROM `".$sTable."`";
    $rResultTotal = $db->query( $sQuery ) or die($db->error);
    list($iTotal) = $rResultTotal->fetch_row();


    /**
    * Output
    */
    $output = array(
    "sEcho" => intval($input['sEcho']),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array(),
    );

    while ( $aRow = $rResult->fetch_assoc() ) {
    $row = array();
    for ( $i=0 ; $i<$iColumnCount ; $i++ ) {
    if ( $aColumns[$i] == 'version' ) {
    // Special output formatting for 'version' column
    $row[] = ($aRow[ $aColumns[$i] ]=='0') ? '-' : $aRow[ $aColumns[$i] ];
    } elseif ( $aColumns[$i] != ' ' ) {
    // General output
    $row[] = $aRow[ $aColumns[$i] ];
    }
    }
    $output['aaData'][] = $row;
    }

    echo json_encode( $output );

    ?>

    [/code]
  • WaynefWaynef Posts: 9Questions: 0Answers: 0
    I have now managed to get it working it looks like i had HTML is some of the Included PHP which was being added to the JSON data
  • allanallan Posts: 54,914Questions: 1Answers: 8,608 Site admin
    Good to hear. Thanks for letting us know.

    Allan
  • WaynefWaynef Posts: 9Questions: 0Answers: 0
    Hi

    I went over my php code again and noticed that I have hard coded a value for sEcho this has allowed the retrieval of the data I'm sure there is a reason that it does not work with a variable but at the moment i can't see why.

    [code]
    $output = array(
    "sEcho" => 1,
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array(),

    [/code]
  • allanallan Posts: 54,914Questions: 1Answers: 8,608 Site admin
    Not sure either, I'd need to debug the script I suspect to understand. sEcho as a static value won't work though. It will draw the first response from the server, but not the subsequent ones.

    Allan
This discussion has been closed.