Okay I'm trying to get this to work server side with what I have. help?

Okay I'm trying to get this to work server side with what I have. help?

SkyScreamSkyScream Posts: 22Questions: 1Answers: 0
edited January 2015 in Free community support

I have a php page that already has a database connection.
this needs to be manual. Because I'm not calling a table.
I'm calling a stored procedure.

So what I did was, I echoed the results in a string that looks exactly like the JSON representation in the serverside example on here.

Here's what my loop and output look like.
When I call this page, Datatables says it isn't JSON.
How I can make this page Appear to be JSON?

I don't want to create another page to spit the results into. Because multiple people may be using this at the same time. If the results from someone and another person update that file, then person A may get person B's results.

--------(code)not sure why only some of it is showing up as "code"(there's some bellow it)----------

$sum = 0;
$records = "";

        while ($row = mysqli_fetch_array($result)){  
            $sum += $row[0];  

            $records = $records. ' [  
                          "'.$row[0].'",  
                          "'.$row[1].'",  
                          "'.( $row[2] == "" ? "Not Specified" : $row[2]).'",  
                          "'.( $row[3] == "" ? "Not Specified" : $row[3]).'",  
                          "'.$row[4].'",  
                          "'.$row[5].'",  
"<a target=\"_blank\" href=\"http://maps.google.com/?q='.$row[8].', '.$row[5].', '.$row[4].'\"><img width=\"22px\" src=\"http://i2techsolutions.com/greenbook/visistat/library/images/googlei22w.png\"></a>",  
                          "'.$row[7].'"  
                        ],';  

}   

$records = substr($records, 0, -1);   
$json = '{  
  "draw": 1,  
  "recordsTotal": '.$sum.',  
  "recordsFiltered": 57,  
  "data": [';  

$json = $json.$records;    

$json = $json.']  
}';  

}

echo JSON.parse($json);

Replies

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0
    edited January 2015

    i tried adding
    header('Content-Type: application/json');

    before the echo and changing it to

    echo json_encode($json);

    but it says same error

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited January 2015

    If you are going to json encode it, why are you trying to put it in json format before that? json_encode will turn your PHP arrays into a json object. Same thing for your records... it will go from a php array to a json object when you echo json_encode(); it.

    $records = array();
    while ($row = mysqli_fetch_array($result)){ 
        $sum += $row[0];
        $records[] = array(
            0   =>  $row[0],
            1   =>  $row[1],
            2   =>  ( $row[2] == "" ? "Not Specified" : $row[2]),
            3   =>  ( $row[3] == "" ? "Not Specified" : $row[3]),
            4   =>  $row[4],
            5   =>  $row[5],
            6   =>  "<a target='_blank' href='http://maps.google.com/?q=".$row[8].",".$row[5].",".$row[4]."><img width='22px' src='http://i2techsolutions.com/greenbook/visistat/library/images/googlei22w.png'></a>",
            7   =>  $row[7]
        );
    }
    
    $json = array(
        "draw"          => 1,
        "recordsTotal"      => $sum,
        "recordsFiltered"   => 57,
        "data"          => $records
    );
    
    echo json_encode($json);
    
  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0
    edited January 2015

    Thank you. I tried this, but it didn't work. I even tried to add:
    header('Content-Type: application/json');

    but i still get this error. "DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"

    this is what my code looks like now:

    $sum = 0;
    $records = "";

    $records = array();  
    

    while ($row = mysqli_fetch_array($result)){
    $sum += $row[0];
    $records[] = array(
    0 => $row[0],
    1 => $row[1],
    2 => ( $row[2] == "" ? "Not Specified" : $row[2]),
    3 => ( $row[3] == "" ? "Not Specified" : $row[3]),
    4 => $row[4],
    5 => $row[5],
    6 => "<a target='_blank' href='http://maps.google.com /?q=".$row[8].",".$row[5].",".$row[4]."><img width='22px' src='http://i2techsolutions.com/greenbook/visistat/library/images/googlei22w.png'></a>",
    7 => $row[7]
    );
    }

    $json = array(
    "draw" => 1,
    "recordsTotal" => $sum,
    "recordsFiltered" => 57,
    "data" => $records
    );

    header('Content-Type: application/json');
    echo json_encode($json);

  • ahrefahref Posts: 9Questions: 1Answers: 1

    Paste your json response(use debugging tools to see) into jsonlint.com

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    actually. it might have worked. I guess it was a fluke the first reload.

    I'm getting this error now:

    "DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3"

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    oh nvm. that error is because I'm calling it from somewhere else.

    the json error is still standing when i just refresh it. Let me go through the debugger and check it.

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0
    edited January 2015

    After inspecting it. It may be getting stuck because my post data isn't being sent correctly.

    Let me show you a piece of the set up; and you can tell me if I'm doing it wrong.
    All this code works fine as it is. When POST data was actually sent to the php page.
    I guess what I have set up is not actually passing POST variables to the php page.
    I assumed that since adding "type": "POST" and no place to actually build the POST variables, that the datatables would automatically grab them from the input fields lieing around the page. Am I wrong?

    ---the debug error---

    <div style='margin: 10px;color: red;font-size: 16px;'>Please select an end date.</div><br />  
    <b>Warning</b>:  Cannot modify header information - headers already sent by (output         started at /home/i2techso/public_html/greenbook/visistat/includes/views/DetailsTableJSON.php:6) in <b>/home/i2techso/public_html/greenbook/visistat/includes/views/DetailsTableJSON.php</b> on line <b>55</b><br />  
    <br />  
    <b>Fatal error</b>:  Call to undefined function parse() in <b>/home/i2techso/public_html  /greenbook/visistat/includes/views/DetailsTableJSON.php</b> on line <b>57</b><br />  
    

    ----jquery---

    $('#example').dataTable( {  
        "processing": true,  
        "serverSide": true,  
        "ajax": {  
        "url": "../includes/views/DetailsTableJSON.php",   
        "type": "POST" }  
    
    } );  
    

    -----the input that isn't being recognized--- (on the same page as the jquery)

    <input placeholder="End Date" id="datepickerF" type="text" class=" datepickerCSS" name="dateF">  
    

    ---the serverside that is trying to recieve the POST data---

    } elseif ( $_POST["dateF"] == "") {  
    
    
    echo "<div style='margin: 10px;color: red;font-size: 16px;'>Please select an end   date.</div>";   
    
    
    } 
    
  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    without the dates passing into the php page, the queries will fail. If the queries fail, the loop will fail. if the loop fails, the array is not made. if the array is not made, the json is not made.

  • numberonenumberone Posts: 86Questions: 0Answers: 0

    check

    line: 57 on views/DetailsTableJSON.php

    Call to undefined function parse()

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0
    edited January 2015

    the strange thing... there is no line 57 on that page.

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    nor is there a "parse()" on the whole page

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    the "parse" must be in the code that "echo json_encode($json);" spits out.

    Which would make sense. because there is no results stored in the array. I have to fix the POST issue first.

  • ahrefahref Posts: 9Questions: 1Answers: 1

    Without using datatables. What is the output of your php file.

    Is it valid json?

    jsonlint.com will tell you if it is.

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    okay I manually set the POST data. (Still is a problem)
    but now, it's working.

    Only issue is, its saying "Showing 0-10 records, and its spitting all of them out. with unclickable page numbers.

    I probably just need to learn the parameters in the beginning of the json.

  • ahrefahref Posts: 9Questions: 1Answers: 1

    Can you please tell us if you have valid JSON or not?

    Invalid JSON will not work with datatables.

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    yes the JSON is working. I'm getting the record results now.

    They just aren't being paged correctly.

  • SkyScreamSkyScream Posts: 22Questions: 1Answers: 0

    The results are showing 26 pages in the pagination at the bottom.
    and the text says "Showing 1 to 10 of 288 entries" at the bottom.

    But... it shows all 288 records on the first page, despite this info.

This discussion has been closed.