2014-01-17 80 views
0

我想为我的购物车(我正在做一个电子商务网站)完成过去的交易,只是从教程中尝试。通过过去的交易从数据库中检索数据

我与用户名相比较,如果用户名登录在数据库中,它会显示过去的记录。 否则,它会说没有过去的交易。

但是,我用我的代码完成了 ,如图所示,我可以检索数据库中的项目。 但是,对于每个不同的物品检索,标题“item,package,etcetc”也会重复。 我可以知道如何摆脱标题,我只希望它显示一次作为我的标题。

$des = $row_supermarketcart['productdes']; 
$pack = $row_supermarketcart['package']; 
$price = $row_supermarketcart['itemprice']; 
$qty = $row_supermarketcart['qty']; 
$ddate = $row_supermarketcart['ddate']; 

if ($row_supermarketcart['username'] == $_SESSION['MM_Username']) 
{ 
    echo "<table><tr>"; 
      echo "<td>items</td>"; 
     echo "<td>Package</td>"; 
     echo "<td>Price</td>"; 
     echo "<td>Quantity</td>"; 
     echo "<td>Date of Purchase</td>"; 
     echo "</tr>"; 
     echo "<tr>"; 
     echo "<td>". $des ."</td>"; 
     echo "<td>". $pack. "</td>"; 
     echo "<td>$" .$price. "</td>"; 
     echo "<td>". $qty. "</td>"; 
     echo "<td>".$ddate."</td>"; 
     echo "</tr>"; 
     echo "</table>"; 
      var_dump($row_supermarketcart); 
} 
else 
{ 
     echo "There are nothing in your cart"; 
} 
?> 

这是我的全部代码

<?php require_once('Connections/MyDatabase.php'); ?> 
    <?php 
    //initialize the session 
    if (!isset($_SESSION)) { 
     session_start(); 
    } 

    // ** Logout the current user. ** 
    $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true"; 
    if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){ 
     $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']); 
    } 

    if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){ 
     //to fully log out a visitor we need to clear the session varialbles 
     $_SESSION['MM_Username'] = NULL; 
     $_SESSION['MM_UserGroup'] = NULL; 
     $_SESSION['PrevUrl'] = NULL; 
     unset($_SESSION['MM_Username']); 
     unset($_SESSION['MM_UserGroup']); 
     unset($_SESSION['PrevUrl']); 

     $logoutGoTo = "login.php"; 
     if ($logoutGoTo) { 
     header("Location: $logoutGoTo"); 
     exit; 
     } 
    } 
    ?> 
    <?php 
    if (!function_exists("GetSQLValueString")) { 
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    { 
     if (PHP_VERSION < 6) { 
     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; 
     } 

     $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); 

     switch ($theType) { 
     case "text": 
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
      break;  
     case "long": 
     case "int": 
      $theValue = ($theValue != "") ? intval($theValue) : "NULL"; 
      break; 
     case "double": 
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; 
      break; 
     case "date": 
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; 
      break; 
     case "defined": 
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; 
      break; 
     } 
     return $theValue; 
    } 
    } 

    mysql_select_db($database_MyDatabase, $MyDatabase); 
    $query_supermarketcart = "SELECT * FROM supermarketcart"; 
    $supermarketcart = mysql_query($query_supermarketcart, $MyDatabase) or die(mysql_error()); 
    $row_supermarketcart = mysql_fetch_assoc($supermarketcart); 
    $totalRows_supermarketcart = mysql_num_rows($supermarketcart); 
    $query_cart = "SELECT * FROM user_data"; 
    $cart = mysql_query($query_cart, $MyDatabase) or die(mysql_error()); 
    $row_cart = mysql_fetch_assoc($cart); 
    $totalRows_cart = mysql_num_rows($cart); 
    ?> 


    <!DOCTYPE HTML> 
    <html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Your Past Transaction</title> 
    </head> 

    <body background="background.jpg"> 
    <table width="1024" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#EBF4FA"> 
     <tr> 
     <td><img src="logo.png" width="450" height="86" hspace="50"> 
     </td> 
     <td> 
      <blockquote><h4><?php echo "Welcome,".($_SESSION['MM_Username']) ?></h4> </blockquote> 
     <blockquote><img src="cart.png" alt="cart" width="35" border="0" usemap="#Map"> 
     <a href="feedback.php">Feedback</a> 
     <a href="<?php echo $logoutAction ?>">Logout</a> 

     </blockquote></td> 
     </tr> 
    </table> 
    <br> 

    <table width="1024" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#EBF4FA"> 
    <tr> 
    <td> 

    <?php 

do{ 
$des = $row_supermarketcart['productdes']; 
$pack = $row_supermarketcart['package']; 
$price = $row_supermarketcart['itemprice']; 
$qty = $row_supermarketcart['qty']; 
$ddate = $row_supermarketcart['ddate']; 


if ($row_supermarketcart['username'] == $_SESSION['MM_Username']) 
{ 
    echo "<table><tr>"; 
      echo "<td>items</td>"; 
      echo "<td>Package</td>"; 
      echo "<td>Price</td>"; 
      echo "<td>Quantity</td>"; 
      echo "<td>Date of Purchase</td>"; 
      echo "</tr>"; 
      echo "<tr>"; 
      echo "<td>". $des ."</td>"; 
      echo "<td>". $pack. "</td>"; 
      echo "<td>$" .$price. "</td>"; 
      echo "<td>". $qty. "</td>"; 
      echo "<td>".$ddate."</td>"; 
      echo "</tr>"; 
      echo "</table>"; 

} 

else 
{ 
     echo "There are nothing in your cart"; 
} 

} while ($row_supermarketcart = mysql_fetch_assoc($supermarketcart)); 
?> 




    </td> 
    </tr> 
    </td> 
    </tr> 
    </table> 

    </body> 
    </html> 
+0

使用'的var_dump($ row_supermarketcart)'来帮助诊断问题。它看起来像你期望的字符串实际上是一个数组。 –

+0

array 'id'=> string'97'(length = 2) 'itemid'=> string'1'(length = 1) 'productdes'=> string'CADBURY BLOCK MILK CHOCOLATE - DAIRY MILK'(length =长度= 4) '包'=>字符串'220g'(长度= 4) 'itemprice'=>字符串'5.20'(长度= 4) 'qty'=>字符串'2'(长度= 1) ' username'=> string'test'(长度= 4) 'ddate'=>字符串'2014年1月17日星期五,13:58:51'(长度= 30) – user3205175

+0

@DigitalChris这就是它说的.. 为什么这样? 对不起,我对php – user3205175

回答

0

您需要循环查询:

if ($row_supermarketcart['username'] == $_SESSION['MM_Username']) 
{ 
    echo "<table><tr>"; 
      echo "<td>items</td>"; 
      echo "<td>Package</td>"; 
      echo "<td>Price</td>"; 
      echo "<td>Quantity</td>"; 
      echo "<td>Date of Purchase</td>"; 
      echo "</tr>"; 

    foreach($row_supermarketcart as $row) { 
       echo "<tr>"; 
       echo "<td>" . $row['productdes'] . "</td>"; 
       echo "<td>" . $row['package'] . "</td>"; 
       echo "<td>" . $row['itemprice'] . "</td>"; 
       echo "<td>" . $row['qty'] . "</td>"; 
       echo "<td>" . $row['ddate'] . "</td>"; 
       echo "</tr>"; 
    } 
      echo "</table>"; 
} 
+0

[]括号不起作用。我使用dreamweaver,它在这些行中被加亮显示为RED – user3205175

+0

我将它改为回显“​​”。$ des。 “”;代替。 但是,它重复所有相同的项目 – user3205175

+0

Dreamweaver是否更喜欢它,如果它的格式如此呢? –

相关问题