0

Here is an example with columns and values:

+----------------------------------------------------------------+
| rate | conn/s | req/s | replies/s avg | errors | net io (KB/s) |
+----------------------------------------------------------------+
|  100 | 99.9   | 99.9  | 99.7          | 0      | 45.4          |
|  120 | 119.7  | 119.7 | 120.0         | 0      | 54.4          |
|  140 | 139.3  | 139.3 | 138.0         | 0      | 63.6          |
|> 160 | 151.9  | 151.9 | 147.0         | 0      | 69.3          |
|  180 | 132.2  | 129.8 | 137.4         | 27     | 59.6          |
|  200 | 119.8  | 117.6 | 139.9         | 31     | 53.9          |
+----------------------------------------------------------------+
3
  • To be viewed where? In a monospaced font environment (i.e. a command line) or written to a file? A browser? Commented Nov 28, 2010 at 5:14
  • 1
    There is also a homework tag, and there is no shame to use it... Commented Nov 28, 2010 at 5:17
  • This is to be written to a file and the data needs to be purged on each run, so that this "result" formatted output in a file is only persisted once every execution cycle. Commented Nov 28, 2010 at 13:25

3 Answers 3

1

Assuming that you want to display it in a browser and fetching data from MySQL. Use this code, replace your dbname and tablename with correct values:

<html>
<head><title>Result as Table</title></head>
<body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '';

$database = 'dbname';
$table = 'tablename';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>
Sign up to request clarification or add additional context in comments.

Comments

0

Look into printf/sprintf:

printf('| %3s | %-5.1f | …', $rate, $conns, …);

Comments

0

printf() is your friend here.

<?php
function printRows($rows){
    $br = "+----------------------------------------------------------------+";
    $he = "| rate | conn/s | req/s | replies/s avg | errors | net io (KB/s) |";

    //Print out the header
    print "$br\n$he\n$br\n";

    //Iterate through the rows
    foreach($rows as $row){
        printf("|[%-5s] | [%-7s] | [%-5s] | [%-13s] | [%-6s] | [%-13s] |",
               $row[0],$row[1],$row[2],$row[3],$row[4],$row[5] 
              );

    }

    //Print the end ascii-art
    print "$br\n";
}
?>

Comments

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.