2013-07-11 50 views
0

我必須使用MySQL表得到表值,即使用其他表。我得到了數組值Mysql的陣列中的數據不顯示錶

值第一表查詢:

$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']; 
       } 
        } 

相同的方式使用的值表

我的問題,這顯示值表continuely

  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>"; 
          } 

這種格式是不顯示正確的更值僅第一個值顯示多個時

+0

你的循環每次都打印相同的東西,如果你想要不同的東西,你需要告訴它打印一些不同的東西。真的,你應該把你的產品列表放到一個數組中,然後循環數組,現在,上面是隻是字符串。例如$數量,沒有什麼改變到產品的數量2 – Anigel

+0

請閱讀[正確的SQL轉義](http://bobby-tables.com/php),因爲你可能有一些嚴重的[SQL注入漏洞]( http://bobby-tables.com/)。使用'mysql_query'也是一個壞主意,因爲它將在未來版本的PHP中被刪除。 PDO是一種更易於正確使用的現代替代品。 – tadman

回答

0

這是一個很難回答,因爲那裏有很多的問題正在進行......

首先....除非這是私人/內部應用程序,否則不能將變量直接放入您的查詢中......查看適當的數據庫類,我可以推薦:http://www.imavex.com/php-pdo-wrapper-class/

其次,你的PHP代碼和html的混合程度太高了......我不打算進入太多,但總的來說你想保持這個東西分開......研究MVC編程模式。

現在回答你的問題...你看到相同的數據重複的原因是因爲你只使用一個變量,每次都被覆蓋...即, $ quantiy(這拼寫錯誤fyi)...所以要破解你的方法,將它分配給一個數組,然後使用$ i計數器就可以了,就像你在做$ productID

更好的方法是做這個東西在循環中,並避免首先分配變量...我假設你已經給消息var分配了東西......如果你可以跳過它並直接回顯出來,那可能會更好。

$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 ."' 

請原諒語法/命名約定。 ..上述命令無法在您的系統上正常運行,它只是爲了讓您知道該怎麼做。

+0

我會問「除非這是一個私人/內部應用程序,你不能直接將變量放入你的查詢中」。雖然你當然不應該在沒有某種形式的衛生條件的查詢中直接使用用戶輸入,但如果你應該是另一回事,那麼你肯定可以在查詢中使用變量。 – Anigel