Table of Contents
Tutorial: How to Limit Number of Items per Row in While Loop using PHP/MySQLi Free Source Code
In this tutorial, I’m going to show you how to limit the number of item per row in while loop from MySQL Table using PHP/MySQLi. This tutorial is beneficial when you are presenting a large number of things, especially those that have images.
Creating a Database
The first step is to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “sample”.
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
CREATE TABLE `user` ( `userid` INT(11) NOT NULL AUTO_INCREMENT, `firstname` VARCHAR(30) NOT NULL, `lastname` VARCHAR(30) NOT NULL, PRIMARY KEY(`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Inserting Sample Data into a Database
Next is to insert sample data into the database that we have created. We are going to use this when we select our data.
1. Click the “sample” database that we have created.
2. Click SQL and paste the code below.
INSERT INTO `user` (`userid`, `firstname`, `lastname`) VALUES (1, 'neovic', 'devierte'), (2, 'lee', 'ann'), (3, 'julyn', 'divinagracia'), (4, 'tintin', 'demapanag'), (5, 'dee', 'tolentino'), (6, 'jaira', 'jacinto'), (7, 'jason', 'flor'), (8, 'queen', 'melicor'), (9, 'joy', 'rael'), (10, 'faith', 'rael'), (11, 'roy', 'butron'), (12, 'leah', 'flores'), (13, 'jub', 'limsiaco'), (14, 'janna', 'atienza'), (15, 'desire', 'osorio'), (16, 'rox', 'infante');
Creating a Sample Table
Last step is to create our sample table with or limiting code. Also, for you to understand the code faster, I’ve created 2 sample tables.
<!DOCTYPE html> <html> <head> <title>How to Limit Number of Items per Row from MySQL Table in PHP/MySQLi</title> </head> <body> <h2>Limit By 4 per row</h2> <table border=1> <thead> <th>Name</th> <th>Name</th> <th>Name</th> <th>Name</th> </thead> <tbody> <?php $conn = mysqli_connect("localhost", "root", "", "sample"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $inc=4; $query=mysqli_query($conn,"select * from user"); while($row=mysqli_fetch_array($query)){ $inc = ($inc == 4) ? 1 : $inc+1; if($inc == 1) echo "<tr>"; ?> <td><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></td> <?php if($inc == 4) echo "</tr>"; } if($inc == 1) echo "<td></td><td></td><td></td></tr>"; if($inc == 2) echo "<td></td><td></td></tr>"; if($inc == 3) echo "<td></td></tr>"; ?> </tbody> </table> <h2>Limit By 3 per row</h2> <table border=1> <thead> <th>Name</th> <th>Name</th> <th>Name</th> </thead> <tbody> <?php $conn = mysqli_connect("localhost", "root", "", "sample"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $inc=3; $query=mysqli_query($conn,"select * from user"); while($row=mysqli_fetch_array($query)){ $inc = ($inc == 3) ? 1 : $inc+1; if($inc == 1) echo "<tr>"; ?> <td><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></td> <?php if($inc == 3) echo "</tr>"; } if($inc == 1) echo "<td></td><td></td></tr>"; if($inc == 2) echo "<td></td></tr>"; ?> </tbody> </table> </body> </html>
Output
And that ends our tutorial. Hoping that this might be useful to someone. Happy coding!
Download Here