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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀