通过过去的事务从数据库中检索数据

时间:2014-01-17 14:20:46

标签: php

我想为我的购物车做一个过去的交易(我正在做一个电子商务网站),只是从教程中试用。

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

然而, 用我的代码完成,如图所示,我能够在数据库中检索我的项目。 但是,标题“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>

1 个答案:

答案 0 :(得分: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>";
}