php - Retrieving data to HTML table from SQL database -
i'm trying data html table database. in database there's company table consists of id, name , percentage columns.
the above data in table entered me manually. need fill same values retrieving database.
and company table in database looks this.database table
in coding it's getting error in place of array formation. tried names in 1 array , print in html table. i'm kindly requesting have @ coding. highly appreciated.
$sql = "select name, percentage company"; $result = $conn->query($sql); $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row= $result->fetch_assoc()) { $arr=str_split($row) //echo $row; echo "<table> <tr> <td> $arr[1] </td> </tr> </table> "; } } else { echo "0 results"; }
@jakumi error enter image description here. line 18 '$arr=str_split($row);'.
str_split
expects string, $row
array. access fields of each row $row['name']
example.
so
echo '<table>'; while($row= $result->fetch_assoc()) { echo '<tr><td>'.$arr['name'].'</td><td>'.$arr['percentage'].'</td></tr>'; } echo '</table>';
updated answering specific comment:
$percentages = []; $namestopercentages = []; $collection = []; while($row = $result->fetch_assoc()) { $percentages[] = $row['percentage']; // produces [0 => 0, 1=>1.1, 2=>1.2, ...] $namestopercentages[$row['name']] = $row['percentage']; // produces ['base_price' => 0, 'a' => 1.1, ...] $collection[]= $row; // produces [['name' => 'base_price', 'percentage'=>0], [...],...] }
Comments
Post a Comment