在表格中格式化PHP Echo

时间:2013-11-24 20:08:15

标签: php echo

我的网站中有一些代码可以从MySQL数据库中检索数据。

   <?php echo "<b><h6 id='lp2'><div id='rpc'> $channel_name</div></h6></b><p />";?>

另外,我有这段代码:

<body style="overflow-y:hidden; overflow-x:scroll">

我想要做的是,让所有回声帖子显示在彼此的顶部,分为2行。有点像桌子。

Post   Post   Post   ...
Post   Post   Post   ...

我如何用这种方式设置MySQL输出的样式以进行回声?

1 个答案:

答案 0 :(得分:0)

你可以这样试试:

<html>
  <head>
    <style type="text/css">
      .items {
        display: inline; /* puts all on the same line */
        float: left; /* keeps putting the items to the left */
      }
      .reset {
        clear: both; /* resets the 'float: left', thus starting a new line */
      }
    </style>
  </head>
  <body>
    <?php
      $link = mysqli_connect(); // enter connection code here
      $query = "select `channel_name` from `your_table` ..."; // enter query here
      $result = mysqli_query($link, $query); // execute query
      $num_rows = mysqli_num_rows($result); // count rows
      $i = 0; // start counting to see if we have passed half way
      while($obj = mysqli_fetch_object($result)) { // for each row ...
        $channel_name = htmlspecialchars($obj->channel_name); // escape results
        echo '<div class="items">' . $channel_name . '</div>'; // output values
        if (++$i > $num_rows / 2) { // if we passed half way ...
          echo '<div class="reset"></div>'; // finish first row
          $i = -1; // and prevent entering this if again
        }
      }
      echo '<div class="reset"></div>'; // finish second row
      mysqli_free_result($result); // free results from memory
      mysqli_close($link); // close db connection
    ?>
  </body>
</html>