Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Datatable中以两列显示数据_Php_Mysql_Datatable - Fatal编程技术网

Php 在Datatable中以两列显示数据

Php 在Datatable中以两列显示数据,php,mysql,datatable,Php,Mysql,Datatable,我已设法在一列表中显示数据,但希望改为使用两列。有办法吗?这是我目前拥有的代码。虽然它可以工作,但它打印在一个长列中,并希望将其分为两列 正如您所知,我使用的是jquerydatatable <?php include('config.php'); mysql_connect($host, $username, $password) or die(mysql_error()) ; mysql_select_db('people') or die(mysql_error()) ; $dat

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

正如您所知,我使用的是jquerydatatable

<?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>  

/*@import“datatables/dt/media/css/demo_table.css”;
.result_容器{
宽度:553;
} */
$(文档).ready(函数(){
$(“#the#u table”).dataTable();
});

非常感谢您的帮助。

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

echo”
最新名称
"; 
而($info=mysql\u fetch\u数组($data)){
//如果在2次迭代后(对于2列)余数为零,并且$c>0时,结束行并开始新行:
如果(($c%2)==0&&$c!=0){
回声“;
}
回显“$info['name']”;
$c++;
}//而。。
//如果您需要填写最后一个空单元格:
如果($i%2)!=0){
//str_repeat()在需要超过2列时非常方便
回声stru重复(“,(2-($i%2));
}
回声“
";

?>

谢谢你的回答,但我不想再添加一个字段。我只想在两列中打印姓名。动态地从表中提取名称,并交替地在单独的列中打印每个名称。感谢示例代码。尽管列显示良好,但由于某些原因,搜索框和分页不会显示。将继续调试。我必须接受这个答案,因为它是唯一一个接近我所追求的答案。再次感谢您的帮助。
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>";