Here is the code:
<?php
require'connection.php';
$cons = mysql_query( 'SELECT Subject FROM tblsubject' );
$total_rows = mysql_num_rows($cons);
$counter = 0;
$fields="";
while($row = mysql_fetch_array($cons)) {
if(++$counter == $total_rows) {
$fields .= $row['Subject']. ': { <br/> title: \''.$row['Subject'].'\',<br/> width: \'20% \' <br/>}<br/>';
}
else {
$fields = $row['Subject'].': { <br/> title: \''.$row["Subject"].'\',<br/> width: \'20% \' <br/>},<br/>';
}
}
echo $fields;
?>
The code stated above should produce an output like this below:
General: {
title:'General',
width: '20%',
},
Math: {
title:'Math',
width: '20%',
}
But if the query reaches its last row, the "," should be omitted after the last "}". I'm getting it right but when I echo it outside the while loop, only the two last row on the database are echoed.
Can someone help me with this? Please tell me where I am going wrong. Thank you very much :D I need to echo all the data in the table not just the final two.