dongmeba4877 2013-11-24 20:08
浏览 48
已采纳

在表格中格式化PHP Echo

I have some coding in my website that retrieves data from a MySQL database.

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

Also, I have this code:

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

What I want to do is, make all the echo'd posts be displayed on top of each other, in 2 rows. Kinda like a table.

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

How would I style the MySQL output this way for echoing?

  • 写回答

1条回答 默认 最新

  • dongzheng4556 2013-11-24 21:12
    关注

    You could try it like this:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?