CREATE TABLE IF NOT EXISTS `checkbox` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, `price` int(11) NOT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
<?php /* Database config */ $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_database = 'tutorial'; /* End config */ $db = new PDO('mysql:host='.$db_host.';dbname='.$db_database, $db_user, $db_pass); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); ?>
<table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4"> <thead> <tr> <th> Name </th> <th> Price </th> </tr> </thead> <tbody> <?php include('connect.php'); $result = $db->prepare("SELECT * FROM checkbox"); $result->execute(); for($i=0; $row = $result->fetch(); $i++){ ?> <tr class="record"> <td><?php echo $row['name']; ?></td> <td><?php echo $row['price']; ?></td> </tr> <?php } ?> </tbody> <thead> <tr> <th> Total </th> <th> <?php $results = $db->prepare("SELECT sum(price) FROM checkbox"); $results->execute(); for($i=0; $rows = $results->fetch(); $i++){ echo $rows['sum(price)']; } ?> </th> </tr> </thead> </table>