Home > Article > Backend Development > Display a single database field in multiple columns in PHP (single field paging, horizontal output), multi-column paging_PHP tutorial
When I was working on a project today, I encountered a problem, which was to display the same field read from the database in rows and columns, that is, each row displays 12 columns, and the number of looping rows is controlled based on the total number of records. If it is multiple fields, it is easy to implement, and it can be done with one loop. If it is a field loop, it will be more troublesome. You need to use multiple loops and incremental variables at the same time. There are also many Phpers on the Internet who encounter similar problems. Today I will introduce my own Share the solution with everyone.
For looping multiple rows of the same field and controlling the number of columns displayed, the implementation principle is to first use Limit to limit the reading of the first loop, and then add the number of records read in the first loop to the number of columns to be displayed in each row. . Attached is the code directly below:
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>
<?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, which is to cycle through the first cycle multiple times. You only need to change the first parameter of Limit. Hope it helps for beginners in phper.
I suggest you use the time() function to save the seconds. Just change the data table field type to int(10). When outputting, you can directly use date('Y-m-d',db[' time']) formatted output!
// The database connection part is omitted
$q = mysql_query("select User,Tel from darkit.Users limit 10", $lnk);
while($rs = mysql_fetch_assoc($q)){
echo $rs['User'], " : ", $rs['Tel'], "df250b2156c434f3390392d09b1c9563";
}
///////