-1

I want to show data from database using datatable plugin, but the problem is, it shows all the data in one page and still says showing 1 of 10 and the pagination button appears but won't work.

enter image description here

enter image description here

jquery:

<script>
    $(document).ready(function() {
    $('#leads').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax":{
            url :"includes/get_data_script_test.php", // json datasource
            type: "post",  // type of method  , by default would be get
          }
    } );
} );
</script>

get_data_script_test.php:

<?php

/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */

// DB table to use
$table = 'lead';

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes

$columns = array(
    array( 'db' => 'name', 'dt' => 0 ),  
);

// SQL server connection information
$sql_details = array(
    'user' => '**********',
    'pass' => '***********',
    'db'   => '**********',
    'host' => '**********'
);


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

require( 'ssp_class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
?>
Zoe
  • 27,060
  • 21
  • 118
  • 148
Jayprakash
  • 29
  • 1
  • 9
  • can you post you html? – inye Jul 09 '17 at 01:19
  • 10 rows per page, 699 rows all together, 70 pages, the math works unless you do not actually have that many rows. Your php should be setting the properties to tell the client how many rows are in the datatbase. – Bindrid Jul 09 '17 at 01:27

1 Answers1

0

To have the pagination work you need to have it setup in jquery script. You would need something like this:

<script>
    $(document).ready(function() {
    $('#leads').DataTable( {
        "lengthMenu": [[10,15,20,25,-1], [10,15,20,25,'All']], //This line needs added so that it knows what to set the page lengths to
        "processing": true,
        "serverSide": true,
        "ajax":{
            url :"includes/get_data_script_test.php", // json datasource
            type: "post",  // type of method  , by default would be get
          }
    } );
} );
</script>

Without the lengthMenu DataTables won't know what to set the page lengths to and will only show all data.

Mike
  • 1,853
  • 3
  • 45
  • 75
  • 1
    @Jayprakash I'm glad to hear that this is already solved. Could you post an answer with how you were able to solve it? This can help others who have a similar problem. – Mike Aug 03 '17 at 18:47