Tuesday, May 20, 2008

display the table column dynamically using php

<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("country") or die(mysql_error());
$query_cat = "SELECT * FROM countries"; 

    $cat = mysql_query($query_cat) or die(mysql_error());
    $total = mysql_num_rows($cat);

    while($row = mysql_fetch_array($cat))
    {
// -- This array will hold your data,
// -- I used it to hold a title, an id and an author
// -- but it can be extended based on your own needs
        $items[] = array(0 => $row['country_id'], 1 => $row['countryname']);
    }
     

     
echo "<table>\n";
for($i=0;$i<=count($items);$i++){
    if( $i % 5 === 0 ) { //its for display the 5 colums per row
                       ?><tr><?
                       }
                        echo "<td>".$items[$i][0]."</td>";

    if( $i % 4 === 4){//its for display the 5 colums per row
                      ?><tr><?
                    }
   
}
echo "<table>";

?>

No comments:

Post a Comment