2011-07-18 100 views
2

我知道我已經錯誤地編碼了這個,但似乎無法找到一種方法來糾正它。目的是要顯示下拉列表,填入mysql數據庫的結果。它目前正在爲每個地址顯示一個下拉菜單。我知道這是爲什麼發生,但似乎無法糾正它。回聲是否應該在while循環之外?或者位置正確但在其他地方錯了?如果有人可以檢查它,並告訴我我在哪裏與它錯誤,這將是好事。非常感謝。php while循環輸出產生每個項目的選擇

<?php 
    $customer = mysql_real_escape_string($_GET["customer"]);   
    $con = mysql_connect("localhost","root",""); 
    $db = "sample"; 
     if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

     mysql_select_db($db, $con); 
     $query_rs_select_address2 = sprintf("SELECT * FROM company_com where idcode_com = '$customer'"); 
     $rs_select_address2 = mysql_query($query_rs_select_address2, $con) or die(mysql_error()); 
     $row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2); 
     $totalRows_rs_select_address2 = mysql_num_rows($rs_select_address2); 

     while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2)) 
      { 
     $address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com']; 
     echo '<select name="customer">'.'<option value="">Select delivery address</option>'.'<option value="address">'.$address.'</option>'.'</select>'; 

      } 

?> 

回答

3

試試這個:

echo '<select name="customer">'; 
echo '<option value="">Select delivery address</option>'; 
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2)) 
{ 
    $address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com']; 
    echo '<option value="address">'.$address.'</option>'; 
} 
echo '</select>'; 
+0

完美。謝謝 – bollo

1

while環路產生每次一個select元件。

你應該把select的同時

echo "<select>"; 
while(/* while statement */){ 
    echo "<option></option>"; 
} 
echo "</select>"; 
+0

感謝您的幫助 – bollo

6

外面如果你的目標是有一個下拉將所有的選項,那麼你需要把回聲的打開和關閉標籤選擇外而和離開選項標籤的同時:

// also put first option on the outside as you *don't* want to repeat that. 
echo '<select name="customer"><option value="">Select delivery address</option>'; 
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2)) 
{ 
    // only place the code in here you want repeated for every value in the query 
    /* 
     have you considered concatenating in the query? 
     Basically: 
     select concat(address1_com, ' ', address2_com, ' ', address3_com, ' ' 
       town_com, ' ', postcode_com) as full_address From... 

     Sometimes the greater number of records means slower execution. 
     Of course, you should benchmark to be sure. 
    */ 
    $address= $row_rs_select_address2['address1_com']. " ". 
       $row_rs_select_address2['address2_com']. " ". 
       $row_rs_select_address2['address3_com']. " ". 
       $row_rs_select_address2['town_com']. " ". 
       $row_rs_select_address2['postcode_com']; 
    // notice I swapped out $uid for address. You want to have each option 
    // reflect a different value so that the POST gives a uid to the server 
    // You can probably get a uid from the primary key of the company_com 
    // table. 
    echo '<option value="$uid">'.$address.'</option>'; 

} 
echo '</select>'; 
+0

感謝您的幫助 – bollo

1

你而改變這一

echo '<select name="customer">'; 
echo '<option value="">Select delivery address</option>'; 
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2)) 
{   
    $address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com'];  
    echo '<option value="address">'.$address.'</option>' 
} 
echo '</select>'; 
+0

感謝您的幫助 – bollo

1

嘗試是這樣的:

echo '<select name="customer">'; 
while ($row_rs_select_address2 = mysql_fetch_assoc($rs_select_address2)) 
      { 
     $address=$row_rs_select_address2['address1_com']. " ". $row_rs_select_address2['address2_com']. " ". $row_rs_select_address2['address3_com']. " ". $row_rs_select_address2['town_com']. " ". $row_rs_select_address2['postcode_com']; 
     echo '<option value="">Select delivery address</option>'.'<option value="address">'.$address.'</option>'; 
      } 
echo '</select>'; 

你是(在錶行)的每個製作不同的,希望這有助於:)。

+0

'選擇送貨地址'選項應該在外面:-) –