CodrGeek is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our Privacy Policy Explore Now!

How to design the bootstrap columns dynamically in php?

Bootstrap is a CSS framework to rapidly develop web applications without writing an element’s entire CSS and JavaScript. Developers can use the prebuilt grid layout, components, etc. in their projects.



In this article, we will see how to design the bootstrap columns in PHP dynamically..

Approach: We will store the data for each of the bootstrap columns in a PHP array and then loop over the array to generate all the columns dynamically.

Example 1: Below is a basic example to show how to dynamically generate bootstrap columns in PHP.


<!DOCTYPE html>
<html>

<head>
	<title>Bootstrap Columns Example</title>
	<!-- Include Bootstrap CSS -->
	<link rel="stylesheet" href=
"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
</head>

<body>
	<div class="container">
		<div class="my-4">
			<h1 class="text-success">CodrGeek</h1>
			<h5>Dynamically Generated Bootstrap Columns</h5>
		</div>
		<div class="row">
			<?php
				// Generate some sample data for the columns
				$columns = array(
					array(
						'size' => 6,
						'content' => 'Column 1'
					),
					array(
						'size' => 6,
						'content' => 'Column 2'
					),
					array(
						'size' => 4,
						'content' => 'Column 3'
					),
					array(
						'size' => 8,
						'content' => 'Column 4'
					)
				);

				// Iterate over the columns and generate the HTML
				foreach ($columns as $column) {
					// Get the size of the column (e.g. "6")
					$colSize = $column['size'];
					// Get the content of the column
					$colContent = $column['content'];
				?>
				<div class="col-<?php echo $colSize; ?>
					bg-primary p-5 border border-5 border-light">
					<?php echo $colContent; ?>
				</div>
			<?php } ?>
		</div>
	</div>
</body>
</html>

Example 2: In this example, we created a separate function that takes the size, content, and background color of the columns as the parameter and generates the HTML for the column.


<?php

	// Function to generate HTML for a Bootstrap column
	function generateColumn($size, $content, $bg)
	{
		$html = '<div class="col-' . $size .
		' bg-' . $bg . ' p-3 border border-5">';
		$html .= $content;
		$html .= '</div>';
		return $html;
	}
?>
<!DOCTYPE html>
<html>

<head>
	<title>Bootstrap Columns Example</title>
	<!-- Include Bootstrap CSS -->
	<link rel="stylesheet" href=
"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>

<body>
	<div class="container">
		<div class="my-4">
			<h1 class="text-success">CodrGeek</h1>
			<h5>Dynamically Generated Bootstrap Columns</h5>
		</div>

		<div class="row">
			<?php
			// Generate some sample data for the columns
			$columns = array(
				array(
					'size' => 6,
					'content' => 'Column 1',
					'background' => 'primary'
				),
				array(
					'size' => 6,
					'content' => 'Column 2',
					'background' => 'warning'
				),
				array(
					'size' => 4,
					'content' => 'Column 3',
					'background' => 'info'
				),
				array(
					'size' => 8,
					'content' => 'Column 4',
					'background' => 'success'
				)
			);

			// Iterate over the columns and generate the HTML
			foreach ($columns as $column) {
				// Get the size of the column (e.g. "6")
				$colSize = $column['size'];
				// Get the content of the column
				$colContent = $column['content'];
				// Get the background of the column
				$colBg = $column['background'];
				echo generateColumn($colSize, $colContent, $colBg);
			}
			?>
		</div>
	</div>
</body>
</html>


About the Author

Hey there! My name is Mayank, a professional Blogger, Graphic Designer, Coder as well as Content Creator from Uttar Pradeh, India. I love to Code and create interesting things while playing with it.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.