php loop table using do while

0
185
php loop table - table looper
php loop table - table looper

Here I used php class object to loop columns and rows through a table with php loop statements do while and for.

Moreover, this is main reason I’ve termed it TableLooper().

After all

you will notice that while looping the columns and rows I inserted contents dynamically into the cells.

Why TableLooper()

Table looper ease difficulties creating table with columns and rows and inserting contents into the cells at same time.

Uses of TableLooper()

Table looper manipulates a html table or a table embedded in javascript.

Below are the table looper most common features.

  • Automatically generates rows and columns based on numbers specified by the user.
  • Integrated an exlode php array content into the cells of the table.

Demonstration of TableLooper()

I used 17 rows and 5 columns for my practice and testing of this table looper.

You can specify any desired number of rows and columns.

Now, you can view, use, test and copy the code.

<?php
class TableLooper
{
	const table = '<table class="shopinson-table" id="shopinson_table">';
	const row_tag = '<tr>';
	const col_tag = '<td>';
	const looper= 1; // 
	public $col_content = null; //Emeka/Mmesoma/Lorita/Ikechukwu
	public $content_separator = null; //:;/.,
	public $col_break = null;
	public $counter = 0;
	public $contents = 0;
	public $row = null; //The Needed numbers of Row
	public $col = null; // The Needed numbers of Column
	public $loop_row= null; // This will loop the row
	public $loop_col= null; // This will loop the column
	public function __construct($number_of_row, $number_of_col, $column_content, $separator){
		$this->row = $number_of_row;
		$this->col = $number_of_col;
		$this->col_content = $column_content;
		$this->content_separator = $separator;
	}
	function TableLoop(){
		$this->col_content = explode($this->content_separator,$this->col_content);
		$this->col_break = count($this->col_content);
		$this->col_breaks = count($this->col_content);
		echo self::table;
		do{
		for($this->loop_row = self::looper;$this->loop_row<=$this->row;$this->loop_row++){ echo self::row_tag;
		for($this->loop_col = self::looper;$this->loop_col<=$this->col;$this->loop_col++){ 
		echo self::col_tag;
		if(empty($this->col_content[$this->counter++])){
		}else{
		echo $this->col_content[$this->contents++];
		}
		
		}
		
		}}while($this->counter <= $this->row);
	}
}

const rows = 17;//number of rows
const cols = 5;//number of columns
const column_contents = "Emeka/Mmesoma/Lorita/Ikechukwu"; //contents for each cell <td>
const content_separators = "/"; //specify content separator
$TableLooper = new TableLooper(rows,cols,column_contents,content_separators);
$TableLooper->TableLoop();
?>

But I added a little css code just to beatify the table not much just the border.

<style>
.shopinson-table{
	border-collapse:collapse;
	border:1px solid #000;
}
.shopinson-table td{
	border:1px solid #000;
	padding:5px;
}
</style>

End Remarks

Most importantly we are hoping to keep updating this code anytime.

Do you have any contribution or implementation idea to add to code?

Send us raven if you have any idea update us via comment or via contact us form

LEAVE A REPLY

Please enter your comment!
Please enter your name here