Categories
php

PHP foreach loop in HTML table – working

This is how I got PHP foreach loop working in HTML table. You can find the working code below. Get all the code for PHP Connect to MySQL Database, using mysqli api.

PHP foreach Loop

PHP foreach loop iterates through the array, associative array, MySQL table, and also lets run arithmetic operators.

php foreach array iteration

$arr = array(1, 2, 3, 4);

foreach ($arr as $value) {
    echo $value;
}

// 1, 2, 3, 4

php foreach array iteration with arithmetic expression

$arr = array(1, 2, 3, 4);

foreach ($arr as &$value) {
    $value = $value * 2;
}

// 2, 4, 6, 8

php foreach associative array iteration


$arr1 = array(
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
);

foreach ($arr1 as $key => $value) {
   echo "$key => $value "."<br>";
}

// echo output
// one => 1
// two => 2
// three => 3
// seventeen => 17

PHP foreach Loop in HTML table

<table> 
<thead>
	<tr>
	 <th>ID</th>
	 <th>Brand</th>
         <th>Model</th>
	 <th>Year</th>
         <th>Category</th>
	 <th>Gender</th>
	 <th>Color</th>
         <th>Weight KG</th>
	</tr>
</thead>

<tbody>
       <tr>
       // PHP foreach Loop
	 <?php foreach ($bicycles as $bicycle){ ?>
	 <td> <?php echo $bicycle['id']; ?> </td>
	 <td> <?php echo $bicycle['brand']; ?> </td>
	 <td> <?php echo $bicycle['model']; ?> </td>
	 <td> <?php echo $bicycle['year']; ?> </td>
	 <td> <?php echo $bicycle['category']; ?> </td>
	 <td> <?php echo $bicycle['gender']; ?> </td>
	 <td> <?php echo $bicycle['color']; ?> </td>
	 <td> <?php echo $bicycle['weight_kg']; ?> </td> 
      </tr>
	<?php } ?>
</tbody>
</table>

PHP Connect to MySQL Database

MySQL Database Connection, Query, & php foreach loop

<?php 
// SETP ONE
// // database credentials
$hostname = 'localhost';
$dbname = 'databse_name';
$dbuser = 'user_name';
$dbpass = 'user_password';


// SETP TWO : connecting to the database
$mysqli = new mysqli($hostname, $dbuser, $dbpass, $dbname);

// SETP THREE : SQL query
$bicycles = $mysqli->query('SELECT * FROM bicycles');

 ?>

// SETP FOUR : Displaying table values through HTML Table
<table> 
<thead>
	<tr>
	 <th>ID</th>
	 <th>Brand</th>
         <th>Model</th>
	 <th>Year</th>
         <th>Category</th>
	 <th>Gender</th>
	 <th>Color</th>
         <th>Weight KG</th>
	</tr>
</thead>

<tbody>
       <tr>
	 <?php foreach ($bicycles as $bicycle){ ?>
	 <td> <?php echo $bicycle['id']; ?> </td>
	 <td> <?php echo $bicycle['brand']; ?> </td>
	 <td> <?php echo $bicycle['model']; ?> </td>
	 <td> <?php echo $bicycle['year']; ?> </td>
	 <td> <?php echo $bicycle['category']; ?> </td>
	 <td> <?php echo $bicycle['gender']; ?> </td>
	 <td> <?php echo $bicycle['color']; ?> </td>
	 <td> <?php echo $bicycle['weight_kg']; ?> </td> 
      </tr>
	<?php } ?>
</tbody>
</table>

By Tube Mint

I am a YouTuber and I also do niche marketing. In the process of create my niche blog using WordPress, Drupal, and GatsbyJS, I learned a few things like html, css, JS, CMS, MVC frameworks, C++, Python, Ruby, MySQL, Windows and Linux. After some time.. I realized that I can share my knowledge with people on YouTube and through my blog. So, here I am. You can ask me if you have any question about these technologies.
Thanks,
@TubeMint