Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a php script that encodes my db table into an array. I echo the json_encode and it echos just fine. script :

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="testdbpass"; // Mysql password
$db_name="test"; // Database name

// Connect to server via PHP Data Object
$dbh = new PDO("mysql:host=localhost;dbname=test", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$array = $dbh->query("SELECT id, anum, first, last,
                        why, comments, aidyear, additional_req,
                        signintime FROM inoffice WHERE 
                        counselorname IS NULL")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($array);

?>

and the output of this :

JSON array :

[{"id":"7","anum":"B00000000","first":"rixhers","last":"ajazi","why":"Other","comments":"Jut need some help!!!","additional_req":"","aidyear":"12-13","signintime":"2013-01-16 09:08:35"},{"id":"8","anum":"A00000000","first":"rixhers","last":"ajazi","why":"Appeal","comments":"","additional_req":"","aidyear":"12-13","signintime":"2013-01-16 09:28:57"},{"id":"9","anum":"A00000000","first":"rixhers","last":"ajazi","why":"Appeal","comments":"","additional_req":"","aidyear":"12-13","signintime":"2013-01-16 10:12:07"},{"id":"10","anum":"A00000000","first":"rixhers","last":"ajazi","why":"Appeal","comments":"","additional_req":"","aidyear":"12-13","signintime":"2013-01-16 11:19:18"}]

I then have a page that end user can visit that should extract the data and then place it into a table. For some reason I can not get it TO post to my first table on that page. The table must include rows for however mysql table results there are.

these are my jquery scripts I have running on the page :

    <head>
    <script src="core/media/js/jquery.js" type="text/javascript"></script>
    <script src="core/media/js/jquery.dataTables.js" type="text/javascript"></script>
    <script type="text/javascript" src="core/media/js/install.js"></script>
    <script type="text/javascript">
    $.ajax({
    url: 'core/media/js/getdatainoffice.php',
    type: 'GET',
    async: false,
    dataType: 'json',
    success: function (result) {

        var insert = '';

        $.each(result, function() {
            insert += '<tr><td>' + id + '</td><td>' + anum + '</td><td>' + first + '</td><td>' + last + '</td><td>' + why + 
                        '</td><td>' + comments + '</td><td>' + additional_req + '</td><td>' + aidyear + '</td><td>' 
                        + signintime + '</td></tr>';
        });
        $('#datatables tr').after(insert);
    }


});

    </script>    

this is my table :

table id='datatables' class='display'>
                <thead>
                    <tr>
                 <th>Session ID </th>                        
                 <th>A Number</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Reason for visit</th>
                        <th>Comments</th>
                        <th>Aid Year</th>
                        <th>Additional Requirements</th>
                        <th>Signin Time</th>
                    </tr>
                </thead>
                    <tbody>


                </tbody>
        </table>

Any help would or explanations would be lovely. I have been reading off this site but to no avail.

More information : I am using a jquery plugin called datatables and I need to populate that table.

The ultimate goal for this project is to have the tables dynamically update when ever there is a new row inserted or a row be updated. Any help would be great!

share|improve this question
Code Review is a Q&A for code review currently functioning, your question would be more suitable in that Stack Overflow that is a Q&A oriented to help and make things work. although two issues seem similar, really are not. – RTOSkit Feb 2 at 0:35

closed as off topic by Brian Reichle, Joseph Silber, Winston Ewert Feb 2 at 14:29

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

I'm not allowed to comment it, so I have to create an answer:

Did you try to debug your script and print the result variable. Is the JSON included or maybe a PHP/SQL exception message? If you don't use it already install Firebug and use the Network tab.


For an auto update you have to regularly pull for the results.


Update:

So result contains your valid JSON. Next step is checking the insert. Is insert build correctly or is it empty after the each? If it's not empty, then you should test your selector. If it is empty, check the each.

In addition to that you might want to use append() instead of after. Did you check the resulting source code for adding the code to the wrong place?

share|improve this answer
Yes data is being passed back in the response. I can also go to the file where I create the json array and i echo it on php and the array is visable – RaGe10940 Jan 18 at 14:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.