Getting draw=0 using "$json_data =array" method

Getting draw=0 using "$json_data =array" method

chessGuru64chessGuru64 Posts: 79Questions: 18Answers: 1
edited March 2019 in Free community support

I thought this code below would get the draw to be 1 but obviously not:

$(document).ready(function() { $('#example3').DataTable().draw; } );

Full length code:
$sql = "SELECT * from tbl where price>100";

$query=mysqli_query($conn, $sql) or die("error");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.

$data = array();

while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array(); 

    $nestedData[] = $row["name"];
    $nestedData[] = $row["info"];
    $nestedData[] = $row["price"];

    $data[] = $nestedData;
}

$json_data =array(
           "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
            "recordsTotal"    => intval( $totalData ),  // total number of records
            "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
            "data"            => $data   // total data array
            );

echo json_encode($json_data);  // send data as json format

Error message (also is attached)
Notice: Undefined index: draw in /storage/ssd3/820/8487820/public_html/filter.php on line 103
{"draw":0,"recordsTotal":11,"recordsFiltered":6,"data":[["Tesla 3","310 mile range, 0-60 mph in 3.3 seconds","36900"],["Intel 8GB","8GB DDR4 RAM","580"],["AMD Ryzen","Ryzen Threadripper 2990WX","999.99"],["Intel 8 Gen Process","The eight-core, 16-thread Intel\u00ae Core\u2122","1099"],["Roku Smart TV","Connect devices like a cable set-top box ","148.59"],["Tesla Model S","Speed and endurance\u2014with incredible aerodynamic","32700"]]}

Answers

  • allanallan Posts: 61,451Questions: 1Answers: 10,055 Site admin

    I don't see where you define $requestData. Is it $_POST or $_GET it is referring to (at a guess, it might be something else)? Does your client-side match that - e.g. if you are using $_POST is it POSTing the data?

    Allan

  • chessGuru64chessGuru64 Posts: 79Questions: 18Answers: 1

    I figured it could be something within here:
    $(document).ready(function() {
    var dataTable = $('#example3').DataTable( {
    processing: true,
    serverSide: true,
    ajax: {
    url: 'filter.php',
    // type: 'get',
    },

    I tried using get, post, and // (commenting out completely). So I gave up on the idea that the issue was coming from there. I do have a $requestData= $_REQUEST; but it obviously requests nothing.

    I will PM you my project link which might help.

  • chessGuru64chessGuru64 Posts: 79Questions: 18Answers: 1
    edited March 2019

    This method:

            $records = "SELECT * from tbl where price>200";
            $records=mysqli_query($conn, $records);
    
            $row = mysqli_fetch_array($records);
            if (!$row) {
                printf("Error: %s\n", mysqli_error($conn));
                exit();
            }
            while ($row = mysqli_fetch_array($records)) { ?>
    

    Seems to be way easier.
    And worked for me.

This discussion has been closed.