Структура:
/result
│
├──/includes
│ │
│ ├──.htaccess
│ │
│ ├──configuration.inc
│ │
│ ├──connect.inc
│ │
│ └──disconnect.inc
│
├──index.phtml
│
├──scripts.js
│
├──scripts.php
│
├──second.phtml
│
└──styles.css
Директория includes:
Файл .htaccess:
<Files *.inc>
Order deny,allow
Deny from all
</Files>
Файл configuration.inc:
<?
$dbhost = 'localhost';
$dbname = '******_education';
$dbuser = '******_education';
$dbpass = '******';
?>
Файл connect.inc:
<?
mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
mysql_query('set names utf-8;');
mysql_query('set collation_connection=utf8_general_ci;');
mysql_query('lock tables education_goods write;');
?>
Файл disconnect.inc:
<?
mysql_query('unlock tables;');
mysql_close();
?>
phtml-документы:
Файл index.phtml:
<?
session_start();
require('includes/configuration.inc');
require('includes/connect.inc');
if(sizeof($_SESSION))
{
$goodsNumber = 0;
$totalSum = 0;
foreach($_SESSION as $Key => $Value)
{
list(, $goodPrice, $goodNumber) = split('\|', $Value);
$totalSum += $goodPrice*$goodNumber;
$goodsNumber += $goodNumber;
}
}
$goodsResult = mysql_query('select good_id, good_name, good_price from education_goods limit 3;');
if(mysql_num_rows($goodsResult)) while(list($goodID, $goodName, $goodPrice) = mysql_fetch_row($goodsResult))
{
$goodNames['g'.$goodID] = $goodName;
$goodPrices['g'.$goodID] = $goodPrice;
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Первая страница</title>
<link rel="stylesheet" href="styles.css">
<script src="scripts.js"></script>
</head>
<body>
<?
if($goodsNumber) echo ' <h1>Корзина: товаров – '.$goodsNumber.', сумма – '.$totalSum.' руб. <button>Очистить</button></h1>'."\n";
else echo ' <h1>Корзина пуста</h1>'."\n";
foreach($goodNames as $Key => $Value)
{
echo ' <div>'.$goodNames[$Key].' ('.$goodPrices[$Key].' руб.)</div>'."\n";
echo ' <input type="text" name="good'.preg_replace('/[^\d]/i', '', $Key).'" value="';
if(isset($_SESSION[$Key]) && $_SESSION[$Key])
{
list(, $goodPrice, $goodNumber) = split('\|', $_SESSION[$Key]);
echo $goodNumber;
}
echo '">'."\n";
}
?>
<hr>
Страницы: 1 | <a href="second.phtml">2</a>
</body>
</html>
<?
require('includes/disconnect.inc');
?>
Файл second.phtml:
<?
session_start();
require('includes/configuration.inc');
require('includes/connect.inc');
if(sizeof($_SESSION))
{
$goodsNumber = 0;
$totalSum = 0;
foreach($_SESSION as $Key => $Value)
{
list(, $goodPrice, $goodNumber) = split('\|', $Value);
$totalSum += $goodPrice*$goodNumber;
$goodsNumber += $goodNumber;
}
}
$goodsResult = mysql_query('select good_id, good_name, good_price from education_goods limit 3,6;');
if(mysql_num_rows($goodsResult)) while(list($goodID, $goodName, $goodPrice) = mysql_fetch_row($goodsResult))
{
$goodNames['g'.$goodID] = $goodName;
$goodPrices['g'.$goodID] = $goodPrice;
}
?>
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Вторая страница</title>
<link rel="stylesheet" href="styles.css">
<script src="scripts.js"></script>
</head>
<body>
<?
if($goodsNumber) echo ' <h1>Корзина: товаров – '.$goodsNumber.', сумма – '.$totalSum.' руб. <button>Очистить</button></h1>'."\n";
else echo ' <h1>Корзина пуста</h1>'."\n";
foreach($goodNames as $Key => $Value)
{
echo ' <div>'.$goodNames[$Key].' ('.$goodPrices[$Key].' руб.)</div>'."\n";
echo ' <input type="text" name="good'.preg_replace('/[^\d]/i', '', $Key).'" value="';
if(isset($_SESSION[$Key]) && $_SESSION[$Key])
{
list(, $goodPrice, $goodNumber) = split('\|', $_SESSION[$Key]);
echo $goodNumber;
}
echo '">'."\n";
}
?>
<hr>
Страницы: <a href="../result/">1</a> | 2
</body>
</html>
<?
require('includes/disconnect.inc');
?>
CSS-разметка (styles.css):
body { font-family: Arial, Tahoma, Verdana; font-size: 14px; line-height: 14px; }
h1 { font-family: Arial, Tahoma, Verdana; font-size: 26px; line-height: 26px; background-color: #f0f0f0; padding: 16px; }
div { float: left; display: table-cell; width: 130px; margin-bottom: 14px; font-family: Arial, Tahoma, Verdana; font-size: 14px; }
input { display: block; margin-bottom: 14px; border: 1px solid #d0d0d0; font-family: Arial, Tahoma, Verdana; font-size: 14px; outline: none; }
button { margin-left: 6px; vertical-align: top; width: 150px; height: 27px; font-size: 16px; border: 1px solid #d0d0d0; outline: none; background-color: #e0e0e0; }
hr { height: 1px; border: 0; background-color: #e0e0e0; margin: 18px 0px 16px 0px; }
JS-скрипты (scripts.js):
var xhr = new XMLHttpRequest();
window.onload = function()
{
if(document.getElementsByTagName('button')[0]) document.getElementsByTagName('button')[0].addEventListener("click", clearCart);
document.body.onkeypress = function(e)
{
if(e.keyCode ==13 && /^good\d{1,}$/i.test(e.target.name))
{
if(!/^\d{1,}$/i.test(e.target.value)) e.target.value = '0';
xhr.open('GET', 'scripts.php?goodid='+e.target.name.replace(/good/i, '')+'&goodnumber='+e.target.value, true);
xhr.responseType = 'text';
xhr.send();
if(e.target.value == '0') e.target.value = '';
}
};
};
xhr.onload = function()
{
if(xhr.response == 'Корзина пуста')
{
if(document.getElementsByTagName('button')[0]) document.getElementsByTagName('button')[0].removeEventListener("click", clearCart);
document.getElementsByTagName('h1')[0].innerHTML = 'Корзина пуста';
var elementsCollection = document.getElementsByTagName('input');
var elementsArray = Array.from(elementsCollection);
elementsArray.forEach(function(currentElement) { currentElement.value = ''; });
}
else
{
if(document.getElementsByTagName('button')[0]) document.getElementsByTagName('button')[0].removeEventListener("click", clearCart);
document.getElementsByTagName('h1')[0].innerHTML = xhr.response;
document.getElementsByTagName('button')[0].addEventListener("click", clearCart);
}
};
function clearCart()
{
xhr.open('GET', 'scripts.php?clearcart', true);
xhr.responseType = 'text';
xhr.send();
}
PHP-скрипты (scripts.php):
<?
session_start();
header('Content-Type: text/html; charset=utf-8');
if(isset($_GET['clearcart']))
{
session_unset();
echo 'Корзина пуста';
return;
}
require('includes/configuration.inc');
require('includes/connect.inc');
$goodResult = mysql_query('select good_name, good_price from education_goods where good_id = '.$_GET['goodid'].';');
list($goodname, $goodprice) = mysql_fetch_row($goodResult);
if($_GET['goodnumber'] == 0) unset($_SESSION['g'.$_GET['goodid']]);
else $_SESSION['g'.$_GET['goodid']] = $goodname.'|'.$goodprice.'|'.$_GET['goodnumber'];
if(sizeof($_SESSION))
{
$goodsNumber = 0;
$totalSum = 0;
foreach($_SESSION as $Key => $Value)
{
list(, $goodPrice, $goodNumber) = split('\|', $Value);
$totalSum += $goodPrice*$goodNumber;
$goodsNumber += $goodNumber;
}
echo 'Корзина: товаров – '.$goodsNumber.', сумма – '.$totalSum.' руб. <button>Очистить</button>';
}
else echo 'Корзина пуста';
require('includes/disconnect.inc');
return;
?>
Результат:
Результат выполнения задания можно посмотреть
здесь.