在单个表中显示多个表中的数据

时间:2015-10-22 12:34:47

标签: php mysql

这是一个简单的php / mysql购物车系统。其中有两个页面具有不同的数据表。一个是men_products,另一个是women_products。我创建了不同的页面来显示两个表的数据,并且两个页面的行为都是"添加到购物车。"问题来自cart.php。我试图显示已添加到购物车但未能解决查询的两个表数据。我提供我的代码。如果任何人可以提供帮助,我们将不胜感激。

  

注意:women.php与men.php文件类似

的index.php

session_start();
if(isset($_GET['page'])){
$pages=array("men","cart","women","prod");
if(in_array($_GET['page'],$pages)){
    $page=$_GET['page'];
}else{
    $page="prod";
}
}else{
$page="prod";
}
?>
 <?php require($page . ".php"); ?>
 <h1>Cart</h1>
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Image</th>
            <th>Qty</th>
         </tr>
    <?php
    if(isset($_SESSION['cart'])){
      $sql = "SELECT men.* , women.*  FROM men_products men , women_products women WHERE men.id IN(";
            foreach($_SESSION['cart'] as $id => $value){
                $sql .=$id. ",";
            }
            $sql=substr($sql,0,-1).")  OR women.id IN(";
            foreach($_SESSION['cart'] as $id => $value){
                $sql .=$id. ",";
            }
            $sql=substr($sql,0,-1).") ORDER BY id ASC"; //Updated

        $query = mysqli_query($con, $sql);
        if(!empty($query)){
        while($row = mysqli_fetch_array($query)){
        ?>
        <tr>
        <td><?php echo $row['product_name']; ?></td>
        <td><img src="images/<?php echo $row['product_image']; ?>"></td>
         <td><?php echo $_SESSION['cart'][$row['id']]['quantity']; ?></td>
         </tr>
        <tr><td colspan="3"><a href="index.php?page=cart">Go To Cart</a>

men.php

     <?php
    if(isset($_GET['action']) && $_GET['action']=="add"){
    $id=intval($_GET['id']);
    if(isset($_SESSION['cart'][$id])){
        $_SESSION['cart'][$id]['quantity']++;
    }else{
        $sql_p="SELECT * FROM men_products WHERE id={$id}";

        $query_p=mysqli_query($con, $sql_p);
        if(mysqli_num_rows($query_p)!=0){
            $row_p=mysqli_fetch_array($query_p);
            $_SESSION['cart'][$row_p['id']]=array("quantity" => 1, "price" => $row_p['product_price']);
        }else{
            $message="Product ID is invalid";
        }
         }
        }
       ?>

       <?php
        if(isset($message)){
       echo "<h2>$message</h2>";    
        }
?>
<table border="1">
    <tr>
        <th>Name</th>
        <th>Picture</th>
        <th>Description</th>
        <th>Price</th>
        <th>Items Price</th>
    </tr>
    <?php
    $query = mysqli_query($con,"SELECT * FROM men_products ORDER BY product_name ASC");
    while($row=mysqli_fetch_assoc($query)){
    ?>
    <tr>
        <td><?php echo $row['product_name']; ?></td>
        <td><img src="images/<?php echo $row['product_image']; ?>" width="120px" height="120px"></td>
        <td><?php echo $row['product_desc']; ?></td>
        <td><?php echo "$" . $row['product_price']; ?></td>
        <td><a href="index.php?page=men&action=add&id=<?php echo $row['id']; ?>">Add to Cart</a></td>
    </tr>
    <?php
    }
    ?>
</table>

Cart.php

<?php
    if(isset($_POST['submit'])){
        if(!empty($_SESSION['cart'])){
        foreach($_POST['quantity'] as $key => $val){
            if($val==0){
                unset($_SESSION['cart'][$key]);
            }else{
                $_SESSION['cart'][$key]['quantity']=$val;
            }
        }
        }
    }
?>

<h1>View Cart || <a href="index.php?page=men">Men Products</a>||<a href="index.php?page=women">Women Products</a></h1>
<form method="post" action="index.php?page=cart">
<table>
    <tr>
        <th>Name</th>
        <th>Quantity</th>
        <th>Price</th>
        <th>Subtotal</th>
    </tr>
    <?php
    $sql = "SELECT * FROM men_products  WHERE id IN("; // stuck here. What query will be used here to display both pages add to cart data
            foreach($_SESSION['cart'] as $id => $value){
            $sql .=$id. ",";
            }
            $sql=substr($sql,0,-1) . ") ORDER BY id ASC";
            $query = mysqli_query($con, $sql);
            $totalprice=0;
            if(!empty($query)){
            while($row = mysqli_fetch_array($query)){
                $subtotal= $_SESSION['cart'][$row['id']]['quantity']*$row['product_price'];
                $totalprice += $subtotal;
    ?>
    <tr>
        <td><?php echo $row['product_name']; ?></td>
        <td><input type="text" name="quantity[<?php echo $row['id']; ?>]" size="6" value="<?php echo $_SESSION['cart'][$row['id']]['quantity']; ?>"> </td>
        <td><?php echo "$" .$row['product_price']; ?></td>
        <td><?php echo "$" .$_SESSION['cart'][$row['id']]['quantity']*$row['product_price']. ".00"; ?></td>
    </tr>

    <?php
            }
            }else{
    ?>
            <tr><td colspan="4"><?php echo "<i>Add product to your cart."; ?></td></tr>
    <?php
            }
    ?>
    <tr>
        <td colspan="3">Total Price: <h1><?php echo "$" ."$totalprice". ".00"; ?></h1><td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:1)

如果我正确理解了这个问题,您需要从两个表中选择数据。然后,您需要选择这两个表:

SELECT men.*, women.*
FROM men_products men, women_products women
WHERE men.id IN(...) OR women.id IN(...)

然后,您将获得men.idwomen.id等结果,因此您必须相应更改检查是否已包含在购物车中。

相关问题