Home > php教程 > php手册 > php中单个数据库字段多列显示

php中单个数据库字段多列显示

WBOY
Release: 2016-06-13 09:27:54
Original
927 people have browsed it

php中单个数据库字段多列显示

 这篇文章主要介绍了php中单个数据库字段多列显示、分行分列显示技巧,也可称为单字段分页、横向输出,需要的朋友可以参考下

 

 

 

今天在做项目时,遇到个问题就是把从数据库读出来的同一字段分行分列显示,就是每行显示12列,根据总记录数控制循环行数。如果是多字段很好实现,一个循环搞定,如果是一个字段循环那么就比较麻烦了,需要同时用到多个循环还有递增变量,网上也有很多Phper遇到相似的问题,今天小编把自己的解决方案分享大家。

对于同一字段循环多行和控制列显示个数,实现原理就是先用Limit限制读出第一次循环,然后拿第一次循环读出的记录数加上要每行显示的列数。下面直接附上代码:

第一次循环代码:

<tr>
<?php
$rer=mysql_query(&ldquo;select EI_EmployeeId
,EI_EmployeeName from employeeinfo order by EI_EmployeeId asc limit 0,10&Prime;);
while($inf=mysql_fetch_array($rer)){
?> 
<td>
<input type=&rdquo;checkbox&rdquo; name=&rdquo;menuemployname&rdquo; id=&rdquo;menuemployname&rdquo; value=&rdquo;
<?php echo $inf['EI_EmployeeName']?>&rdquo;/>
<?php echo $inf['EI_EmployeeName']?>
</td>
<?php }?>
</tr>
Copy after login

之后循环代码:

<?php
$rer=mysql_query(&ldquo;select EI_EmployeeId,EI_EmployeeName from employeeinfo order by EI_EmployeeId asc&rdquo;);
$num=mysql_num_rows($rer);
$i=0;$j=10;
$count=ceil($num/$j);
for($k=0;$k<$count;$k++){
$i=$i+$j;
?> 
<tr>
<?php
$rer=mysql_query(&ldquo;select EI_EmployeeId,
EI_EmployeeName from employeeinfo order by EI_EmployeeId asc limit $i,$j&rdquo;);
while($inf=mysql_fetch_array($rer)){
?> 
<td>
<input type=&rdquo;checkbox&rdquo; name=&rdquo;menuemployname&rdquo; id=&rdquo;menuemployname&rdquo; value=&rdquo;
<?php echo $inf['EI_EmployeeName']?>&rdquo;/>
<?php echo $inf['EI_EmployeeName']?>
</td>
<?php }?>
</tr>
<?php }?>
Copy after login

当然还有更直接的方法,就是多次循环第一次循环,只需要改动Limit的第一个参数即可。希望对于初学者phper有所帮助。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template