Dynamically generate html table with php of mysql records -
the reason complicated (for me) each column of table loaded separate mysql table, , each mysql table have varying number of records. thought start generating html table top-left bottom-right column column , cell cell, won't work because each mysql table have different length of records, generate malformed html tables. have suggestions?
my idea far:
- get list of tables in mysql, determine number of columns
- get count table records
- create table parameters (# of tables columns, max# rows
- update each cell corresponding record, not quite sure how
as requested, code:
$tables = mysql_query("show tables"); $output = "<table border=1><thead><tr>"; while($table = mysql_fetch_array($tables)) { $output .= "<td>"; $output .= $table[0]; $output .= "</td>"; $tablenames[] = $table[0]; } $output .= "</tr></thead>"; $output .= "<tbody>"; //get count of table records for($i=0; $i<count($tablenames); $i++ ){ $currenttable = $tablenames[$i]; $tablecontent = mysql_query("select * $currenttable") or die("error: ".mysql_error()); //generating content column $output .= "<tr>"; while($content = mysql_fetch_array($tablecontent)){ //generating cell in column $output .= "<td>"; $output .= "<strong>".$content['subtheme'].": </strong>"; $output .= $content['content']; $output .= "</td>"; } $output .= "</tr>"; } $output .= "</tbody>"; $output .= "</table>";
this wrong not because generates malformed table, because transposed columns rows...
any appreciated
solution hated question:
$mymax = 0; for($i=0; $i<count($tablenames); $i++){ $currenttable = $tablenames[$i]; $tablecounts = "select * $currenttable"; if($stmt = $mysqli->prepare($tablecounts)){ mysqli_stmt_execute($stmt); mysqli_stmt_store_result($stmt); $count = mysqli_stmt_num_rows($stmt); mysqli_stmt_close($stmt); } ($mymax >= $count ? "" : $mymax = $count); $colwidth = 100 / count($tablenames); } // div grid // via div generation $output .= "<div class='grid'>"; ($i=0; $i<count($tablenames); $i++){ $output .= "<div id='col$i' class='col' style=\"width:$colwidth%\">"; $output .= "<h3>".$tablenames[$i]."</h3>"; $tableqry = "select * $tablenames[$i]"; if ($result = mysqli_query($mysqli, $tableqry)) { while($row = mysqli_fetch_array($result, mysqli_assoc)){ $output .= "<div class='item'>".$row["content"]."</div>"; } mysqli_free_result($result); } $output .= "</div>"; } $output .="</div>"; $output .="<div class='clear'></div>";
Comments
Post a Comment