PHP Function – Generate Table Using MySQL DB


php-logo1
Generat Table menggunakan MySQL Database dalam PHP Function, berikut scriptnya:

* Function:

function maketable($query, $fieldarray){
//count number of columns
 $columns = count($fieldarray);
//run the query
 $result = mysql_query($query) or die(mysql_error()) ;
 $itemnum = mysql_num_rows($result);
 if($itemnum > 0){
  do{  
   echo "< tr >" ;
   for($x = 0; $x < $columns; $x++){
    echo "< td >" .$items[$fieldarray[$x]]. "< /td >" ;
   }
   echo "< /tr >" ;
  }while($items = mysql_fetch_assoc($result));
 }
}

* Penggunaan:

echo "< table >";
$fieldarray = array("id","title","description");
maketable("SELECT * FROM bw_news", $fieldarray);
echo "< /table >";

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s