No data available in table

No data available in table

BaranekBaranek Posts: 3Questions: 2Answers: 0

I try load data in to table by PHP - MySQL external file:

<?php

// datatbáza premenné
    $sql_server="***"; 
    $uzivatel="***"; 
    $heslo="***"; 
    $nazov_databazy="***";
    $nazov_tabulky="***";
    
    $con = mysql_connect($sql_server,$uzivatel,$heslo);
    $dbs = mysql_select_db($nazov_databazy, $con);
    
    $result = mysql_query("SELECT zak_id, zak_nazov, zak_ulica FROM zakaznici"); 

    $json_array = array();
    while ($row = mysql_fetch_assoc ($result))
        {
            $json_array[] = $row;
        }

  echo json_encode($json_array);


<?php
>
```
?>


It give data in to console as (its trying datas):

> [{"zak_id":"1","zak_nazov":"Baranek s.r.o.","zak_ulica":"Lomnick\u00e1 6"},{"zak_id":"2","zak_nazov":"sdasdasdsada","zak_ulica":"asdasdsad 4"},{"zak_id":"3","zak_nazov":"sdffsdf","zak_ulica":"sdfdsfsf"},{"zak_id":"4","zak_nazov":"scscscsdcc","zak_ulica":"sdcsdcsc"},{"zak_id":"6","zak_nazov":"ssadd","zak_ulica":"asdad"},{"zak_id":"11","zak_nazov":"cyc","zak_ulica":"yxcxyc"},{"zak_id":"12","zak_nazov":"sdfsdf","zak_ulica":"sdfdsf"},{"zak_id":"13","zak_nazov":"asdasd","zak_ulica":"asdasd"},{"zak_id":"14","zak_nazov":"ada","zak_ulica":"ada"},{"zak_id":"15","zak_nazov":"oooo","zak_ulica":"ooooo"},{"zak_id":"16","zak_nazov":"adad","zak_ulica":"asdad"}]

But I cannot see them in table. For it I use script looks like:

$(document).ready(function() {

    $('#datatable-keytable').DataTable({
        processing: true,
    serverSide: true,
    type:'Post',
    url: 'php-list-zakaznik.php',           
    data: 'data',                             
    dataType: 'json',
        columns: [
                    { data: 'zak_id' },
                    { data: 'zak_nazov' },
                    { data: 'zak_ulica' }
                ]
      }
    });

});
```

Thanks for help. I did it to two days without datas in table....

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Your syntax is wrong.  data: 'data' should not be there if serverSide is true. If it is not true, then data should be your json object, not a string. ServerSide:true should only be used if you have a lot of data, like over ten thousand rows, or a very slow network.

    If you are sending 'data' as a parameter to the server, it should look more like
    $(document).ready(function() {

        $('#datatable-keytable').DataTable({
            processing: true,
        serverSide: true,
       ajax:{
            type:'Post',
            url: 'php-list-zakaznik.php',           
            data: 'data',                             
            dataType: 'json',
        },
            columns: [
                        { data: 'zak_id' },
                        { data: 'zak_nazov' },
                        { data: 'zak_ulica' }
                    ]
          }
        });
    

    });

    Keep in mind that when serverSide is true, all of the paging, filtering, etc is done by the server. DataTables normally passes the object you need to figure all of this out but your use of "data":"data" would clobber this.

  • BaranekBaranek Posts: 3Questions: 2Answers: 0

    Thanks for your help but without success. This code show all required datas but without pagination and filetring or any table action:
    - PHP file stay without changes only I read all tables tata select * from table
    - js code is this by your recomandation:

        <script>
          $(document).ready(function() {
            $('#datatable-keytable').DataTable({
                processing: true,
                serverSide: true,
                ajax:{
                         type:'Post',
                         url: 'php-list-zakaznik.php', 
                         data: 'data',    
                         dataType: 'json',
                     },
                     columns: [
                        { data: 'zak_id' },
                        { data: 'zak_nazov' },
                        { data: 'zak_ulica' },
                        { data: 'zak_mesto' },
                        { data: 'zak_icdph' },
                        { data: 'zak_tel1' },
                        { data: 'zak_tel2' },
                        { data: 'zak_email' },
                        { data: 'zak_poznamka' }
                      ]
            });
          });   
        </script>
    

    But if I use this then all is function (is this code right?):

        <script>
          $(document).ready(function() {
            $('#datatable').DataTable({
                ordering: false,
            searching: true,
            processing: true,
            serverSide: false,
            ajax: 'php-list-zakaznik.php', 
            dataSrc: 'data', 
                columns: [
                                        { data: 'zak_id' },
                            { data: 'zak_nazov' },
                            { data: 'zak_ulica' },
                            { data: 'zak_mesto' },
                            { data: 'zak_icdph' },
                            { data: 'zak_tel1' },
                            { data: 'zak_tel2' },
                            { data: 'zak_email' },
                            { data: 'zak_poznamka' }
                        ],
                
            columnDefs: [
                 {              
                        targets: [ 0 ],
                        visible: false,
                         searchable: false
                  }
             ],
            });
                
        // Click on row
            $('#datatable tbody').click( function () {
                var table = $('#datatable').DataTable();
                table.rows( { selected: true } ).data();
                alert( "salary is: ");
            });
          });   
        </script>
    
    

    Would somebody help how I do this:
    - end of script // Click on row...
    - here when I click on row I need open form with datas from this selected row for change datas which I need
    - after click on button save these new row datas will be replaced in database.

    Thanks.

This discussion has been closed.