HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Success page + data entry for a store page

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
entrysuccessstoreforpagedata

Problem

The purpose of this code is to log the inputs of a form (which is a menu for a shop) to the database to be kept as records as well as printed out as a receipt at the store.

I have been learning how to code from the internet and tried to put together my knowledge in a project. The code is working, but I would like to adopt elegance and good coding practices.



Untitled Document

$total);
$STH = $DBH->prepare("INSERT INTO orderlist(total) values (:total)");
$STH->execute($data1);
$lastId = $DBH->lastInsertId();
//dataentry2
$data2 = array('orderid' => $lastId, 'soba_size' => $soba_size, 'soba_type' => $soba_type, 'topping' => $dbtoppings,'request' => $request);
$STH = $DBH->prepare("INSERT INTO orderdetails(orderID,soba_size,soba_type,topping,request) values (:orderid,:soba_size,:soba_type,:topping,:request)");
$STH->execute($data2);
}
?>

Thank you for ordering. Your order has been printed.


Order Number:

Amount:$
Please show this page at the cashier to confirm your order. Thank you!

Solution

First and most important thing is to indent your code. Indenting provides an instant visual structure which makes it easy to read. More often than not, bugs and quirks can be easily avoided simply by good code indentation. PHP is especially susceptible to this because its code is mixed directly with the HTML output. There is actually an issue in your posted code where should there be an error, your code will still try to display the order details at the bottom.

Next thing you want to consider is creating your code in blocks. On a high level this is basic MVC code structure where the logic and the output are separated. You want to apply this as often as possible to make your code easier to read and maintain.

Make sure to close your database connection after opening it. I typically declare my database object outside the included class file in order to make it clearer what the variable is. This also helps your IDE out for providing popup advice, and code completion. If you're not using an IDE yet, try NetBeans or CodeLobster for PHP development.

Here's my take on your code. I obviously couldn't test it without your database details but it should give you some ideas.

db = $DBH;       //should probably declare $DBH outside the included file to make code easier to read, and to help the ide

        $this->ProcessOrder();

        //close database connection here
    }

    public function getNumber(){
        return $this->Number;
    }

    public function getAmount(){
        return $this->Amount;
    }

    private function ProcessOrder(){
        $total = 0;
        $soba_size = $_POST['soba_size'];
        switch($soba_size){
            case "small":
                $total = $total + 4;
                break;
            case "reg":
                $total = $total + 6;
                break;
            case "large":
                $total = $total + 8;
                break;
            default:break;      //always have a default even if you're confident it won't be needed
        }
        $soba_type = $_POST['soba_type'];
        $dbtoppings = '';
        if ($_POST['topping']){
            foreach($_POST['topping'] as $topping){
                switch($topping){
                    case "duck":
                        $total = $total + 1;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "egg":
                        $total = $total + 2;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "chicken":
                        $total = $total + 3;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "teriyaki chicken":
                        $total = $total + 4;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    default:break;      //always have a default even if you're confident it won't be needed
                }
            }
        }
        if($_POST['requests']){
            $request = $_POST['requests'];
        }
        else{
            $request = '';
        }

        //begin data entry
        $data1 = array('total' => $total);
        $STH = $this->db->prepare("INSERT INTO orderlist(total) values (:total)");
        $STH->execute($data1);
        $lastId = $this->db->lastInsertId();
        //dataentry2
        $data2 = array('orderid' => $lastId, 'soba_size' => $soba_size, 'soba_type' => $soba_type, 'topping' => $dbtoppings,'request' => $request);
        $STH = $this->db->prepare("INSERT INTO orderdetails(orderID,soba_size,soba_type,topping,request) values (:orderid,:soba_size,:soba_type,:topping,:request)");
        $STH->execute($data2);

        //set the class variables for grabbing by your other code
        $this->Amount = $total;
        $this->Number = $lastId;
    }
}
?>

    
    Untitled Document

    
        error, order was not received!
        
        return to  order page 
        
        
            
                Thank you for ordering. Your order has been printed.
                
            
            
                Order Number: getNumber();?>
                
                Amount: $ getAmount();?>
            
            
                Please show this page at the cashier to confirm your order. Thank you!
               
        
        

Code Snippets

<?php
class OrderDetails{
    private $Number;
    private $Amount;
    private $db;

    public function OrderDetails(){
        require 'databaseaccess.php';
        $this->db = $DBH;       //should probably declare $DBH outside the included file to make code easier to read, and to help the ide

        $this->ProcessOrder();

        //close database connection here
    }

    public function getNumber(){
        return $this->Number;
    }

    public function getAmount(){
        return $this->Amount;
    }

    private function ProcessOrder(){
        $total = 0;
        $soba_size = $_POST['soba_size'];
        switch($soba_size){
            case "small":
                $total = $total + 4;
                break;
            case "reg":
                $total = $total + 6;
                break;
            case "large":
                $total = $total + 8;
                break;
            default:break;      //always have a default even if you're confident it won't be needed
        }
        $soba_type = $_POST['soba_type'];
        $dbtoppings = '';
        if ($_POST['topping']){
            foreach($_POST['topping'] as $topping){
                switch($topping){
                    case "duck":
                        $total = $total + 1;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "egg":
                        $total = $total + 2;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "chicken":
                        $total = $total + 3;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    case "teriyaki chicken":
                        $total = $total + 4;
                        $dbtoppings = $dbtoppings.",".$topping;
                        break;
                    default:break;      //always have a default even if you're confident it won't be needed
                }
            }
        }
        if($_POST['requests']){
            $request = $_POST['requests'];
        }
        else{
            $request = '';
        }

        //begin data entry
        $data1 = array('total' => $total);
        $STH = $this->db->prepare("INSERT INTO orderlist(total) values (:total)");
        $STH->execute($data1);
        $lastId = $this->db->lastInsertId();
        //dataentry2
        $data2 = array('orderid' => $lastId, 'soba_size' => $soba_size, 'soba_type' => $soba_type, 'topping' => $dbtoppings,'request' => $request);
        $STH = $this->db->prepare("INSERT INTO orderdetails(orderID,soba_size,soba_type,topping,request) values (:orderid,:soba_size,:soba_type,:topping,:request)");
        $STH->execute($data2);

        //set the class variables for grabbing by your other code
        $this->Amount = $total;
        $this->Number = $lastId;
    }
}
?><!DOCTYPE>
<html>
<head>
    <meta charset="utf-8">
    <title>Untitled 

Context

StackExchange Code Review Q#120028, answer score: 2

Revisions (0)

No revisions yet.