PHP Tutorial on Record Processing

PHP Tutorial on Record Processing thumbnail
Use PHP code to process running totals of records.

PHP can be used to process data for business applications such as inventory control programs and online cash registers. For these applications, the PHP code calculates a running total of the current inventory or a running total of new purchases. The PHP program first must be coded to retrieve the records and fields from the database that contain the needed data. Once that information is retrieved, it takes just a few lines of PHP code to calculate totals such as running totals.

Things You'll Need

  • MySQL database table with primary index field and count field with at least three records with data
Show More

Instructions

    • 1

      Create an online database with a database table that you will access records from. Create this table so that it has an primary key index field and an integer field named "count." Name the table "test8." Use a MySQL relational database management system that will allow PHP functions to use MySQL statements to access the database.

    • 2

      Create a text file and name it "running_total.php" to type your code into. Type on the first line of the text editor the mysql connection statement that will connect you to the test8 database table that you created.

    • 3

      Type, starting at next line of the text editor, the PHP code that will be used to form a query that will be sent to the test8 database table. Code the query so that it will select all the records you entered into the test8 table, display each of the counts entered and the running total of the counts.

      $sql4 = "SELECT * FROM test8";

      $res4 = mysqli_query($mysqli, $sql4);

      if ($res4) {

      while ($newArray = mysqli_fetch_array($res4, MYSQLI_ASSOC)) {

      $item1 = $newArray['count'];

      $total = $total + $item1;

      echo "The count is ".$item1."<br/>";

      echo "The running total is ".$total."<br/>";

      };

      };

      The code assigns the MySQL query "SELECT * FROM test8" to the PHP variable named $sql4. The PHP mysqli_query statement uses this variable and the connection variable, $mysqli, to retrieve all the records in the test8 database table and place them in a PHP variable named $res4. The PHP mysqli_fetch_array function is used with the "while" statement to loop through the table stored in $res4 and place all the counts in a temporary holding variable named $item1. The running total is computed with the PHP code "$total = $total + $item1;." The echo statements are used to display the count for each record and the running total for each count.

Tips & Warnings

  • You always need to connect to a mysql database with a mysqlconnect statement to access the data in the database. When you first begin to work with PHP, start with small database tables with just one or two fields and a few records.

  • Pay specific attention to the syntax of your PHP code. PHP will not work correctly if you forget to place a semicolon after the end of a line.

Related Searches:

References

  • Photo Credit Thinkstock/Comstock/Getty Images

Comments

Related Ads

Featured