2014-11-21 50 views
0

中的一個下拉菜單我創建了一個相當大的動態站點作爲一個項目,並且除了一個函數之外,一切都運行正常。從數據庫中獲取一個值,然後使用forloop來填充php

我使用PHP來顯示從我的數據庫填充的HTML表。我可以獲取值爲ip_stock(這是一個int值),我想獲取該值並通過它進行循環,以填充表中要購買的每個項目的下拉菜單。每個項目在['ip_stock']中將有不同的值。這裏是我的代碼,減去頂部的一些HTML,這不是非常相對的,但是連接已經建立並且成功,我的forloop不起作用,並且如果我將它放入選擇代碼本身,則不起作用。

<?php 

    $pagerows = 5; 

    if (isset($_GET['p']) && is_numeric($_GET['p'])) { 
     $pages=$_GET['p']; 
    } else { 
     $query = "SELECT COUNT(ip_id) FROM in_product"; 
     $result = mysqli_query($dbhandle, $query); 
     $rows = @mysqli_fetch_array($result, MYSQLI_NUM); 
     $records = $rows[0]; 

    if ($records > $pagerows) { 
     $pages = ceil($records/$pagerows); 
    }else{ 
     $pages = 1; 
     } 
    } 

    if (isset($_GET['s']) && is_numeric ($_GET['s'])) { 
     $start = $_GET['s']; 
    }else{ 
     $start = 0; 
    } 


    echo "<table class='tableadmin2'> 
    <tr> 
    <td class='td2'><b>Add&nbsp;to&nbsp;Cart: </b></td> 
    <td class='td2'><b>Product Information: </b></td> 
    <td class='td2'><b>Product Photo: </b></td> 
    <td class='td2'><b>Select Amount: </b></td> 
    </tr>"; 

    $display6="SELECT * 
     FROM in_product LIMIT $start, $pagerows; " ; 
    $displayResult6 = @mysqli_query($dbhandle, $display6) 
       or die(mysqli_error($dbhandle)); 

    while($row6 = mysqli_fetch_array($displayResult6, MYSQLI_ASSOC)) { 
    $i = 1; 
    $x = $row6['ip_stock']; 
    $y = " "; 
    for($i = 1; $i <= $x; $i++) { 
    $y = "<option value=' . $i . '> $i </option>"; 
    }    
    echo "<tr> 
    <input type='hidden' id='prod' value='" . $row6['ip_id'] . "' /></td> 
    <td class='td2'><a href='addcartInstate.php?ip_id=" . $row6['ip_id'] . "'>Add To Cart&nbsp</a></td> 
    <td class='td2'><strong> " . $row6['ip_name'] . " </strong><br> " . $row6['ip_desc'] . " <br> $" . $row6['ip_price'] . " </td> 
    <td class='td2'><img alt='first' src=" . $row6['ip_image'] . " width='300' height='250'></td> 
    <td class='td2'><br> 
     <b>Quantity:&nbsp;&nbsp;</b><br> 
       <select id='orderIn_quantity' name='orderIn_quantity'> 
        <option value='Select'> Select </option> 
        $y            
       </select> 
    </td> 
    </tr>"; 
    } 
    echo "</table>"; 

    if(isset($_POST['orderIn_quantity'])) { 
     $orderIn_quantity = $_POST['orderIn_quantity']; 
     } 

    if($pages > 1) { 
     echo '<p class="table3">'; 
     $current = ($start/$pagerows) + 1; 
     if ($current != 1) { 
      echo '<a href="instate.php?s=' . ($start - $pagerows) . '&p=' . $pages . '">Previous Page </a>'; 
     } 
     if ($current != $pages) { 
      echo '<a href="instate.php?s=' . ($start + $pagerows) . '&p=' . $pages . '">Next Page </a> '; 
     } 
    } 
?> 
+2

你是什麼意思「不行」?有錯誤嗎?你在使用錯誤檢查嗎?你看過錯誤日誌嗎? – 2014-11-21 16:16:59

+0

忘記了添加這個...此代碼獲取數據庫中包含的值,但只顯示該值,不會通過該值循環以基於該值填充許多選擇。 – user3447733 2014-11-21 16:17:54

+1

歡迎來到計算器!我們要求的一件事是您將代碼與特定問題配對。就像@JayBlanchard說的,告訴我們你有什麼錯誤。定義「不起作用」,並顯示相關的代碼片段 – Cfreak 2014-11-21 16:19:28

回答

2

到目前爲止,我可以看到它只顯示最後一個值,正確嗎?因爲你用新的覆蓋你以前的選項。您需要使用string concatenation

您需要更改

$y = "<option value=' . $i . '> $i </option>"; 

// this will append new string to previous string 
$y .= "<option value=' . $i . '> $i </option>"; 
+0

完美的工作!我無法相信我如此親密。缺少一個 – user3447733 2014-11-21 16:21:42

+0

非常感謝您的幫助! – user3447733 2014-11-21 16:22:24

0

您需要添加到$ Y沒有覆蓋它。在回顯字符串時,你也應該堅持eiter級聯或變量解釋,所以當你輸出$ y時,要用級聯方式來做:

$y=''; 
for($i = 1; $i <= $x; $i++) { 
    $y =. "<option value='" . $i . "'>". $i ."</option>"; 
}    
echo "<tr> 
<input type='hidden' id='prod' value='" . $row6['ip_id'] . "' /></td> 
<td class='td2'><a href='addcartInstate.php?ip_id=" . $row6['ip_id'] . "'>Add To Cart&nbsp</a></td> 
<td class='td2'><strong> " . $row6['ip_name'] . " </strong><br> " . $row6['ip_desc'] . " <br> $" . $row6['ip_price'] . " </td> 
<td class='td2'><img alt='first' src=" . $row6['ip_image'] . " width='300' height='250'></td> 
<td class='td2'><br> 
    <b>Quantity:&nbsp;&nbsp;</b><br> 
      <select id='orderIn_quantity' name='orderIn_quantity'> 
       <option value='Select'> Select </option> 
       ". $y ."            
      </select> 
</td> 
</tr>"; 
相關問題