conn.php
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "fongfood";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
=========================
order.php
<?php
require "conn.php";
date_default_timezone_set('Asia/Taipei');
//自動產生訂單編號
// session_start();
if(!isset($_SESSION['order'])){
$_SESSION['order']=date('Ymdhis'); //訂單編號
}
// unset($_SESSION['order']);
//刪除 單筆資料
if(isset($_POST['del']) and $_POST['del']=='delx'){
$sno=$_POST['sno'];
$sql="delete from shop_car where sno=$sno";
mysqli_query($conn, $sql);
exit;
}
//刪除 全部資料
if(isset($_POST['del']) and $_POST['del']=='delall'){
$orderx=$_SESSION['order'];
$sql="delete from shop_car where orderx=$orderx";
mysqli_query($conn, $sql);
exit;
}
//查 產品名稱/克數
if(isset($_POST['sel']) and $_POST['sel']=='sel'){
$products1=$_POST['products1'];
$sql="select * from products2 where products1=$products1";
$qsa=mysqli_query($conn, $sql);
// $qsa2=mysql_fetch_assoc($qsa);
$p1="<option value='x'>請選擇</option>";
while($qsa2=mysqli_fetch_assoc($qsa)){
$p1.="<option value='$qsa2[price]'>$qsa2[name2] / $qsa2[weight]</option>";
}
echo $p1;
exit;
}
//購物車 新增資料
if(isset($_POST['add']) and $_POST['add']=='addx'){
$orderx=$_SESSION['order']; //訂單編號
$products1=$_POST['products1']; //產品系列
$products2=$_POST['products2']; //產品名稱
$amount=$_POST['amount']; //數量
$price=$_POST['price']; //單價
// echo "$order+$products1+$products2+$amount+$price";
// exit;
$sql="select * from shop_car where products2='$products2' and orderx=$orderx";
$qss=mysqli_query($conn, $sql);
$qn=mysqli_num_rows($qss);
$qss2=mysqli_fetch_assoc($qss);
$amount2=$qss2['amount']+$amount;
$sno2=$qss2['sno'];
if($qn!=0){ //購物車已有此項產品
$sql="update shop_car set amount='$amount2' where sno=$sno2";
}else{ //購物車沒有此項產品
$sql="insert into shop_car(orderx,products1,products2,amount,price) values('$orderx','$products1','$products2','$amount','$price')";
}
mysqli_query($conn, $sql);
exit;
}
//購物車 重新計算總金額
if(isset($_POST['totalx']) and $_POST['totalx']=='retotal'){
// $sno=split(",",$_POST[total_sno]); //訂購產品的sno
// $i=count($sno); //訂購產品筆數
if(count($_POST)>0){
foreach($_POST as $sno=>$amount){
// echo "$sno = $amount <br />";
if(ctype_digit($amount)){ //數字判斷
$sql="update shop_car set amount='$amount' where sno=$sno";
mysqli_query($conn, $sql);
}
}
}
}
//列出 產品系列
$sql="select * from products1";
$qs=mysqli_query($conn, $sql);
// $qs2=mysql_fetch_assoc($qs);
//列出 購物車資料
$sql="select * from shop_car where orderx='".$_SESSION['order']."'";
$qsc=mysqli_query($conn, $sql);
// $qsc2=mysql_fetch_assoc($qsc);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="favicon.ico" rel="shortcut icon"/>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo1</title>
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/menu.css" rel="stylesheet" type="text/css" />
<script type='text/javascript' src='js/jquery.js'></script>
<script type="text/javascript">
$(function(){
//表格換行變色
$(".list:odd").css("background","#FFF9E9");
//查 產品名稱/克數
$('#products1').change(function(){
var sel='sel';
var products1=$('#products1').val();
$.post('order.php',{sel:sel,products1:products1},function(e){
$('#products2').html(e);
});
});
//加入購物車
$('#addcar').click(function(){
// var products1=$('#products1').val();
// var products2=$('#products2').val();
var products1=$('#products1').find(":selected").text(); //產品系列
var products2=$('#products2').find(":selected").text(); //產品名稱
var amount=$('#amount').val(); //數量
var price=$('#products2').val(); //單價
// alert(products1+','+products2+','+amount+','+price);
// exit;
//驗證 產品系列
if(products1=='請選擇'){
alert("請選擇「產品系列」!");
exit;
}
//驗證 產品名稱/克數
if(products2=='請選擇'){
alert("請選擇「產品名稱/克數」!");
exit;
}
//驗證 數量
if(isNaN(amount) || amount==''){ //驗證 非數字 或 空白
alert("「數量」必需填入數字!");
$('#amount').focus().val('');
exit;
}
//傳資料到後端
$.post('order.php',{add:'addx',products1:products1,products2:products2,amount:amount,price:price},function(e){
// alert(e);
location.href="order.php";
});
});
//刪除 單筆資料
$('.del').click(function(){
var sno=this.alt; //訂購資料的sno
var del=confirm("確定要刪除嗎?");
if (del){
$.post('order.php',{del:'delx',sno:sno},function(e){
// alert(e);
location.href="order.php";
});
}
});
//刪除 全部資料
$('#delall').click(function(){
var del=confirm("確定要清除購物車嗎?");
if (del){
$.post('order.php',{del:'delall'},function(e){
location.href="order.php";
});
}
});
});
</script>
</head>
<body onload="MM_preloadImages('images/foote-taki-h.png')">
<div id="wrapper">
<?php require_once("header.php"); ?>
<!-- conter -->
<div id="conter">
<div id="contall">
<div id="in-bg">
<!-- Cont -->
<div id="shadow"></div>
<div id="contall">
<?php require_once("menu.php"); ?>
<!-- Right -->
<div class="right">
<h2><img src="images/intitle-order.jpg" alt="線上訂購" /></h2>
<h3><img src="images/h3-order-1.jpg" alt="商品選擇" width="87" height="24" /></h3>
<!-- 商品選擇 -->
<div class="order-opt">
<div class="order-opt-set">
<h4><img src="images/order-w-set.jpg" alt="產品系列" /></h4>
<p>
<select name="products1" id="products1" class="box01">
<option value="x">請選擇</option>
<?php while($qs2=mysqli_fetch_assoc($qs)){ ?>
<option value="<?php echo $qs2['sno']; ?>"><?php echo $qs2['name1']; ?></option>
<?php } ?>
</select>
</p>
</div>
<div class="order-opt-gram">
<h4><img src="images/order-w-gram.jpg" alt="產品名稱/克數" /></h4>
<p>
<label>
<select name="products2" id="products2" class="box01">
<option value="x">請選擇</option>
</select>
</label>
</p>
</div>
<div class="order-opt-mete">
<h4><img src="images/order-w-mete.jpg" width="38" height="18" alt="數量" /></h4>
<p>
<label>
<input name="amount" type="text" class="box02" id="amount" value="1" />
</label>
</p>
</div>
<div class="order-opt-buy"><a href="#"><img src="images/order-bu-addtocar.jpg" alt="加入購物車" id="addcar" width="116" height="32" border="0" /></a></div>
</div>
<form id="form1" name="form1" method="post" action="">
<div class="order-opt">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="middle" bgcolor="#941618"><b>產品系列</b></td>
<td align="center" valign="middle" bgcolor="#941618"><b>產品名稱/克數</b></td>
<td align="center" valign="middle" bgcolor="#941618"><b>購買數量</b></td>
<td align="center" valign="middle" bgcolor="#941618"><b>小計(元)</b></td>
<td align="center" valign="middle" bgcolor="#941618"><b>修改</b></td>
</tr>
<?php
$total=0; //合計
$total_sno=''; //所有的sno
while($qsc2=mysqli_fetch_assoc($qsc)){
$total+=$qsc2['price']*$qsc2['amount'];
// $total_sno.="$qsc2['sno'],";
$total_sno.=$qsc2['sno'].",";
?>
<tr class="list">
<td align="center" valign="middle"><?php echo $qsc2['products1']; ?></td>
<td align="center" valign="middle"><?php echo $qsc2['products2']; ?></td>
<td align="center" valign="middle"><input name="<?php echo $qsc2['sno']; ?>" id="amount<?php echo $qsc2['sno']; ?>" type="text" class="box02" value="<?php echo $qsc2['amount']; ?>" size="3" /></td>
<td align="center" valign="middle">$ <?php echo $qsc2['price']*$qsc2['amount']; ?></td>
<td align="center" valign="middle"><a href="#"><img src="images/order-icon-delete.png" width="47" height="19" alt="<?php echo $qsc2['sno']; ?>" class="del"/></a></td>
</tr>
<?php } ?>
<tr>
<td colspan="3" align="right" valign="middle"><img src="images/order-w-all.jpg" width="36" height="18" alt="合計" /></td>
<td colspan="2" align="left" valign="middle"><span class="orderwall">$ <?php echo $total; ?></span><input name="totalx" type="submit" class="againmo" id="totalx" value="" /></td>
</tr>
</table>
<input name="total_sno" id="total_sno" type="hidden" value="<?php echo substr($total_sno,0,-1); ?>" />
<input name="totalx" id="totalx" type="hidden" value="retotal" />
</div>
<div class="order-opt-but"> <a href="products.php"><img src="images/order-bu-gobuy.jpg" width="104" height="32" alt="繼續購物" /></a>
<a href="buy.php"><img src="images/order-bu-next.jpg" width="209" height="32" alt="下一步 填寫訂購者資料" /></a>
<a href="#"><img src="images/order-bu-nobuy.jpg" width="116" height="32" id="delall" alt="清除購物車" /></a>
</div>
</form>
</div>
<!-- Footer -->
<?php require_once("footer.php"); ?>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
======================
admin/index.php
<?php
require "../conn.php";
echo md5('admin');
if(isset($_POST['user']) and $_POST['user']!="" and isset($_POST['psw']) and $_POST['psw']!=""){
$user=$_POST['user'];
$psw=md5($_POST['psw']);
$s1="select * from admin where user='$user' and psw='$psw'";
$s2=mysqli_query($conn, $s1);
$s3=mysqli_num_rows($s2);
$s4=mysqli_fetch_assoc($s2);
if($s3!=0){
session_start();
$_SESSION['key']=$s4['user'];
echo '1';
exit;
}else{
echo '0';
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理系統</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function(){
$('#login_btn').click(function(){
var user=$('#admin_id').val();
var psw=$('#admin_pw').val();
if(user==''){
alert('請輸入帳號 !');
exit;
}
if(psw==''){
alert('請輸入密碼 !');
exit;
}
$('#back').load('index.php',{user:user,psw:psw},function(e){
if(e=='1'){
location.href='news.php';
}else if(e=='0'){
alert('帳號或密碼不正確 !');
}
});
});
});
</script>
</head>
<body id="longin_page">
<div id="back" style="display:none"></div>
<div id="all">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" style="margin:0px; padding:0px;"><img src="images/title.jpg" width="372" height="96" alt="後台管理系統" /></td>
</tr>
<tr>
<td align="center"> </td>
</tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td colspan="4" bgcolor="#BB2024" class="td2"> </td>
</tr>
<tr>
<td width="16%" align="center" valign="middle" bgcolor="#BB2024" class="td1"> </td>
<td width="16%" align="center" valign="middle" bgcolor="#BB2024" class="td1">帳號:</td>
<td width="34%" align="left" valign="middle" bgcolor="#BB2024"><input name="admin_id" type="text" class="lnbox" id="admin_id" size="20" maxlength="20" /></td>
<td width="34%" rowspan="2" align="center" valign="middle" bgcolor="#BB2024"><span class="td1">
<input name="login_btn" type="button" class="lnbutton" id="login_btn" value="登入" />
</span></td>
</tr>
<tr>
<td align="center" valign="middle" bgcolor="#BB2024" class="td1"> </td>
<td align="center" valign="middle" bgcolor="#BB2024" class="td1">密碼:</td>
<td align="left" valign="middle" bgcolor="#BB2024"><input name="admin_pw" type="password" class="lnbox" id="admin_pw" size="20" maxlength="20" /></td>
</tr>
<tr>
<td colspan="4" bgcolor="#BB2024" class="td1"> </td>
</tr>
</table>
</div>
</body>
</html>
=========================
news.php
<?php
require "../conn.php";
require "key.php";
require "../pub/page.php";
// $sel='news_add';
// $ok=0;
//刪除
if($_GET['del']=='del'){
$sno=$_GET['sno'];
$sql="delete from news where sno=$sno";
mysqli_query($conn, $sql);
}
//分頁設定
$phpfile = "news.php"; //頁面文件名
$page= isset($_GET['page'])?$_GET['page']:1; //默認頁碼
// $db = mysql_connect('localhost','root','firtalks'); //鏈接數據庫
// mysql_select_db('dberic',$db); //選擇數據庫
$counts = mysqli_num_rows(mysql_query($conn, "select * from news order by date desc,sno desc")); //獲取需要的數據總條數
$sql="select * from news order by date desc,sno desc"; //定義查詢語句SQL
$getpageinfo = page($id,$page,$counts,$phpfile); //調用函數,生成分頁HTML 和SQL LIMIT 子句
$sql.=$getpageinfo['sqllimit']; //組合完整的SQL語句
$data = $row = array(); //初始化數組
$q=mysqli_query($conn, $sql); //獲取結果集
//資料列表
// $sql="select * from news order by date desc,sno desc";
// $q=mysql_query($sql);
// $q2=mysql_fetch_assoc($q);
// $psize=3;
// $pno=($page*$psize)-2;
$pno=1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>最新消息</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<!--選單-->
<link href="css/dcaccordion.css" rel="stylesheet" type="text/css" />
<link href="css/skins/grey.css" rel="stylesheet" type="text/css" />
<script type='text/javascript' src='js/jquery.cookie.js'></script>
<script type='text/javascript' src='js/jquery.hoverIntent.minified.js'></script>
<script type='text/javascript' src='js/jquery.dcjqaccordion.2.7.min.js'></script>
<script type="text/javascript">
$(function(){
//修改
$('.update').click(function(){
var sno=this.name;
location.href="news_edit.php?sno="+sno;
});
//刪除
$('.del').click(function(){
var sno=this.name;
var del=confirm("確定要刪除嗎?")
if (del){
location.href="news.php?del=del&sno="+sno;
}
});
});
</script>
</head>
<body>
<div id="allbox">
<div id="container" style="background-color:#9A1B1D"><img src="images/title.jpg" width="372" height="96" /></div>
<div id="container" class="bgbg">
<!--內容-->
<div id="content">
<table>
<tr>
<td colspan="4" class="td2">最新消息 <a href="news_add.php">(新增)</a></td>
</tr>
<tr>
<td bgcolor="#9A1B1D" class="td1" style="width:30px;">序號</td>
<td bgcolor="#9A1B1D" class="td1" style="width:180px;">標題</td>
<td bgcolor="#9A1B1D" class="td1" style="width:70px;">發表日期</td>
<td bgcolor="#9A1B1D" class="td1" style="width:105px;">執行</td>
</tr>
<?php while($q2=mysqli_fetch_assoc($q)){ ?>
<tr>
<td style="text-align: center;"><?php echo $pno++;?></td>
<td>
<?php echo $q2['title']; ?>
</td>
<td>
<p><?php echo $q2['date']; ?></p>
<p id="update_data" style="display:none">修改:eric<br />2012-6-21</p>
</td>
<td>
<input type="button" name="<?php echo $q2['sno'];?>" class="update" value="修改" />
<input type="button" name="<?php echo $q2['sno'];?>" class="del" value="刪除" />
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4" class="td1"><?php echo $getpageinfo['pagecode']; //顯示分頁的html代碼 第一頁 上一頁 1 2 3 4 5 6 7 8 9 10 下一頁 最後頁 ?></td>
</tr>
</table>
</div>
<!--左選單-->
<div id="sidebar">
<?php require_once("menu.php"); ?>
</div>
</div>
</div>
</body>
</html>
==================
key.php
<?php
//開啟session功能
if(!isset($_SESSION)){
session_start();
}
//仿止非法登入
if($_SESSION['key']==""){
header("Location:index.php");
}
?>
==================
page.php
<?php
//分頁程式: 最前頁 上一頁 1 2 3 4 5 6 7 8 9 下一頁 最後頁========================================
// function page($page,$total,$phpfile,$pagesize=1,$pagelen=9){
function page($idx,$page,$total,$phpfile,$pagesize=10,$pagelen=9){
$pagecode = ''; //定義變量,存放分頁生成的HTML
$page = intval($page); //避免非數字頁碼
$total = intval($total); //保證總記錄數值類型正確
if(!$total) return array(); //總記錄數為零返回空數組
$pages = ceil($total/$pagesize); //計算總分頁
//處理頁碼合法性
if($page<1) $page = 1;
if($page>$pages) $page = $pages;
//計算查詢偏移量
$offset = $pagesize*($page-1);
//頁碼範圍計算
$init = 1;//起始頁碼數
$max = $pages;//結束頁碼數
$pagelen = ($pagelen%2)?$pagelen:$pagelen+1; //頁碼個數
$pageoffset = ($pagelen-1)/2; //頁碼個數左右偏移量
//生成html
if($pages!=1){
// $pagecode='<div class="page">';
$pagecode='<div id="page"><ul>';
// $pagecode.="<span>$page/$pages</span> "; //第幾頁,共幾頁
}
//如果是第一頁,則不顯示第一頁和上一頁的連接
if($page!=1){
// $pagecode.="<a href=\"{$phpfile}?page=1&id=$idx\">最前頁</a> "; //第一頁
$pagecode.="<li><a href=\"{$phpfile}?page=1&id=$idx\">最前頁</a></li>"; //第一頁
// $pagecode.="<a href=\"{$phpfile}?page=".($page-1)."&id=$idx\">上一頁</a> "; //上一頁
$pagecode.="<li><a href=\"{$phpfile}?page=".($page-1)."&id=$idx\">上一頁</a></li>"; //上一頁
}
//分頁數大於頁碼個數時可以偏移
if($pages>$pagelen){
//如果當前頁小於等於左偏移
if($page<=$pageoffset){
$init=1;
$max = $pagelen;
}else{//如果當前頁大於左偏移
//如果當前頁碼右偏移超出最大分頁數
if($page+$pageoffset>=$pages+1){
$init = $pages-$pagelen+1;
}else{
//左右偏移都存在時的計算
$init = $page-$pageoffset;
$max = $page+$pageoffset;
}
}
}
//生成html(頁碼)
for($i=$init;$i<=$max;$i++){
if($pages!=1){ //總頁數不等於1才列出頁碼
if($i==$page){
// $pagecode.=' <span>'.$i.'</span> ';
$pagecode.='<li class="current-tag"><a href="#">'.$i.'</a></li>';
}else{
// $pagecode.="<a href=\"{$phpfile}?page={$i}&id=$idx\">$i</a>";
$pagecode.="<li><a href=\"{$phpfile}?page={$i}&id=$idx\">$i</a></li>";
}
}
}
if($page!=$pages){
// $pagecode.=" <a href=\"{$phpfile}?page=".($page+1)."&id=$idx\">下一頁</a> "; //下一頁
$pagecode.="<li><a href=\"{$phpfile}?page=".($page+1)."&id=$idx\">下一頁</a></li>"; //下一頁
// $pagecode.="<a href=\"{$phpfile}?page={$pages}&id=$idx\">最後頁</a>"; //最後一頁
$pagecode.="<li class=\"final-page\"><a href=\"{$phpfile}?page={$pages}&id=$idx\">最後頁</a></li>"; //最後一頁
}
if($pages!=1){
// $pagecode.='</div>';
$pagecode.='</ul></div>';
}
return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize);
}
?>
沒有留言:
張貼留言