php中单个数据库字段多列显示(单字段分页、横向输出)_php实例

WBOY
Release: 2016-06-07 17:17:08
Original
891 people have browsed it

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

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

第一次循环代码:

<tr>
<&#63;php
$rer=mysql_query(“select EI_EmployeeId,EI_EmployeeName from employeeinfo order by EI_EmployeeId asc limit 0,10″);
while($inf=mysql_fetch_array($rer)){
&#63;> 
<td>
<input type=”checkbox” name=”menuemployname” id=”menuemployname” value=”<&#63;php echo $inf['EI_EmployeeName']&#63;>”/><&#63;php echo $inf['EI_EmployeeName']&#63;>
</td>
<&#63;php }&#63;>
</tr>
Copy after login
之后循环代码:
<&#63;php
$rer=mysql_query(“select EI_EmployeeId,EI_EmployeeName from employeeinfo order by EI_EmployeeId asc”);
$num=mysql_num_rows($rer);
$i=0;$j=10;
$count=ceil($num/$j);
for($k=0;$k<$count;$k++){
$i=$i+$j;
&#63;> 
<tr>
<&#63;php
$rer=mysql_query(“select EI_EmployeeId,EI_EmployeeName from employeeinfo order by EI_EmployeeId asc limit $i,$j”);
while($inf=mysql_fetch_array($rer)){
&#63;> 
<td>
<input type=”checkbox” name=”menuemployname” id=”menuemployname” value=”<&#63;php echo $inf['EI_EmployeeName']&#63;>”/><&#63;php echo $inf['EI_EmployeeName']&#63;>
</td>
<&#63;php }&#63;>
</tr>
<&#63;php }&#63;>
Copy after login

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

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!