DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Populating datagrid techniques with PHP

Populating datagrid techniques with PHP

Gonzalo Ayuso user avatar by
Gonzalo Ayuso
·
Jul. 19, 11 · Interview
Like (0)
Save
Tweet
Share
7.26K Views

Join the DZone community and get the full member experience.

Join For Free

today i want to speak about populating datagrid techniques with php. at least in my daily work datagrids and tabular data are very common, because of that i want to show two different techniques when populating datagrids with data from our database. maybe it’s obvious, but i want to show the differences. let’s start. imagine we need to fetch data from our database and show it in a datagrid. let’s do the traditional way. i haven’t use any framework for this example. just old school spaghetti code

$dbh = new pdo('pgsql:dbname=mydb;host=localhost', 'gonzalo', 'password');
$dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception);

$stmt = $dbh->prepare('select * from test.tbl1 limit 10');
$stmt->setfetchmode(pdo::fetch_assoc);
$stmt->execute();

$data = $stmt->fetchall();

$table = "";
$table.= "<table>";
foreach ($data as $ow) {
    $table.= "<tr>";
        foreach ($ow as $item) {
            $table.= "<td>{$item}</td>";
        }
    $table.= "</tr>";
}
$table.= "</table>";
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>inline grid</title>
    </head>
    <h1>inline grid</h1>
    <body>
        <?php echo $table; ?>
        <script type="text/javascript" src=" https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    </body>
</html>

and that’s all. the code works, and we’ve got or ugly datagrid with the data from our db. where’s the problem? if our “select” statement is fast enougth and our connection to the database is good too, the page load will be good, indeed. but what happens if or query is slow (or we even have more than one)? the whole page load will be penalized due to our slow query. the user won’t see anything until our server finish with all the work. that means bad user experience. the alternative is load the page first (without populated datagrid, of course) and when it’s ready, we load with ajax the data from the server (json) and we populate the datagrid with javascript.

page without the populated datagrid:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>grid ajax</title>
    </head>
    <h1>grid ajax</h1>
    <body>
        <table id='grid'></table>

        <script type="text/javascript" src=" https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script type="text/javascript">
        $(function(){
            $.getjson("json.php", function(json){
                        for (var i=0;i<json.length;i++) {
                            $('#grid').append("<tr><td>" + json[i].id + "</td><td>" + json[i].field1 + "</td></tr>")
                        }
                    });
        });
        </script>
    </body>
</html>

json data fron the server:

$dbh = new pdo('pgsql:dbname=mydb;host=localhost', 'gonzalo', 'password');
$dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception);

$stmt = $dbh->prepare('select * from test.tbl1 limit 10');
$stmt->setfetchmode(pdo::fetch_assoc);
$stmt->execute();

$data = $stmt->fetchall();

header('cache-control: no-cache, must-revalidate');
header('expires: mon, 26 jul 1997 05:00:00 gmt');
header('content-type: application/json');

echo json_encode($data);

the outcome of this second technique is the same than the first one, but now user will see the page faster than the first technique and data will load later. probably the total time to finish all the work is better in the classical approach, but the ux will be better with the second one. here you can see the times taken from chorme’s network window:

even though the total time is better in the inline grid example: 156ms vs 248ms, 1 http request vs 3 http request . the user will see the page (without data) faster with the grid data example.

what’s better. as always it depends on our needs. we need to balance and choose the one that fits with your requirements.

probably the javascript code that i use to populate the datagrid in the second technique could be written in a more efficient way (there’s also plugins to do it). i only want to show the indirect way to create html to improve the user experience of our applications.

from http://gonzalo123.wordpress.com/2011/07/18/populating-datagrid-techniques-with-php/

PHP

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices 101: Transactional Outbox and Inbox
  • Is DevOps Dead?
  • AWS CodeCommit and GitKraken Basics: Essential Skills for Every Developer
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: