As in HTML-Table you need to code a ROW first then a second row AND you have a ROW but want to display in different rows VERTICALLY... So you need to store the rows in variables.
Let me show you how. Assume you have MySQL Row with 3 columns (as in your code), Then:
$row_1='<tr align="center">';
$row_2='<tr align="center">';
$row_3='<tr align="center">';
while ($row1 = mysql_fetch_array($hasil1))
{
$row_1.="<td>".$row1["tgl"]."</td>";
$row_2.="<td>".$row1["kom"]."</td>";
$row_3.="<td>".$row1["nilai"]."</td>";
}
echo $row_1."</tr>";
echo $row_1."</tr>";
echo $row_1."</tr>";
EDIT: AS OP need Pivot
[Reason of incorrectness for above answer:] If group by
is required then its not possible by above code (I m leaving it for future users) & PIVOT is not supported in FREE MySQL...
AND
By static queries you can not do that UNLESS values of tgl
are static/fixed. So you need to use dynamic query AND second option is to create stored procedure.
Both method are explained in this question: Pivot table in MySQL - convert to pivot table with values in varchar format
EDIT 2: Got the way to do it by PHP after analyzing the above link:
$result = mysql_query("SELECT DISTINCT tgl FROM ambilmk WHERE nim='1';");
$tgls_col="";
while($row = mysql_fetch_array($result)){
$tgls_col.=",MAX(CASE WHEN ambilmk.tgl = '".$row[0]."' THEN ambilmk.nilai END) `".$row[0]."`";
}
$query1 = "SELECT list.kom as Kom ".$tgls_col." FROM ambilmk LEFT JOIN list
ON ambilmk.kodemk=list.id_check where nim='1' GROUP BY ambilmk.tgl,list.kom;";
$hasil1 = mysql_query($query1);
$fields=mysql_fetch_fields($hasil1);
echo '<tr>';
foreach ($fields as $col) {
echo '<th>'.$col->name.'</th>';
}
echo '</tr>';
$fields=count($fields);
while($row=mysql_fetch_array($hasil1)){
echo '<tr>';
for($I=0;$I<$fields;I++){
echo "<td>".$row[$I].'</td>';
}
echo '</tr>';
}
HERE you can switch between list.kom
and ambilmk.nilai
AND can change aggregation function MAX
to any other you want like COUNT
etc. ALSO toggle the order of columns in GROUP BY caluse to get desire result. As your column names are in different language, that's why cant understands whats in those, so you need to do these suggested variations. AND ALSO NOT ABLE TO TEST AS DONT HAVE YOUR TABLE STRUCTURE.