Multi column display of single database field in php (single field paging horizontal output)

  • 2021-07-10 19:01:34
  • OfStack

Today, when I was doing the project, I encountered a problem that I displayed the same 1 field read from the database separately, that is, 12 columns were displayed in each row, and the number of circular rows was controlled according to the total number of records. If it is a multi-field very good realization, a loop, if it is a field loop, then more troublesome, need to use multiple loops and incremental variables, there are many Phper on the Internet encountered similar problems, today this site to share their own solutions.

For the same 1 field cycle multi-row and control column display number, the realization principle is to first use Limit to limit the first cycle read, and then take the first cycle read the number of records plus the number of columns to be displayed per row. The code is attached directly below:

The first loop code:


<tr>
<?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)){
?> 
<td>
<input type= " checkbox "  name= " menuemployname "  id= " menuemployname "  value= " <?php echo $inf['EI_EmployeeName']?> " /><?php echo $inf['EI_EmployeeName']?>
</td>
<?php }?>
</tr>
Then loop the code:

<?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;
?> 
<tr>
<?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)){
?> 
<td>
<input type= " checkbox "  name= " menuemployname "  id= " menuemployname "  value= " <?php echo $inf['EI_EmployeeName']?> " /><?php echo $inf['EI_EmployeeName']?>
</td>
<?php }?>
</tr>
<?php }?>

Of course, there is a more direct method, that is, to loop the first loop many times, only need to change the first parameter of Limit. I hope it will be helpful for beginners phper.


Related articles: