在Datatable中的两列中显示数据

时间:2011-10-03 21:37:18

标签: php mysql datatable

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

正如您所知,我正在使用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>  

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

只需在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> ";
?>

答案 1 :(得分:0)

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>&nbsp;</td>", ( 2 - ( $i % 2 ) ) );
}


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

&GT;