2

I am converting MySQL table into HTML table using this PHP snippet, and it looks something like this [html]:

/----------------------------------------\
| Name | Subscribed           | Duration | <- Column names
|----------------------------------------|
| Bob  | 2011-08-20 04:07:01  | 60       |
|----------------------------------------| 
| Ben  | 2011-08-20 04:07:01  | 260      |
\----------------------------------------/
  1. How can I output the table without column names? (Name, Subscribed, Duration) So that it starts from Bob.
  2. Is it possible to convert that date into something like this: August 20, 2011 4:07:01?
  3. Duration is in seconds, how can I convert it into minutes (Duration (s)/60) in PHP, so that it will show 1 instead of 60 seconds?
4
  • What have you tried? It should be simple if you are familiar with PHP and MySQL. For #1, try reading the snippet COMMENTS. Commented Aug 20, 2011 at 14:35
  • Unfortunately it's an article, no comment section :( Commented Aug 20, 2011 at 14:41
  • 1
    There are comments in the code! Commented Aug 20, 2011 at 14:41
  • link facepalm Commented Aug 20, 2011 at 14:44

4 Answers 4

2

Here is the code, tailored to your requirements:

// 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'>";
// printing table rows
while($row = mysql_fetch_assoc($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $key => $cell){
        if($key == 'Subscribed'){
            $cell = date('F j, Y H:i:s', strtotime($cell)); // format date August 20, 2011 4:07:01
        } elseif($key == 'Duration'){
            $cell = $cell/60; // format time in minutes
        }
        echo "<td>$cell</td>";
    }


    echo "</tr>\n";
}
echo '</table>';
Sign up to request clarification or add additional context in comments.

1 Comment

@Shef is one awesome guy. I have got lots of help from him as well. He's a one in a million :)
0

About the date, try something like this:

echo strftime("%F %d, %Y %g:%i:%S", strtotime($v["datum"]));

Comments

0

You can use this code to remove the column names. This is the same snippets from the link you posted but with some lines removed. This will also print the duration in minutes.

<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'lptm42b';

$database = 'sphinx';
$table = 'spheres';

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");
}


echo "<h1>Table: {$table}</h1>";
echo "<table border='1'>";
// printing table rows
while($row = mysql_fetch_assoc($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $field => $value)
    {
        if ($field === 'Duration')
        {
            $value = $value / 60;
        }
        echo "<td>$value</td>";
    }
    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>

Comments

0

1.) Delete the for loop after // printing table headers.

2.) Use date() and strtotime() for the column "Subscribed" (second one if you use mysql_fetch_row())

3.) Divide the third column by 60.

But I would suggest using mysqli_fetch_array() instead of mysqli_fetch_row().

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.