在数据表中以两列显示数据

发布于 2024-12-07 17:57:56 字数 1568 浏览 0 评论 0原文

我已设法在一列表中显示数据,但希望有两列。有办法做到吗?这是我目前拥有的代码。虽然它可以工作,但它会打印一长列,并希望将其分成两列。

如您所知,我正在使用 jQuery Datatable。

<?php
include('config.php');
mysql_connect($host, $username, $password) or die(mysql_error()) ;
mysql_select_db('people') or die(mysql_error()) ;

$data = mysql_query("SELECT * FROM names ORDER BY RAND() LIMIT 20") or die(mysql_error());
?>
<html>
<head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="datatables/dt/media/js/jquery.dataTables.js">
    </script>
    <style type="text/css">
      /* @import "datatables/dt/media/css/demo_table.css";

     .result_container{
       width: 553;
      } */
      </style>
    <script>
      $(document).ready(function(){
        $('#the_table').dataTable();
      });
    </script>

</head>

<body>

 <?php

    echo "<table id=\"the_table\">
                     <thead>
                            <tr>
                                <th>Latest names</th>

                            </tr>

                      </thead>

                      <tbody> ";

        while($info = mysql_fetch_array( $data )){

           echo"<tr> <td>" . $info['name'] . "</td> 
                </tr>";                     

            }
            echo" </tbody> ";
      echo "</table> ";
?>
     </body>
</html>  

任何帮助将非常感激。

I have managed to display data in one column table, but would like to have two column instead. Is there a way of doing it? Here's the code I currently have. Although, it works, it prints in one long column and would like to break it into two columns.

As you can tell, I'm using jQuery Datatable.

<?php
include('config.php');
mysql_connect($host, $username, $password) or die(mysql_error()) ;
mysql_select_db('people') or die(mysql_error()) ;

$data = mysql_query("SELECT * FROM names ORDER BY RAND() LIMIT 20") or die(mysql_error());
?>
<html>
<head>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script src="datatables/dt/media/js/jquery.dataTables.js">
    </script>
    <style type="text/css">
      /* @import "datatables/dt/media/css/demo_table.css";

     .result_container{
       width: 553;
      } */
      </style>
    <script>
      $(document).ready(function(){
        $('#the_table').dataTable();
      });
    </script>

</head>

<body>

 <?php

    echo "<table id=\"the_table\">
                     <thead>
                            <tr>
                                <th>Latest names</th>

                            </tr>

                      </thead>

                      <tbody> ";

        while($info = mysql_fetch_array( $data )){

           echo"<tr> <td>" . $info['name'] . "</td> 
                </tr>";                     

            }
            echo" </tbody> ";
      echo "</table> ";
?>
     </body>
</html>  

Any help will be appreciated very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2024-12-14 17:57:56
echo "<table id=\"the_table\">
      <thead>
         <tr>
      <th>Latest names</th>
    </tr>
      </thead>

      <tbody>

      <tr>"; 


while($info = mysql_fetch_array( $data )){


// if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row:  
if( ($c % 2) == 0 && $c != 0){
echo "</tr><tr>";
}


echo "<td>" . $info['name'] . "</td>";

$c++; 

} // while..


// in case you need to fill a last empty cell:
if ( ( $i % 2 ) != 0 ){

// str_repeat() will be handy when you want more than 2 columns
  echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
}


echo "</tr>
      </tbody>
      </table>";

?>

echo "<table id=\"the_table\">
      <thead>
         <tr>
      <th>Latest names</th>
    </tr>
      </thead>

      <tbody>

      <tr>"; 


while($info = mysql_fetch_array( $data )){


// if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row:  
if( ($c % 2) == 0 && $c != 0){
echo "</tr><tr>";
}


echo "<td>" . $info['name'] . "</td>";

$c++; 

} // while..


// in case you need to fill a last empty cell:
if ( ( $i % 2 ) != 0 ){

// str_repeat() will be handy when you want more than 2 columns
  echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
}


echo "</tr>
      </tbody>
      </table>";

?>

递刀给你 2024-12-14 17:57:56

只需在 HTML 中添加另一个单元格?

<?php

    echo "<table id=\"the_table\">
                     <thead>
                            <tr>
                                <th>Latest names</th>
                                <th>ANOTHER_FIELD</th>
                            </tr>
                      </thead>
                      <tbody> ";
        while($info = mysql_fetch_array( $data )){

           echo"<tr> <td>" . $info['name'] . "</td><td>".$info['ANOTHER_FIELD']." 
                </tr>";                     

            }
            echo" </tbody> ";
      echo "</table> ";
?>

Just add another cell in HTML?

<?php

    echo "<table id=\"the_table\">
                     <thead>
                            <tr>
                                <th>Latest names</th>
                                <th>ANOTHER_FIELD</th>
                            </tr>
                      </thead>
                      <tbody> ";
        while($info = mysql_fetch_array( $data )){

           echo"<tr> <td>" . $info['name'] . "</td><td>".$info['ANOTHER_FIELD']." 
                </tr>";                     

            }
            echo" </tbody> ";
      echo "</table> ";
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文