0

The following 'bit' of code, creates a standard html page with a simple table, displaying messages and their timestamps (that could potentially be presented nicer, but atm never mind.

The PHP first retrieves all of the messages (from a particular updated timestamp onwards, if provided). It uses PDO to retrieve records (for DB abstraction reasons). If the timestamp is provided, it encodes the content in json, returns that and dies (Ah, the murdering semantics). If not, it continues and returns a complete html page, where all of the records are shown in a table, as well as a button, that, when pressed, will call the same PHP script with a timestamp, to retrieve any updated messages since the meantime, and updates the related table row cells.

I think that, although basic, this works very nicely. It uses jQuery well to update any relative updates, and I guess something very similarly could work very well for a little html chat, where the table_update function is called every two seconds, or time exponentially incrementing.

Question: Are there any good practice coding out there, that could make this even better?

For example, I wonder whether giving TR any id is a good idea; the PDO code block is a bit of a mixed purée, and I over-enjoy seeing the DIE() function used in a productive way, when normally I have only seen it for post-error mortem-handling, security check-failures, or false script includes.

Any good developers out there, who used to do this, and are now using better approaches?

I don't want frameworks, I want something relatively simple, for eg. flash-pages.

<?php
$sql = 'SELECT id, message, timestamp
    FROM messages
    WHERE id_user = :id_user';
if(isset($_REQUEST["timestamp"]))
{
 $sql .= ' && timestamp >= :timestamp';
}
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth->execute(array(':id_user' => $id_user, ':timestamp' => $_REQUEST["timestamp"]));
$data = $sth->fetchAll();

if(isset($_REQUEST["timestamp"]))
{
 echo json_encode($data);
 die();
}

?>
<html>
 <head>...</head>
<body>
<input id='button_check' value='Update this table' type='button' />
<table id="table_check">
    <tr>
        <th>Message</th>
        <th>Time</th>
    </tr>
<?php foreach($data as $record) { ?>
    <tr id="tr_<?php echo $record["id"] ?>">
        <td><?php echo $record["strNumber"] ?></td>
        <td><?php echo $record["strFullName"] ?></td>
    </tr>
<?php } ?>
</table>

<script type="text/javascript">
var intLastUpdate = <?php echo time() ?>;
$(document).ready(function() {
 $('#button_check').click(update_table());
});

function update_table()
{
 $.getJSON("this_page.php?timestamp=" + intLastUpdate, function(data) {
        $.each(data, function(index, record) {
            $('#tr_' + record.id).find("td").eq(0).html(record.message);
            $('#tr_' + record.timestamp).find("td").eq(1).html(record.timestamp);
            if(intLastUpdate < record.timestamp) {
                intLastUpdate = record.timestamp;
            }
        });
    });
}
</script>

<body>
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Florian Mertens
  • 2,418
  • 4
  • 27
  • 37
  • Try this: http://stackoverflow.com/questions/11868223/using-jquery-load-to-add-rows-to-an-html-table – Jirilmon Jan 21 '13 at 17:22
  • I like that, but that then means you need to generate the TBODY innerHTML BEFORE you determine if the entire layout is needed and DIEEE(), and it's generally considered a bad practice to mix html with server code. Also, your referenced method requires the server to load ALL contents when only very few rows/records may require updating? This being said, your reference is a very interesting alternative. – Florian Mertens Jan 21 '13 at 18:55

0 Answers0