Menu

11/28/21

Quan li loai (crud_database)

 


Cấu trúc thư mục:

Css\style.css

action_add.php

action_delete.php

action_update.php

index.php

add.php

delete.php

update.php

pdo.php

File loai.sql

-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Nov 28, 2021 at 04:41 AM
-- Server version: 5.7.23
-- PHP Version: 7.2.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `bookstorevn`
--

-- --------------------------------------------------------

--
-- Table structure for table `loai`
--

DROP TABLE IF EXISTS `loai`;
CREATE TABLE IF NOT EXISTS `loai` (
  `maloai` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `tenloai` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`maloai`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `loai`
--

INSERT INTO `loai` (`maloai`, `tenloai`) VALUES
('gk', 'GK'),
('kt', 'Kinh Tế'),
('nn', 'Ngoại Ngữ'),
('pl', 'Pháp Luật'),
('SK', 'SUC KHOE'),
('td', 'Từ Điển'),
('test', 'Loai Moi'),
('th', 'tin hocc'),
('to', 'toan hocs'),
('tt', 'The Thao Du Lich'),
('vh', 'Văn Học'),
('vhxh', 'Van Hoa xa Hoi');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

File style.css
		body {
			font:30px arial;
			margin: 30px;
			padding: 10px;
			background: #959393;
		}
		#wrapper{ 
			border: 3px solid black;
			background-color:#9e9e9e;
			height: 500px;
		
		}
		#header{
			background-color:#66917a;
			text-align:center;
			padding: 8px;
			font-size:30px;
		}
		#header h1{
			text-transform: uppercase;
			font-size:30px;
			font-style:italic;
		}
		#menu{
			background-color: blue;
		}
		#menu ul{
			padding: 20px;
			margin: 0px;
			font-weight: bold;
			text-transform: uppercase;

		}
		#menu ul li{
		display: inline-block;
		
		}
		#menu ul li a{
			color: white;
			font-size: 20px;
			padding:8px 10px;
			list-style:none;
			text-decoration: none;
			display: block; 
			transition: all 2s ease;
		}
		#menu ul li a:hover{
			background-color: #4c625678;
		}
		#main-content{

			border: 1px solid yellow;
			min-height: 100px;
			padding: 30px;
			background-color: #efefef;
		}
		#main-content h2{
			text-transform: capitalize;
			padding: 10px;
			font-size: 100px;
		}
		#main-content table{
	
			width: 80%;
    		border: 1px solid black;
    		height: auto;
			
		}
		#main-content th{
			border: 1px solid black;
			background-color: #788f8f;
			text-transform: uppercase;
		}
		#main-content td{
			border: 1px solid black;    
			text-align: center;
			background-color: #e7e7e2;
		}

		#main-content td a{
			font-size: 20px;
			color: white;
			padding: 5px;  
			text-align: center;
			background-color: #e17e21;
			text-transform: uppercase;
			margin: 5px;

		}
		#main-content td a:nth-child(2){
			background-color: red;
		}
		#main-content td a:nth-child(3){
			background-color: #607d8b;
		}

		#main-content .post-form {
			width: 40%;	   
			margin: 0 auto; 
			padding: 30px;
			background-color: #abafafd4;
			border-radius: 10px;
		}
		#main-content .post-form .form-group{
			margin:  0 0 15px;
		}
		#main-content .post-form .form-group label{
				font-size: 30px;
			    display:block;
    			margin: 0 0 12px;
		}
		#main-content .post-form .form-group input{
			font-size:  30px;
		}
		#main-content .post-form .form-group .select{
			font-size: 30px;
		}
		#main-content .post-form .submit{

				    background-color: black;
				    color: white;
				    font-size: 25px;
				    margin-left: 30%;
				    border-radius: 9px;
				    padding: 10px;
		}
File action_add.php

<?php
 
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='insert into loai(maloai, tenloai) value(?,?)';
    $stm=$o->prepare($sql);
    $arr=[$_POST['maloai'], $_POST['tenloai']];
    $stm->execute($arr);
    header('location:index.php');
?>

File action_delete.php

<?php
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='delete from loai where maloai=?';
    $stm=$o->prepare($sql);
    $arr=[$_POST['maloai']];
    $stm->execute($arr);
    header('location:index.php');
?>

File action_update.php

<?php
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='update loai set tenloai=? where maloai=?';
    $stm=$o->prepare($sql);
    $arr=[$_POST['tenloai'], $_POST['maloai']];
    $stm->execute($arr);
    header('location:index.php');
?>

File add.php

<!DOCTYPE html>
<html>
<head>
<title>Add</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<?php include "pdo.php" ; ?>
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>add new record</h2>
		<form class="post-form" method="post" action="action_add.php">
				<div class="form-group">
					<label>Ma Loai</label>
					<input type="text" name="maloai">
				</div>
				<div class="form-group">
					<label>Ten Loai </label>
					<input type="text" name="tenloai" >
				</div>
				
				<div class="form-group">
					<label>Ten Loai Trong DB</label>
					<select class="select">
						<option selected disabled>xem loai</option>
						<option value="1">A</option>
						            <?php 

			            foreach ($data as $key=>$value){
			                 ?>                 
			                                
			                 <option value=""> <?php echo $value["tenloai"]; ?></option>
			                       
			                    
			                 <?php

			            }
			            ?>
					</select>
				</div>
				<input class="submit" type="submit" value="Them" />

		</form>

	</div>

</body>
</html>

File delete.php

<!DOCTYPE html>
<html>
<head>
<title>delete</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>delete record</h2>
		<form class="post-form" method="post" action="action_delete.php">
				<div class="form-group">
					<label>Id</label>
					<input type="text" name="maloai">
				</div>
				<input class="submit" type="submit" value="delete">
		</form>

	</div>

</body>
</html>

File index.php

<!DOCTYPE html>
<html>
<head>
	<title>Page Title</title>
	<link rel="stylesheet"  href="css/style.css">
    <?php include "pdo.php"; ?>
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>all record</h2>
				 <table cellpadding="7px">
        <thead>
        <th>stt</th>
        <th>ma loai</th>
        <th>ten loai</th>
        <th>Action</th>
        </thead>
        <tbody>
     
            <?php 

            foreach ($data as $key=>$value){
                 ?>                 
                    <tr>
                        <td><?php echo $key+1 ?></td>
                        <td><?php echo $value["maloai"]; ?></td>
                        <td><?php echo $value["tenloai"]; ?></td>
                        <td> 
                            <a href="add.php?maloai=<?php echo $value['maloai']; ?>">
                            Add
                            </a>
                            <a href="delete.php?maloai=<?php echo $value['maloai']; ?>">
                            Delete
                            </a>
                             <a href="update.php?maloai=<?php echo $value['maloai']; ?>">
                            Edit
                            </a>
                        </td>
                    </tr>
                 <?php

            }
            ?>
            
        </tbody>
    </table>
		</div>



</body>
</html>

File pdo.php

    <?php
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='select * from loai';
    $stm=$o->query($sql);
    $data=$stm->fetchAll(PDO::FETCH_ASSOC);
    ?>
File update
<!DOCTYPE html>
<html>
<head>
<title>update</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>edit record</h2>
		<form class="post-form" method="post" action="action_update.php">
				<div class="form-group">
					<label>Ma Loai</label>
					<input type="text" name="maloai">
				</div>
				
				<div class="form-group">
					<label>Ten Loai </label>
					<input type="text" name="tenloai" >
				</div>
				
				<input class="submit" type="submit" value="Save" />

		</form>

	</div>

</body>
</html>

11/27/21

Project menu Crud (html+css)

 


File style.css

body {
			font:30px arial;
			margin: 30px;
			padding: 10px;
			background: #959393;
		}
		#wrapper{ 
			border: 3px solid black;
			background-color:#9e9e9e;
			height: 500px;
		
		}
		#header{
			background-color:#66917a;
			text-align:center;
			padding: 8px;
			font-size:30px;
		}
		#header h1{
			text-transform: uppercase;
			font-size:30px;
			font-style:italic;
		}
		#menu{
			background-color: blue;
		}
		#menu ul{
			padding: 20px;
			margin: 0px;
			font-weight: bold;
			text-transform: uppercase;

		}
		#menu ul li{
		display: inline-block;
		
		}
		#menu ul li a{
			color: white;
			font-size: 20px;
			padding:8px 10px;
			list-style:none;
			text-decoration: none;
			display: block; 
			transition: all 2s ease;
		}
		#menu ul li a:hover{
			background-color: #4c625678;
		}
		#main-content{

			border: 1px solid yellow;
			min-height: 100px;
			padding: 30px;
			background-color: #efefef;
		}
		#main-content h2{
			text-transform: capitalize;
			padding: 10px;
			font-size: 100px;
		}
		#main-content table{
	
			width: 80%;
    		border: 1px solid black;
    		height: auto;
			
		}
		#main-content th{
			border: 1px solid black;
			background-color: #788f8f;
			text-transform: uppercase;
		}
		#main-content td{
			border: 1px solid black;    
			text-align: center;
			background-color: #e7e7e2;
		}

		#main-content td a{
			font-size: 20px;
			color: white;
			padding: 5px;  
			text-align: center;
			background-color: #e17e21;
			text-transform: uppercase;
		}
		#main-content td a:nth-child(2){
			background-color: red;
		}

		#main-content .post-form {
			width: 40%;	   
			margin: 0 auto; 
			padding: 30px;
			background-color: #abafafd4;
			border-radius: 10px;
		}
		#main-content .post-form .form-group{
			margin:  0 0 15px;
		}
		#main-content .post-form .form-group label{
				font-size: 30px;
			    display:block;
    			width: 30%;
    			margin: 0 0 12px;
		}
		#main-content .post-form .form-group input{
			font-size:  30px;
		}
		#main-content .post-form .form-group .select{
			font-size: 30px;
		}
		#main-content .post-form .submit{

				    background-color: black;
				    color: white;
				    font-size: 25px;
				    margin-left: 30%;
				    border-radius: 9px;
				    padding: 10px;
		}
File add.php

<!DOCTYPE html>
<html>
<head>
<title>Add</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>add new record</h2>
		<form class="post-form" method="get" action="savedata.php">
				<div class="form-group">
					<label>Name</label>
					<input type="text" name="sname">
				</div>
				<div class="form-group">
					<label>Address </label>
					<input type="text" name="saddress" >
				</div>
				<div class="form-group">
					<label>Phone</label>
					<input type="text" name="sphone" />
				</div>
				<div class="form-group">
					<label>Class</label>
					<select class="select" name="class">
						<option selected disabled>Selected Class</option>
						<option value="1">A</option>
						<option value="2">B</option>
						<option value="3">C</option>
					</select>
				</div>
				<input class="submit" type="submit" value="Save" />

		</form>

	</div>

</body>
</html>


File update.php

<!DOCTYPE html>
<html>
<head>
<title>update</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>edit record</h2>
		<form class="post-form" method="get" action="savedata.php">
				<div class="form-group">
					<label>Id</label>
					<input type="text" name="sid">
				</div>
				<input class="submit" type="submit" value="show">
				<div class="form-group">
					<label>Name</label>
					<input type="text" name="sname">
				</div>
				<div class="form-group">
					<label>Address </label>
					<input type="text" name="saddress" >
				</div>
				<div class="form-group">
					<label>Phone</label>
					<input type="text" name="sphone" />
				</div>
				<div class="form-group">
					<select class="select" name="class">
						<option selected disabled>Selected Class</option>
						<option value="1">A</option>
						<option value="2">B</option>
						<option value="3">C</option>
					</select>
				</div>
				<input class="submit" type="submit" value="Save" />

		</form>

	</div>

</body>
</html>

File delete.php

<!DOCTYPE html>
<html>
<head>
<title>delete</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>delete record</h2>
		<form class="post-form" method="get" action="savedata.php">
				<div class="form-group">
					<label>Id</label>
					<input type="text" name="sid">
				</div>
				<input class="submit" type="submit" value="delete">
		</form>

	</div>

</body>
</html>

File index.php

<!DOCTYPE html>
<html>
<head>
	<title>Page Title</title>
	<link rel="stylesheet"  href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>all record</h2>
				 <table cellpadding="7px">
        <thead>
        <th>Id</th>
        <th>Name</th>
        <th>Address</th>
        <th>Class</th>
        <th>Phone</th>
        <th>Action</th>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>Ramesh</td>
                <td>Delhi</td>
                <td>BCA</td>
                <td>9876543210</td>
                <td>
                    <a href='edit.php'>Edit</a>
                    <a href='delete-inline.php'>Delete</a>
                </td>
            </tr>
            <tr>
                <td>2</td>
                <td>Suresh</td>
                <td>Punjab</td>
                <td>BCOM</td>
                <td>9876543210</td>
                <td>
                    <a href='edit.php'>Edit</a>
                    <a href='delete-inline.php'>Delete</a>
                </td>
            </tr>
            <tr>
                <td>3</td>
                <td>Suresh</td>
                <td>Haryana</td>
                <td>BSC</td>
                <td>9876543210</td>
                <td>
                    <a href='edit.php'>Edit</a>
                    <a href='delete-inline.php'>Delete</a>
                </td>
            </tr>
            <tr>
                <td>4</td>
                <td>Krishna</td>
                <td>Gujrat</td>
                <td>BCA</td>
                <td>9876543210</td>
                <td>
                    <a href='edit.php'>Edit</a>
                    <a href='delete-inline.php'>Delete</a>
                </td>
            </tr>
            <tr>
                <td>5</td>
                <td>Rohit</td>
                <td>Delhi</td>
                <td>BCA</td>
                <td>9876543210</td>
                <td>
                    <a href='edit.php'>Edit</a>
                    <a href='delete-inline.php'>Delete</a>
                </td>
            </tr>
        </tbody>
    </table>
		</div>



</body>
</html>

10/16/21

Xây dựng cartcontroller


-Chuyển hướng URL (chuyển hướng trang) 

header('location:index.php?controller=CartController');

-ứng dụng session trong việc tạo giỏ hàng:

$_SESSION['cart']: thong tin gio hang

td01 - 2 : key-value: mã sách và số lượng

td02 - 3:  key-value: mã sách và số lượng

$_SESSION['cart'] = ['td01'=>2, 'td02'=>3]

Thực tế: var_dump($_SESSION);

==> Mảng 2 chiều:

Array ( [cart] => Array ( [td01] => 1 ) )

Thực tế: var_dump($_SESSION['cart']);

==> Mảng 1 chiều: 

Array ( [td01] => 1 )

-Khi truyền giá trị lên thanh URL, nếu số lượng ko có thì mặc định là 01 sản phẩm

 index.php?controller=CartController&action=add&id=td01

Để lấy giá trị của URL ta dùng hàm getindex

$id = getIndex('id'); //$id=td01;

$sl = getIndex('sl', 1);//sl=1

Lưu ý: 

if(!isset($_SESSION)) session_start();

var_dump($_SESSION);

Trước khi dùng session, phải start() dù cho giá trị $_SESSION là empty- rỗng.

Xây dựng 1 giỏ hàng ảo: 

	<?php
        function add()

	{

		if(!isset($_SESSION)) session_start();

		$_SESSION['CART']=array('td01'=>2,'td02'=>5);

		//unset($_SESSION['CART']);

		var_dump($_SESSION);

	}

Thông tin giỏ hàng được lưu vào mảng 1 chiều:

$_SESSION['cart'], nhưng do lần đầu khi chưa mua hàng thì mảng rỗng, nên phải dùng mảng tam[] để khới tạo giá trị rồi gán ngược lại cho mảng $_SESSION['cart'].

function add()
	{
		//index.php?controller=CartController&action=add&id=td01
		$id = getIndex('id'); //$id=td01;
		$sl = getIndex('sl', 1);//sl=1
		$tam = isset($_SESSION['cart'])?$_SESSION['cart']:[];//$tam=[];
		if (isset($tam[$id]))
		{
			$tam[$id] += $sl;
		}
		else $tam[$id]=$sl;

		$_SESSION['cart']= $tam;
		header('location:index.php?controller=CartController');
		
	}
kiểm tra giỏ hàng dùng hàm print_r trong index:

function index()
	{

		print_r($_SESSION); //hoặc: print_r($_SESSION['cart']);
}
 
CART CONTROLLER THÊM SAN PHAM KHI XAY DUNG XONG

<?php
class CartController
{
	function __construct()
	{
		$action =getIndex('action','index');

		if(method_exists($this,$action)){
			$ref=new ReflectionMethod($this, $action);
			if(!$ref->isPublic()){
				echo "ham ko duoc public";exit;
			}
			
			$this->$action();
		}
		else{
			echo "ham ".$action. " chua xay dung!";
		}
		
	}
	function index()
	{
		//if(!isset($_SESSION)) session_start();
		var_dump($_SESSION['cart']);
	}
	function add()
	{
		//if(!isset($_SESSION)) session_start();
	    $id=getIndex("id");
	    $sl=getIndex("sl",1);
	    $tam=isset($_SESSION['cart'])?$_SESSION['cart']:[];
	    if(isset($tam[$id])){
	    	$tam[$id]+=$sl;
	    } else $tam[$id]=$sl;
	    
        $_SESSION['cart']=$tam;
        header('location:index.php?controller=CartController');
        
	}

        function delete()
	{     
                $id = getIndex('id');
		$tam = $_SESSION['cart']?$_SESSION['cart']:[];
		unset($tam[$id]);
		$_SESSION['cart']= $tam;
		header('location:index.php?controller=CartController');

	}
} ?>
-Thêm tính năng chọn up/down số lượng sản phẩm bằng form


<?php
<form action="index.php" method="get">
                    <input type="hidden" name="controller" value="CartController">
                    <input type="hidden" name="action" value="add">
                    <input type="hidden" name="id" value="<?php echo $data['masach'];?>">
                    <input type="number" name="sl" value="1" min="1" max="10" autofocus>
                    <input type="submit" value="addcart">
                    
</form>


Cách kiểm tra sự tồn tại của hàm

 Kiểm tra sự tồn tại của hàm theo cách thông thường:

-Trong php có hàm mặc định method_exist(), sẽ trả về kết quả true nếu hàm tồn tại:

VD1: 

<?php
class CartController
{
	function __construct()
	{
		$action =getIndex('action','index');
		
		if(method_exists($this,$action)){
			$this->$action();
		}
		else{
			echo "ham ".$action. " chua xay dung!";
		}
		
	}
	function index()
	{
		echo "day la index CartController";
	}
	function F1()
	{
		echo "ham f1 dc goi";
	}

}
?>



Kiểm tra sự tồn tại của hàm và phạm vi truy xuất dựa vào hàm ReflectionMethod
class CartController
{

	function __construct()
	{
		
		$action=getIndex('action','index');
		if (method_exists($this, $action))
		{
			$reflection = new ReflectionMethod($this, $action);
		    if (!$reflection->isPublic()) {
		     
		     echo "CO, NHUNG PHAI PUBLIC MOI DUOC GOI";exit;
		    }

			$this->$action();
		}
		else 
		{
			echo "Chua xay dung ham...";
			exit;
		}
	}
       function index()
	{
		echo "ham index duoc goi";
		
	}

	protected function add()
	{
		echo "ham add duoc goi";
		
	}

}

Khi gọi controller và nhận kết quả:

http://localhost/mvc/index.php?controller=CartController&action=add
CO, NHUNG PHAI PUBLIC MOI DUOC GOI

10/10/21

Xây dựng hàm định dạng tiền tệ php

<?php
/**
 *
 * Chuyển đổi chuỗi kí tự thành dạng slug dùng cho việc tạo friendly url.
 *
 * @access    public
 * @param    string
 * @return    string
 */
if (!function_exists('currency_format')) {
    function currency_format($number, $suffix = 'đ') {
        if (!empty($number)) {
            return number_format($number, 0, ',', '.') . "{$suffix}";
        }
    }
}

Khi gọi hàm:

<?php
echo currency_format(5000000);
?>
<?php
echo currency_format(37, 'USD');
?>

















9/30/21

Bóc tách layer ra subview cho gọn

 Chúng ta có 1 trang layout html-css code nhìn rất rối, ta nên tách ra thư mục subview theo từng phần head.php, header.php, content_left.php, content_right.php, footer.php rồi dùng câu lệnh include để insert vào trang layout nhé!


Trang layout sau khi đã bóc tách:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <?php include 'subview/head.php'; ?>
</head>
<body>
<!--  Free CSS Templates from www.templatemo.com -->
<div id="templatemo_container">
	<div id="templatemo_menu">
       <?php include 'subview\menu.php'; ?>
	   
    </div> <!-- end of menu -->
    
    <div id="templatemo_header">
    	<?php include 'subview\header.php'; ?>
    </div> <!-- end of header -->
    
    <div id="templatemo_content">
    	
        <div id="templatemo_content_left">
        	<?php include 'subview\content_left.php'; ?>
        </div> <!-- end of content left -->
        
        <div id="templatemo_content_right">
        	<?php include 'subview/content_right.php'; ?>
        </div> <!-- end of content right -->
    
    	<div class="cleaner_with_height">&nbsp;</div>
    </div> <!-- end of content -->
    
    <div id="templatemo_footer">
    
	      <?php include 'subview/footer.php'; ?>
    <!-- end of footer -->
<!--  Free CSS Template www.templatemo.com -->
</div> <!-- end of container -->
<!-- templatemo 086 book store -->
<!-- 
Book Store Template 
http://www.templatemo.com/preview/templatemo_086_book_store 
-->
</body>
</html>

Cấu trúc thư mục subview:








9/29/21

Đường dẫn tương đối php (relative path)

 -Đường dẫn tương đối (relative path) là đường dẫn xuất phát từ thư mục hiện tại đang chạy, ví dụ bạn muốn truy cấp đến file index.php thì cú pháp như sau:

/index.php

Nếu muốn thoát ra 1 cấp thư mục ta dùng ../ ví dụ ta muốn truy xuất file index.php ở ngoài thư mục hiện tại: ../index.php

Ngoài ra, chúng ta cũng có hàm để echo đường dẫn tương đối :

Syntax: realpath( $path);

Trong đó: $path là đường dẫn tương đối bạn nhập vào

ví dụ: 

<?php

$path = '../images/templatemo_ads.jpg';

echo realpath($path);

kết quả:

C:\wamp64\www\book\images\templatemo_ads.jpg