首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mysql数组数据不显示表格

Mysql数组数据不显示表格
EN

Stack Overflow用户
提问于 2013-07-11 21:47:40
回答 1查看 171关注 0票数 0

我使用了MySQL表获取表上值,这些值使用了另一个表.i获取数组值

第一个表查询:

代码语言:javascript
复制
    $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数组值在另一个表中使用:

给定:

代码语言:javascript
复制
       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'];
               }
                  }

与使用值表的方式相同

我对连续显示表的疑问

代码语言:javascript
复制
         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>";
                           }

此格式正确,不显示更多值,仅显示第一个值多次显示

EN

回答 1

Stack Overflow用户

发布于 2013-07-11 22:13:33

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

首先off....unless这是一个私有/内部的应用程序,你不能把变量直接放到你的query...look中,放到一个合适的数据库类中,我可以推荐:http://www.imavex.com/php-pdo-wrapper-class/

其次,你把你的php代码和html混在一起太多了……我不打算过多地讨论这个,但总的来说,你想要保留这些东西seperate...research MVC编程模式。

现在回答您的question...The,您看到相同数据重复的原因是因为您只使用了一个变量,该变量在每个time...ie中都会被覆盖。$quantiy (拼写错误供参考)...so破解您的方法,将其分配给一个数组,然后对其使用$i计数器,就像对$productID所做的那样

一个更好的方法是在循环中做这些事情,并避免在第一个地方赋值变量...我假设你已经给消息var...if赋值了,你可以跳过它,直接回显它,这可能会更好。

代码语言:javascript
复制
$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.

}

现在,我不能百分之百地确定它是否适用于您,但是您可以使用JOINS使用单个查询来执行类似于上面的操作

代码语言:javascript
复制
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 ."'

请原谅上面的语法/naming conventions...the命令不能在你的系统上正常运行,它只是给你一个如何做的想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17595133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档