0

我已经使用 MySQL Table 获取表上的值,该值使用了另一个表。我得到了数组值

第一个表查询:

    $ordersdetail = "SELECT * FROM `order_item` WHERE `Order_ID`='".$orderid ."'";
$customerorder = mysql_query($ordersdetail );
                   $i=0;
             while($rows = mysql_fetch_array($customerorder ))
             {
                $orderproduct= $rows['Order_product_ID'];
                $orderids= $rows['Order_ID'];
                $productId[$i] = $rows['Product_ID'];
                $sizeId[$i] = $rows['Size_ID'];

                 $colorId[$i] = $rows['Color_ID'];
                  $quantiy = $rows['Order_Quantity'][$i];

                  $price = $rows['Unit_Price'][$i];
                  $subTotal = $rows['Sub_Total'];
                  $customerccode = $rows['Record_Status'];
                  $customerphone = $rows['Created_Time'];
                  $i++;
                }     

表列 productid 数组值被另一个表使用:

给定:

       for($i=0;$i<count($productId);$i++)  
             { 
        $product = "SELECT * FROM `product` WHERE `Product_ID`='".$productId[$i]."'";
$products = mysql_query($product);

             while($rows = mysql_fetch_array($products))         
               { 
                   $productnames = $rows['Product_Name'];


               }

                  }  

color id used :




     for($i=0;$i<count($colorId);$i++) 
                { 
       $color = "SELECT * FROM `color` WHERE `Color_ID`='".$colorId[$i] ."'";
$cname = mysql_query($color);

             while($rows = mysql_fetch_array($cname))         
               { 
                   $colorname = $rows['Color_Name'];
               }
                  }

同样的方式使用值表

我的问题值显示表继续

         table structure  



        $message .= "<table width='100%' border='1' cellspacing='0' cellpadding='1'>
          <thead>
         <tr style='background: #eee;'>
          <th><strong>Quantity</strong> </th>
          <th><strong>ProductName</strong> </th>
         <th><strong>Size</strong> </th>
           <th><strong>Style</strong> </th>
          <th><strong>Price</strong> </th>
          <th><strong>Total</strong> </th>

            </tr>
                 ";
          for($i=0;$i<count($productId);$i++)  
             { 
                      $message .=" <tr style='color: #c40000;'>
                      <th>" .$quantiy. "</th>
                     <th>" .$productnames. "</th>
                         <th>" .$sizename. "</th>
                            <th>" .$colorname. "</th>
                               <th>" . $price. "</th>
                                   <th>" . $subTotal. "</th>  
                                                        </tr>";
                           }

这种格式是正确的 更多的值不显示只第一个值显示多次

4

1 回答 1

0

这是一个很难回答的问题,因为有很多问题正在发生......

首先....除非这是一个私有/内部应用程序,否则您不能将变量直接放入您的查询中...查看一个适当的数据库类,我可以推荐:http ://www.imavex.com/php-pdo -包装类/

其次,您将 php 代码和 html 混合得太多...我不会过多介绍,但总的来说您希望将这些东西分开...研究 MVC 编程模式。

现在回答您的问题...您看到相同数据重复的原因是因为您只使用了一个每次都会被覆盖的变量...即。$quantiy(拼写错误仅供参考)...所以要破解您的方法,将其分配给一个数组,然后像使用 $productID 一样在其上使用 $i 计数器

更好的方法是在循环中执行此操作并避免首先分配变量...我假设您已将内容分配给消息变量...如果您可以跳过它并直接将其回显可能会更好。

$ordersdetail = "SELECT * FROM `order_item` WHERE `Order_ID`='".$orderid ."'";
$customerorder = mysql_query($ordersdetail );
$i=0;
while($rows = mysql_fetch_array($customerorder ))
    $product = "SELECT * FROM `product` WHERE `Product_ID`='".$row[$i]."'";
    $products = mysql_query($product);
    $color = "SELECT * FROM `color` WHERE `Color_ID`='".$colorId[$i] ."'";
    $cname = mysql_query($color);


   Now you would create your table from the $rows data directly and when you goto the      color/size stuff you could look through in there.

}

现在我不是 100% 确定它会为您工作,但您可能可以使用 JOINS 使用单个查询执行与上述类似的操作

SELECT * FROM `order_item` LEFT JOIN product ON order_item.productID = product.productID LEFT JOIN color ON order_item.colorID = color.colorID WHERE order_item.`Order_ID`='".$orderid ."'

请原谅语法/命名约定...上面的命令将无法在您的系统上正常运行,它只是为了让您知道该怎么做。

于 2013-07-11T14:13:33.480 回答