1

I try to make a dynamic table. The data comes from a database. It works so far, but I want to make a table with seperate fields, not like my way. My code so far:

 <?php

$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result); 
echo("Buy Date"."|Expire Date");
echo "<br />";
echo "<br />";
while ($row = mysql_fetch_array($result)) {
    echo '<th>'.$row['buyTime'].'</th>'."|".'<th>'.$row['untilTime'].'</th>'; 
    echo "<br />";
}
?>

The result:

Click here

So how can I make a correct table, not a pseudo one?

Thank you :)

Regards

1
  • OT: You will need to ditch using all mysql functions for your own good (its depreciated in later PHP 5.6+). And also look into prepared statements when dealing with building the sql. Commented Nov 4, 2017 at 15:35

2 Answers 2

1

Use a table element on html and put your php loop code inside the tbody element. How to properly create a table;

<table>
 <thead>
  <tr>
   <th>Buy Date</th>
   <th>Expire Date</th>
  </tr>
 </thead>

 <tbody>
 <?php
  $result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
  $num_rows = mysql_num_rows($result);

  while ($row = mysql_fetch_array($result)) {
   echo '<tr>';
   echo '<td>'.$row['buyTime'].'</td><td>'.$row['untilTime'].'</td>';
   echo '</tr>'
  }
 ?>
 </tbody>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

ahem ... sql injection alert, and also mysql depreciated alert ... ;)
In this case, he is asking how to properly display a table. Although, it's nice if he would study some mysqli.
Yes. I see a lot of downvotes because of it, despite the question or answer having nothing to do with it. So, wanted to protect you from those people downvoting just because you forgot to mention mysql and sql injection. Your answer is spot on for doing what he asked for.
1
    <!DOCTYPE html>
    <html>
    <head>
    <style>
    table {
        border-collapse: collapse;
        width: 100%;
    }

    th, td {
        text-align: left;
        padding: 8px;
    }

    tr:nth-child(even){background-color: #f2f2f2}

    th {
        background-color: #4CAF50;
        color: white;
    }
    </style>
    </head>
    <body>

    <h2>Colored Table Header</h2>

    <table>
      <tr>
        <th>Buy Date</th>
        <th>Expire Date</th>
      </tr>

      <?php
$result = mysql_query("SELECT buyTime, untilTime FROM users WHERE userName='".$_SESSION["user"]."';");
$num_rows = mysql_num_rows($result); 

while ($row = mysql_fetch_array($result)) {  ?>
      <tr>

        <td><?php echo $row['buyTime']; ?></td>
        <td><?php echo $row['untilTime']; ?></td>

      </tr>
<?php } ?>


    </table>

    </body>
    </html>

NOTE: An HTML table is defined with the <table>tag.

Each table row is defined with the <tr> tag. A table header is defined with the <th> tag. By default, table headings are bold and centered. A table data/cell is defined with the <td> tag. Moreover Mysql is outmoded please try learn new approach. You can learn it on w3schools php

Good Luck!!!

2 Comments

Thanks, thats beautiful, but its not adding a line when there are more than one lines :S image.prntscr.com/image/1e7cAgEmT4q-KJrPZ5Fw8Q.png
you can change the css to fit what you want. By the way this one is professional and it differentiate it by color not by line @ReneNikolay

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.