|
|
|
มีปัญหาเรื่องการใส่ variable ลงไปในลิ้งอ่าค่ะ แล้วก็งงว่าจะโชว์product ตามcondition where ได้ยังไงอ่าค่ะถ้าคลิกตรงเมนูอ่า |
|
|
|
|
|
|
|
โค้ดตามนี้นะคะ..
หน้า menubar.php ค่ะCode (PHP)
<? print "<table id=sideMenu> <tr> <td width=200>";
include('onlineshop.inc.php');
print "<ul id=sideMenu> SHOP BY CATEGORY";
$sql = "select * from Category"; // คิวรี่ category มาจากตารางใน db ค่ะ
$dbquery = mysql_db_query($dbname, $sql);
$num_rows = mysql_num_rows($dbquery);
$i=0;
while ($i < $num_rows)
{
$result = mysql_fetch_array($dbquery);
$cat_id = $result[Cat_Id];
$cat_name = $result[Cat_Name];
print "<li> <a id=subSideMenu href='index.php?Cat_Id=$cat_id'> $cat_name </a></li>"; //เวลาคลิกลิ้งแล้วจะให้มันส่งค่าไปหน้า index.php เพื่อจะได้โชว์ product ตามไอดี category อ่าค่ะ
$i++;
}
print "</ul>";
print "<ul id=sideMenu> SHOP BY COLOR";
$sql = "select * from Color";
$dbquery = mysql_db_query($dbname, $sql);
$num_rows = mysql_num_rows($dbquery);
$i=0;
while ($i < $num_rows)
{
$result = mysql_fetch_array($dbquery);
$color_id = $result[Color_Id];
$color_name = $result[Color_Name];
print "<li> <a id=subSideMenu href=''> $color_name </a></li>";
$i++;
}
print "</ul>";
print "<ul id=sideMenu> SHOP BY OCCASION </ul>";
print "<ul id=sideMenu> SHOP BY SIZE </ul>";
print "<ul id=sideMenu> SHOP BY WEARER";
$sql = "select * from Wearer";
$dbquery = mysql_db_query($dbname, $sql);
$num_rows = mysql_num_rows($dbquery);
$i=0;
while ($i < $num_rows)
{
$result = mysql_fetch_array($dbquery);
$wearer_id = $result[Wearer_Id];
$wearer_type = $result[Wearer_Type];
print "<li> <a id=subSideMenu href=''> $wearer_type </a></li>";
$i++;
}
print "</ul>";
print "<ul id=sideMenu> SHOP BY MATERIAL";
$sql = "select * from Material";
$dbquery = mysql_db_query($dbname, $sql);
$num_rows = mysql_num_rows($dbquery);
$i=0;
while ($i < $num_rows)
{
$result = mysql_fetch_array($dbquery);
$mat_id = $result[Mat_Id];
$mat_name = $result[Mat_Name];
print "<li> <a id=subSideMenu href=''> $mat_name </a></li>";
$i++;
}
print "</ul>";
print "</td></tr></table>";
?>
หน้า index.php
Code (PHP)
<!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>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<script type="text/javascript" src="myjs.js">
</script>
<script type="text/javascript" src="framework.js">
</script>
<script>
function addCart(id){
var data = "id=" + id;
var url = "add_cart.php";
ajaxLoad('post', url, data, "cart");
}
function readCart() {
ajaxLoad('post', "read_cart.php", null, "cart");
}
</script>
</head>
<body>
<div>
<? include('menubar.php'); ?>
</div>
<? include('onlineshop.inc.php');?>
<table width="1000" align="center" border="0">
<tr>
<td align="left"> <? include('sidemenu.php');?>
<table id="cart" bgcolor="#FF0066">
<tr> <td> <script> readCart(); </script> </td></tr>
</table>
</td>
<td>
<table width="100%" border="0">
<tr> <td width="30%"> </td>
<td width="50%"> </td>
<td> </td>
</tr>
<?php include("paging.inc.php");
$current_page=1;
if(isset($_GET['page']))
{
$current_page=$_GET['page'];
}
$rows_per_page = 9;
$start_row = paging_start_row($current_page,$rows_per_page);
//จะรับค่า category id จากที่คลิกตรงเมนู มายังไงอ่ะคะ เพื่อที่จะให้มันโชว์เฉพาะโปรดักตามcategory นั้นๆอ่ะค่ะ
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM product LIMIT $start_row, $rows_per_page;";
$result=mysql_query($sql);
$found_rows=mysql_query("SELECT FOUND_ROWS();");
$total_rows=mysql_result($found_rows,0,0);
$total_pages = paging_total_pages($total_rows, $rows_per_page);
if($total_rows==0){
echo "<caption><b> no result </b></caption>";
}
else {
$stop_row = paging_stop_row($start_row, $rows_per_page, $total_rows);
echo "<caption><b> product no.". ($start_row +1) . "-" . "$stop_row from total $total_rows </b></caption>";
}
$count=0;
echo "<table border=0 align=center >";
while($objResult = mysql_fetch_array($result))
{
$PID = $objResult['Product_Id'];
$Pname = $objResult['Product_Name'];
$Pdesc1 = $objResult['Product_Des'];
$Pdesc = substr($Pdesc1,0,80). "..." ;
$Pprice = $objResult['Product_Prices'];
$Ppic = $objResult['Product_Pict'];
if($count==0)
{
echo "<tr> <td width=220><div id=product> <img align=center width=115 src='$Ppic?Product_Id=$PID'> </div>";
echo "<div id=product> <br><b> $Pname </b></div>";
echo "<div id=product> <br> $Pdesc </div>";
echo "<div id=product> <br> $Pprice </div>";
echo "<div id=product> <br> <input type=button value='add to cart' > </div>";
echo "</td>";
$count++;
}
else if($count==1)
{
echo "<td width=220> <div id=product> <img width=115 src='$Ppic?Product_Id=$PID'> </div>";
echo "<div id=product> <br><b> $Pname </b> </div>";
echo "<div id=product> <br> $Pdesc </div>";
echo "<div id=product> <br> $Pprice </div>";
echo "<div id=product> <br> <input type=button value='add to cart' > </div>";
echo "</td>";
$count++;
}
else if($count==2)
{
echo "<td width=220> <div> <img width=115 src='$Ppic?Product_Id=$PID'> </div>";
echo "<div id=product> <br><b> $Pname </b> </div>";
echo "<div id=product> <br> $Pdesc </div>";
echo "<div id=product> <br> $Pprice </div>";
echo "<div id=product> <br> <input type=button value='add to cart' > </div>";
echo "</td>";
echo "</tr>";
$count=0;
}
} echo "</table>";
?>
<p align="right"> <br />
<?php
$page_range=5;
$qry_str="";
//$total_pages = paging_total_pages($total_rows, $rows_per_page);
$pagenum = paging_pagenum($current_page, $total_pages, $page_range, $qry_str); // ต้องใส่ variable ตาม condition ของ sql ด้วยไม๊คะ เพื่อที่จะให้เพจมันหน้าอื่นๆโชว์สินค้าถูกต้องตาม condition อ่ะค่ะ
?>
<?
echo "page:" . $pagenum;
?>
</p>
</table>
</td>
</tr>
</table>
<div>
<? include('footer.php'); ?>
</div>
</body>
</html>
แล้วถ้าเรามีทำการแบ่งเพจไว้อ่ะคะ เราจะส่งค่า category id ไปในฟังชั่นแบ่งเพจยังไงอ่ะคะ
นี่หน้าฟังก์ชั่นแบ่งเพจนะคะ
paging.inc.php..
Code (PHP)
<?php
function paging_start_row($current_page, $rows_per_page)
{
return ($current_page - 1) * $rows_per_page;
}
function paging_total_pages($total_rows, $rows_per_page)
{
return ceil($total_rows/$rows_per_page);
}
function paging_stop_row($start_row, $rows_per_page, $total_rows) {
return (($start_row + $rows_per_page) < $total_rows) ? ($start_row + $rows_per_page) : $total_rows;
}
function paging_pagenum($current_page, $total_pages, $page_range, $query_string)
{
$page_start = $current_page - $page_range;
$page_end = $current_page + $page_range;
if($page_start<1) {
$page_end += 1-$page_start;
$page_start = 1;
}
if($page_end>$total_pages) {
$diff = $page_end - $total_pages;
$page_start -= $diff;
if($page_start <1) {
$page_start = 1;
}
$page_end = $total_pages;
}
$url = $_SERVER['PHP_SELF']."?".$query_string;
$result ="";
if($current_page >1) {
$pg = $current_page - 1;
$result .= " " ;
$result .= "<a href=\"$url&page=$pg\"> previous </a>";
}
if($page_start >1) {
$pg = $page_start -1;
$result .= " ";
$result .= "<a href=\"$url&page=$pg\">...</a>";
}
for($i=$page_start; $i<= $page_end; $i++) {
$result .= " ";
if($i == $current_page) {
$result .= $i;
}
else {
$result .= "<a href=\"$url&page=$i\"> $i </a>";
}
$result .= " ";
}
if($page_end < $total_pages) {
$pg = $page_end +1;
$result .= " ";
$result .= "<a href=\"$url&page=$pg\">...</a>";
}
if($current_page < $total_pages) {
$pg = $current_page +1;
$result .= " ";
$result .= "<a href=\"$url&page=$pg\"> next </a>";
}
if($result == "") {
return "1" ;
}
else {
return $result;
}
}
?>
Tag : PHP, MySQL, HTML/CSS
|
|
|
|
|
|
Date :
2011-10-20 00:35:46 |
By :
gellarjung |
View :
884 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ดูตัวอย่างนี้ครับ ให้ส่งเป็น id ไป จากนั้นก็ไป WHERE ที่อีกหน้าครับ
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE CustomerID = '".$_GET["CusID"]."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if(!$objResult)
{
echo "Not found CustomerID=".$_GET["CusID"];
}
else
{
?>
Go to : PHP MySQL Edit/Update Record
|
|
|
|
|
Date :
2011-10-20 06:37:27 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เข้าใจแล้วค่ะ ขอบคุณมากๆเรยค่า
|
|
|
|
|
Date :
2011-10-20 12:34:39 |
By :
gellarjung |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|