Create PHP MVC app. Get order detail information
In next several lessons we will work with orders. Today we will get detail information from database about order.
Code lesson (cabinet.tpl.php)
<!DOCTYPE html>
<html lang="ru" data-ng-app="cabinet">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo $pageData['title']; ?></title>
<!-- Bootstrap Core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<!-- MetisMenu CSS -->
<link href="/css/admin/metisMenu.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="/css/admin/sb-admin-2.css" rel="stylesheet">
<!-- Morris Charts CSS -->
<link href="/css/admin/morris.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div id="wrapper">
<!-- Navigation -->
<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Кабинет</a>
</div>
<!-- /.navbar-header -->
<ul class="nav navbar-top-links navbar-right">
<!-- /.dropdown -->
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><a href="/cabinet/profile"><i class="fa fa-user fa-fw"></i> Профиль</a>
</li>
<li class="divider"></li>
<li><a href="/cabinet/logout"><i class="fa fa-sign-out fa-fw"></i> Выйти</a>
</li>
</ul>
<!-- /.dropdown-user -->
</li>
<!-- /.dropdown -->
</ul>
<!-- /.navbar-top-links -->
<div class="navbar-default sidebar" role="navigation">
<div class="sidebar-nav navbar-collapse">
<ul class="nav" id="side-menu">
<li>
<a href="/cabinet"><i class="fa fa-area-chart"></i> Статистика</a>
</li>
<li>
<a href="/cabinet/products"><i class="fa fa-cart-plus"></i> Товары</a>
</li>
<li>
<a href="/cabinet/users"><i class="fa fa-user-o"></i> Пользователи</a>
</li>
</ul>
</div>
<!-- /.sidebar-collapse -->
</div>
<!-- /.navbar-static-side -->
</nav>
<div id="page-wrapper" data-ng-controller="cabinetController">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Статистика</h1>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-4 col-md-4">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fa fa-money fa-5x"></i>
</div>
<div class="col-xs-9 text-right">
<div class="huge">
<?php echo $pageData['ordersCount']; ?>
</div>
<div>заказов</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="panel panel-green">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fa fa-cart-plus fa-5x"></i>
</div>
<div class="col-xs-9 text-right">
<div class="huge">
<?php echo $pageData['productsCount']; ?>
</div>
<div>товаров</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4 col-md-4">
<div class="panel panel-yellow">
<div class="panel-heading">
<div class="row">
<div class="col-xs-3">
<i class="fa fa-user-o fa-5x"></i>
</div>
<div class="col-xs-9 text-right">
<div class="huge">
<?php echo $pageData['usersCount']; ?>
</div>
<div>пользователей</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /.row -->
<div class="row">
<div class="col-lg-12">
<!-- /.panel -->
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bar-chart-o fa-fw"></i> Заказы
</div>
<!-- /.panel-heading -->
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>ID заказа</th>
<th>Сумма заказа</th>
<th>ФИО</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<?php foreach ($pageData['orders'] as $key => $value) {
echo "<tr data-ng-click='openOrderDetails(". $value['id'].")'>";
echo "<td>" . $value['id'] . "</td>";
echo "<td>" . $value['total'] . "</td>";
echo "<td>" . $value['fullName'] . "</td>";
echo "<td>" . $value['email'] . "</td>";
echo "<tr>";
}
?>
</tbody>
</table>
</div>
<!-- /.table-responsive -->
</div>
<!-- /.col-lg-4 (nested) -->
<!-- /.col-lg-8 (nested) -->
</div>
<!-- /.row -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
<!-- /.panel -->
</div>
<!-- /.col-lg-8 -->
</div>
<!-- /.row -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="/js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>
<script src="/js/angular.min.js"></script>
<script src="/js/admin/cabinet.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="/js/admin/metisMenu.js"></script>
<!-- Custom Theme JavaScript -->
<script src="/js/admin/sb-admin-2.js"></script>
</body>
</html>
Code lesson (cabinet.js)
var cabinet = angular.module('cabinet', []);
cabinet.controller("cabinetController", function($scope, $window){
$scope.openOrderDetails = function(orderId) {
$window.location.href = "cabinet/orders?orderId=" + orderId;
}
});
Code lesson (OrdersController.php)
<?php
class OrdersController extends Controller {
private $pageTpl = "/views/order.tpl.php";
public function __construct() {
$this->model = new OrdersModel();
$this->view = new View();
}
public function index() {
if(!$_SESSION['user']) {
header("Location: /");
}
$this->pageData['title'] = "Детали заказа";
if(isset($_GET['orderId'])) {
$orderId = $_GET['orderId'];
$this->pageData['orderInfo'] = $this->model->getOrderInfoByOrderId($orderId);
}
$this->view->render($this->pageTpl, $this->pageData);
}
}
Code lesson (OrdersModel.php)
<?php
class OrdersModel extends Model {
public function getOrderInfoByOrderId($orderId) {
$result = array();
$sql = "SELECT users.fullName, users.email, orders.amount, products.name, products.price
FROM users
INNER JOIN orders ON orders.user_id = users.id
INNER JOIN productsInOrders ON orders.id = productsInOrders.order_id
INNER JOIN products ON productsInOrders.product_id = products.id
WHERE orders.id = :orderId";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":orderId", $orderId, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();
return $result;
}
}
0 Comments