So I'm quite new to PHP and SQL. I'm trying to get values from my SQL table and add them together and then display it in HTML. It's a basic inventory system where after an entry is made, it shows how many entries are there in total. I don't really know if PHP or SQL is the right code to use. I've tried searching around for an answer but I can't really find anything on it. I tried something on my own, but I seriously doubt this is far from what I'm trying to achieve. If there is a website or another link that is answering the same question, please let me know. Here is my code.
<div class="card-content">
<p class="category">Sales</p>
<h3 class="title">
<?php
db_connect();
$sumsale = mysql_query("SELECT SUM(price) FROM sale");
print ($sumsale);
?>
</h3>
</div>
Any help would be greatly appreciated it!
An example of what I want is the following in HTML. Now the table is an SQL table within a database.:
<table>
<tr>
<th>ID</th>
<th>Type</th>
<th>Price Sold At</th>
<th>Quantity</th>
</tr>
<tr>
<td>1</td>
<td>Type A</td>
<td>10</td>
<td>3</td>
</tr>
<tr>
<td>2</td>
<td>Type B</td>
<td>15</td>
<td>5</td>
</tr>
<tr>
<td>3</td>
<td>Type C</td>
<td>12</td>
<td>9</td>
</tr>
<tr>
<td>4</td>
<td>Type D</td>
<td>13</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>Type E</td>
<td>11</td>
<td>5</td>
</tr>
</table>
Result. The <p>
tags are the same, whereas, the <h3>
tags will have the code that adds the values together.:
<p>Revenue</p>
<h3>61.00</h3>
<p>Number of entires</p>
<h3>5</h3>
Code that prints SQL table to HTML:
function createTable(array $results = array())
{
if (empty($results)) {
return '<table class="table" id="export"><tr><td>No inventory yet.</td></tr></table>';
}
$table = '<table class="table" id="export">';
$keys = array_keys(reset($results));
$table.='<thead class="text-success"><tr>';
foreach ($keys as $key) {
$table.='<th>'.$key.'</th>';
}
$table.='</tr></thead>';
$table.='<tbody>';
foreach ($results as $result) {
$table.='<tr>';
foreach ($result as $val) {
$table.='<td>'.$val.'</td>';
}
$table.='</tr>';
}
$table.='</tbody></table>';
return $table;
}
HTML:
<div class="card-content table-responsive">
<?php
db_connect();
$result = mysql_query("SELECT * FROM inventory ORDER BY 1 ASC LIMIT 1000");
print createTable(array_result($result));
?>
</div>