0

I am a beginner in PHP programing.i have a code and i need to get the first 3 datas in the first table and all the datas in the next table.the data is coming from db as json.

The following is my php file

<?php 
    $host = "localhost"; 
    $user = "root"; 
    $pass = ""; 
    $database = "TMS_Sample"; 

    $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
    mysql_select_db($database, $linkID) or die("Could not find database."); 


    $result = mysql_query("SELECT * FROM Completed_Training");
    while ($row = mysql_fetch_assoc($result)) {
        $array[] = $row;
    }
    echo json_encode($array);
?> 

I get the following json as result:

[{"Date":"2012-12- 04","Topic":"Collections","Trainer":"Prabhakaran.G","Status":"Invitation Sent"},{"Date":"2012-12-12","Topic":"Collections","Trainer":"Prabhakaran.G","Status":"Invitation Sent"},{"Date":"2012-12-07","Topic":"ffb","Trainer":"vcvxcv","Status":"cvxcv"},{"Date":"2012-12-08","Topic":"xcv","Trainer":"cvxcv","Status":"vxcv"},{"Date":"2012-12-09","Topic":"cvxcv","Trainer":"cvxcv","Status":"cvxcvxc"},{"Date":"2012-12-10","Topic":"xcv","Trainer":"vxcvxc","Status":"vxcv"},{"Date":"2012-12-11","Topic":"vv","Trainer":"vv","Status":"vxcv"},{"Date":"2012-12-12","Topic":"vv","Trainer":"vcv","Status":"cvxcv"},{"Date":"2012-12-13","Topic":"vv","Trainer":"cvxcv","Status":"cvv"},{"Date":"2012-12-14","Topic":"vxcv","Trainer":"vccv","Status":"xcvcv"},{"Date":"2012-12-14","Topic":"vcxvxc","Trainer":"cvxcv","Status":"cvxcv"},{"Date":"2012-12-15","Topic":"cvxcv","Trainer":"xcvxcv","Status":"xcvxcv"},{"Date":"2012-12-16","Topic":"sdasd","Trainer":"sdasd","Status":"dscxzc"},{"Date":"2012-12-16","Topic":"scxzcxzc","Trainer":"sdfscxzc","Status":"sadffcvzxc"},{"Date":"2012-12-17","Topic":"fxzcvxzc","Trainer":"zcvxzcz","Status":"xzcxzcxzcxz"},{"Date":"2012-12-18","Topic":"xzceafsdfv","Trainer":"vxvxv","Status":"xgsdfgvsd"},{"Date":"2012-12-12","Topic":"xcdvxvxcv","Trainer":"vxzdgvgSv","Status":"gbvsgv"},{"Date":"2012-12-27","Topic":"SDgvsdv","Trainer":"sdvsdv","Status":"sdgvsdv"},{"Date":"2012-12-11","Topic":"sdvsvd","Trainer":"sdvsdv","Status":"vdv"},{"Date":"2012-12-22","Topic":"dvsdv","Trainer":"vssdv","Status":"vsdvV"}]

Now what I need is to display this json data in a html table

I have displayed all the data in 1 table. But what I need is to get the first 3 data in the first table and all the data in the next table.

This is my html code:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript" src="jquery182.js"></script>
        <script type="text/javascript" language="javascript">
            $(document).ready(function () {
                $.getJSON('CompletedTraining.php', null, function (data) {
                    var Completed_counter = 1;
                    if (data) {
                        if (Completed_counter <= 3) {
                            var table = '<table border="1">';
                            $.each(data, function (i, element) {
                                table += '<tr>';
                                table += '<td>' + element.Date + '</td>';
                                table += '<td>' + element.Topic + '</td>';
                                table += '<td>' + element.Trainer + '</td>';
                                table += '<td>' + element.Status + '</td>';
                                table += '</tr>';
                            });
                            table += '</table>';
                            $('#getname').html(table);
                        }

                        var table1 = '<table border="1">';
                        $.each(data, function (j, element1) {
                            table1 += '<tr>';
                            table1 += '<td>' + element1.Date + '</td>';
                            table1 += '<td>' + element1.Topic + '</td>';
                            table1 += '<td>' + element1.Trainer + '</td>';
                            table1 += '<td>' + element1.Status + '</td>';
                            table1 += '</tr>';
                        });
                        table1 += '</table>';
                        $('#getname1').html(table1);
                        Completed_counter = Completed_counter + 1;
                    } else {
                        alert("error");
                    }
                });
            });
        </script>
    </head>
    <body>
        <form name="index">
            <table id="getname" ></table>
            <table id="getname1" ></table>
        </form>
    </body>
</html>
5
  • you can use the variable i for looping only 3 times in the first table and in the table1, use the variable j starting from 4 onwards. Commented Dec 14, 2012 at 8:27
  • i need all the datas in the second table.... Commented Dec 14, 2012 at 8:28
  • so u can have all the JSON data in the table1 without starting from 3 Commented Dec 14, 2012 at 8:29
  • where is your code to receive data from json. Commented Dec 14, 2012 at 8:40
  • Thats the php code tht i hv given first.it works perfectly.the problem is in the HTML code Commented Dec 14, 2012 at 8:44

1 Answer 1

1

i made it as simple as i can.. so that it is easy to understand..

no need to loop the data twice... use $.each() function once.. and check for the condition inside the $.each function

here is the fiddle..

http://jsfiddle.net/yXB3W/4/

updated

another fiddle..

http://jsfiddle.net/yXB3W/7/

You already have data .. so you just need to check the codes from if(data)... in fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx a lot dude...tht was a great help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.