开发者

An algorithm to create HTML table from this row data?

I have come unstuck on a relatively (almost) trivial problem. I have a row of data that I want to display 开发者_Python百科in tabular form (HTML). For some reason (possibly long day [again] behind the computer), I am not coming up with any elegant solutions (algorithms) to do this.

I have presented some sample data, and how such data would be displayed in the table. I would be grateful for some ideas on algos to implement this.

The output table has rows labelled with the various scores and the indices are displayed at the bottom.

I would like to be able to have a variable determine the number of columns to print, before a new table is printed underneath - to prevent ridiculously long tables.

so I want to write a function with the following signature (ignoring data types):

function create_table_html_from_rows(datarows, max_cols_per_table)

Here is the sample data and mock output table presentation

Row data:

index, Score, amount
1, level 1, 12.24
3, level 4, 14.61
9, level 10, 42.35
15, level 2, -8.12


Scores
======

Level 1  12.24   
Level 2                       -8.12
Level 3
Level 4         14.61
.....
Level 10               42.35
----------------------------------------
         |  1  |  3  |  9   |   15       <- Index

Pseudocode should be sufficient, however, if you snippet is in a programming language, it is perharps worth pointing out that I will be implementing my algorithm in Python, however any snippets in any of the following languages would be fine:

Python, C, C++, PHP, C#


I assumed you have Raw data stored similar to what I have below. Also, the level is non-unique.

<?
    $rowData = array( 
        0=> array('index'=>1, 'Score'=>"level 1", 'amount'=>12.24), 
        1=> array('index'=>3, 'Score'=>"level 4", 'amount'=>14.61),
        2=> array('index'=>9, 'Score'=>"level 10", 'amount'=>42.35),
        3=> array('index'=>15, 'Score'=>"level 2", 'amount'=>-8.12),
        4=> array('index'=>12, 'Score'=>"level 10", 'amount'=>16.5)
        // example Raw Data with non-unique levels
    );
    $numOfScores = 15; // Predefined Score Level Max
    foreach($rowData as $c){
        $cols[] = $c['index']; //Create index row
        $score = str_replace("level ","",$c['Score']); // split the score so it is only numeric
        $levels[$score][] = $c; //create a 2-D array based on the level
    }
    echo "<table><tr><th colspan=".(sizeof($cols)+1).">Scores:</th></tr>";
    for($ii = 1; $ii < $numOfScores; $ii++){ // Go through the levels
        echo "<tr><td>Level ".$ii."</td>";
        for($i = 0; $i < sizeof($cols); $i++){
            echo "<td>";
            if(isset($levels[$ii])){ // If I have a level, let's print it in the right column
                foreach($levels[$ii] as $lvl)
                    if($cols[$i] == $lvl['index']) echo $lvl['amount'];
            }
            echo "</td>";
        }
        echo "<tr>";
    }
    echo "<td>Index:</td>";
    foreach($cols as $c){
        echo "<td>$c</td>";
    }
    echo "</table>";
    ?>

I get the following output:

Scores:
Level 1 12.24               
Level 2             -8.12   
Level 3                 
Level 4     14.61           
Level 5                 
Level 6                 
Level 7                 
Level 8                 
Level 9                 
Level 10        42.35       16.5
Level 11                    
Level 12                    
Level 13                    
Level 14                    
Index:  1   3   9   15  12

It is tabbed correctly on my screen, probably won't appear to be.

As you can see, I added an extra row in your data to simulate a non-unique row. Should work!


Tomorrow is a new day. The problem is really trivial. Also there can not be elegant solution, just long one.

Also you can write even solution (with no respect to its elegancy or other), just to get your work done. And after that you will get ideas of where your thinking was stuck and how to code better solution.


I'd do it in the following way (Note: the code is not tested / debugged at all!):

<?php
$lines = file ("data.txt");
foreach ($lines as $line) {
  $array = explode(",", $line);
  $level = $array[1];
  $index = $array[0];
  $amount = $array[2];
  $result[level][index] = $amount;
  $indeces[] = $index;
}

echo "Scores<br>\n======<br>\n"
echo "<table>";
foreach ($result as $level => $row) {
  echo "<tr>\n<td>$level\n";
  foreach ($indeces as $index) {
    echo "<td>";
    if (!isset($row[$index])) {
      echo "&nbsp;";
    } else {
      echo $row[$index];
    }
    echo "\n";
  }
}
echo "<tr>\n<td>\n";
foreach ($indeces as $index) {
  echo "<td>$index\n";
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜