Generate Html Table From Array

Description

Generates the html table as output from the array provided.

Many times, we need to display the select query result in the html table format. This function does same. Only 2-3 lines of code and you are done. You can also pass css class for table and tr.

Function Reference :

            	string getHtmlTableFromArray(string $input_array [,string $table_css_class="sdb_tbl_cls"] [, string $tr_css_class="sdb_tr_cls"])
            

Parameters

Name Type Required Parameter Info Example
input_array Associative array Yes Associative array to be converted into html table Pass array, convert in table form e.g. array('result'=>$result,'object'=>$object,'name'=>$name)
table_css_class string Optional Css class for table The name of css class for table, default is 'sdb_tbl_cls'
tr_css_class string Optional Css class for tr The name of css class for table row, default is 'sdb_tr_cls'

Examples

 
	        <?php 
            $sdb= new SimplifiedDB(); 
            $sdb->dbConnect("localhost","username","password","dbuser");
            
            // get associative array directly from database using select operation
            $input_array=$sdb->dbSelect("users");
            echo $sdb->getHtmlTableFromArray($input_array); 
            ?> 
           

Result

Generate html table from the associative array.
userId username password firstName lastName role_id email gender dob website city state country pincode phoneno faxno comments user_reg_date
1 Ram sdf123 Ram Yadav 10 y.ram11@gmail.com Male 1986-09-11 http://www.google.com Indore MP India 2312 99229922 90900 comments about user 1985-09-11
2 Prites sdfsfds213 Pritesh Gupta 10 y.ram11@gmail.com Male 1985-09-11 http://www.google.com Indore MP India 2312 99229922 90900 comments about user 1985-09-11
3 Mike lkosd12 Mike 10 y.ram11@gmail.com Male 1985-09-11 http://www.google.com Indore MP India 2312 99229922 90900 comments about user 1985-09-11
4 Sachine dfsda Sachine 10 y.ram11@gmail.com Male 1975-09-11 http://www.google.com Mumbai MH India 2312 99229922 90900 comments about user 1985-09-11
5 David asdf da vid 10 y.ram11@gmail.com Male 1955-09-11 http://www.google.com Mumbai MH India 2312 99229922 90900 comments about user 1985-09-11
6 helena asdf da vid 10 y.ram11@gmail.com Female 1955-09-11 http://www.google.com Mumbai MH India 2312 99229922 90900 comments about user 1985-09-11
7 priya asdf da vid 8 p.riya@gmail.com Female 1955-09-11 http://www.google.com Mumbai MH India 2312 99229922 90900 comments about user 1985-09-11