Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

php - Echo out mysql into a html table?

I need some help on how to echo out my mysql data into a html table. I'm trying to put in the relevant table tags where necessary but I must be doing it wrong as its not looking how I want it.

Here's what I am getting:

ID        Name      Number       Note        Alert
1
2         

Nick      Nick                   insurance       insurance      alert      alert

Here is how I want it to look:

ID        Name      Number       Note           Alert
1         Nick                   Insurance      Alert
2         Nick                   Insurance      Alert

Here is my code, can someone please show me where I need to put my table tags to get the desired result:

<?php
include 'config.php';

$data = mysql_query("SELECT * FROM supplier_stats") or die(mysql_error());
echo "<table class="table" style="width:900px;  font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px; color:#96969;" >";

while ($info = mysql_fetch_array($data))
    {
    echo "<tr><td><p>" . $info['id'] . "</p></td></tr>";
    }

?> 

<?php
include 'config.php';

$data = mysql_query("SELECT * FROM supplier_stats") or die(mysql_error());

while ($info = mysql_fetch_array($data))
    {
    echo "<td><p>" . $info['company_name'] . "</p></td>";
    }

?> 

<?php
include 'config.php';

$data = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date 
  FROM supplier_stats") or die(mysql_error()); ?>

<?php
include 'config.php';

$result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date 
                         FROM supplier_stats") or die(mysql_error());

while ($row = mysql_fetch_array($result))
    {
    $days = $row['expire_date'] - 1;
    if ($days > 0)
        {
        echo "<td><p>Insurance expires in <font color="red">{$row['expire_date']} day(s)!</font></p></td>";
        }
      else
        {
        $when = $days * -1;
        echo "<td><p>Insurance expires";
        if ($when > 1)
            {
            echo " in {$when} days</p></td>";
            }

        if ($when >= 8)
            {
            echo " <div class="green_light"></div>";
            }

        if ($when <= 7)
            {
            echo " <div class="red_light"></div>";
            }
        elseif ($when === 1)
            {
            echo " tomorrow</p></td>";
            }
        elseif ($when = 0)
            {
            echo " today</p></td>";
            }
        }
    }

?>

<?php
include 'config.php';

$result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date 
                         FROM supplier_stats") or die(mysql_error());

while ($row = mysql_fetch_array($result))
    {
    $days = $row['expire_date'] - 1;
    if ($days > 8)
        {
        echo "a is bigger than b";
        }
    }

echo "</table>"; //Close the table in HTML

?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need more or less somethin like this:

    <?php include 'config.php';
     $data = mysql_query("SELECT *, TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date FROM supplier_stats") 
     or die(mysql_error()); 

     echo "<table class="table" style="width:900px;  font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
     font-size:11px; color:#96969;" >";

     while($row = mysql_fetch_array( $data )) { 
       $days = $row['expire_date'] -1;

       echo "<tr><td><p>".$row['id'] . "</p></td>"; 
       echo "<td><p>".$row['company_name'] . "</p></td>"; 

       if ($days > 0) {
            echo "<td><p>Insurance expires in <font color="red">{$row['expire_date']} day(s)!</font></p></td>"; 
        } else {
          $when = $days*-1;           

          echo "<td><p>Insurance expires";

          if ($when > 1){
              echo " in {$when} days</p></td>";
          }

          if ($when >= 8){
            echo " <div class="green_light"></div>";
          }

          if ($when <= 7){
            echo " <div class="red_light"></div>";
          } elseif ($when ===1) {
            echo " tomorrow</p></td>";
          } elseif ($when == 0) {
            echo " today</p></td>";
          }
        }

        echo "<tr>";
      }

      echo "</table>"; //Close the table in HTML
    ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...