Importing and mapping data from the source file to the HTML tables


php newbie here..I need some PHP help ideas/examples on how to import data from a delimited text file and map them into html tables. The data should populate and be mapped under its proper header. There are instances also where each record doesn't have all the values and if no data, then we can leave it null (See sample records). I would also create a table row entry for each record.

For example, the input/source file has these entries: (they are prefixed by a number to represent the header in the html table. So data from "1-MyServer" is "" and should be under table header "Server" for example. There are instances also where the record doesn't have all the values (1-7) (See below):;2-MyLogdate=Wed Aug 11 2010;3-MyDataset=dbip.pp1;4-MyStartTime=01:00:03;5-MyDuration=00:36:09;6-MySize=41.54 GB;7-MyStatus=Succeeded;;2-MyLogdate=Wed Aug 11 2010;3-MyDataset=gls202.kul_lvm;5-MyDuration=06:20:33;7-MyStatus=Succeeded;;2-MyLogdate=Wed Aug 11 2010;3-MyDataset=gls101.aie_lvm;4-MyStartTime=01:00:02;

Here is a copy of my html table that I would need it to map it too: (Also, I would not have to populate record for "2-MyLogdate" into a header)

<table id="stats">
<td>41.54 GB</td>

So what I really need is a system to map these appropriately. How would I write this in php?? Thanks!

It's all about finding the patterns in your files. In your example, it's rather easy:

[number]-[column name]=[value];

The main problem I see is that you have redundant information: the number of the column, and the columns themselves, which are repeated for every row. You can parse them away, though. It depends on what you expect from your program: will the columns order always be the same? Will there always be the same columns? How should you react to unknown columns?

Here's a quick example of what you could do, using regular expressions. This example assumes that column names are all the same, and that you want to display them all, and that they'll always be in the same order. In other words, I'm doing it the easy way.

$data = array();

$lines = file("my/log/file.log");
// this will parse every line into an associative array, discarding the header number
foreach ($lines as $line)
    // populate $matches with arrays where indices are as follows:
    // 0: the whole string (0-Foo=bar;)
    // 1: the column number (0)
    // 2: the column name (Foo)
    // 3: the value (Bar)
    preg_match_all("/([0-9])-([^=]+)=([^;]+);/", $line, $matches, PREG_SET_ORDER);
    $lineData = array();
    foreach ($matches as $information)
        $lineData[$information[2]] = $information[3];
    $data[] = $lineData;

$keys = array_keys($data[0]); // you can also set this yourself
// for instance, $keys = array('MyServer', 'MyDataset'); would only display those two columns
echo '<table><tr>';
foreach ($keys as $column)
    echo '<th>' . $column . '</th>';
echo '</tr>';

foreach ($data as $row)
    echo '<tr>';
    foreach ($keys as $column)
        echo '<td>' . (isset($row[$column]) ? $row[$column] : '') . '</td>';
    echo '</tr>';
echo '</table>';