m127
Attack Surface Analyzer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
This tutorial will teach you on how to Compute or Autosum the price of all products in database table using sum function in PHP. To understand more lets follow the steps bellow.
Creating Our Database
First we are going to create our database which stores our data.
To create a database:
1. Open phpmyadmin
2. Then create database and name it as "tutorial".
3. After creating a database name, click the SQL and paste the following code.
Creating our Database Connection
Next step is to create a database connection and save it as "connect.php". In this Step, we will write our connection script in PDO format.
Creating Our Display Autosum Script
The code bellow will retrieve data from our database table.
This also include our script that get the total of all price in database table using php sum function, copy the code bellow and save it as "index.php".
Hope this tutorial will help you.
Download
Creating Our Database
First we are going to create our database which stores our data.
To create a database:
1. Open phpmyadmin
2. Then create database and name it as "tutorial".
3. After creating a database name, click the SQL and paste the following code.
- CREATE TABLE IF
NOT EXISTS `checkbox` (
- `id` int(
11
)
NOT NULL
AUTO_INCREMENT,
- `name` varchar(
100
)
NOT NULL
,
- `price` int(
11
)
NOT NULL
,
- PRIMARY KEY
(
`id`)
- )
ENGINE=
InnoDB DEFAULT
CHARSET=
latin1 AUTO_INCREMENT=
11
;
Creating our Database Connection
Next step is to create a database connection and save it as "connect.php". In this Step, we will write our connection script in PDO format.
- <?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
)
;
- ?>
Creating Our Display Autosum Script
The code bellow will retrieve data from our database table.
This also include our script that get the total of all price in database table using php sum function, copy the code bellow and save it as "index.php".
- <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>
Hope this tutorial will help you.
Download
You must upgrade your account or reply in the thread to view the hidden content.