2011-09-12 6 views
0

私は大きなジレンマを持っています。私はMySQLからフォームにデータを生成しています。問題は、SQLで生成された最初の行が0のときに、最初の値は1です。 このクエリ内からいくつかのデータをスローしたくない場合は問題ありません。ここで生成されたHTMLコードさSelect-Optionのnum値とSQLの数値num_row

<select id="exp_address" name="exp_address" onChange="document.getElementById('exp_city').value = this.options[this.selectedIndex].getAttribute('data-exp_city'); document.getElementById('exp_dist').value = this.options[this.selectedIndex].getAttribute('data-exp_distr'); document.getElementById('exp_address_val').value = this.options[this.selectedIndex].getAttribute('data-exp_city_val')"> 
<?php 
for($i = 0; $i < $expselnum; $i++){ 
    $exps_add_id = mysql_result($expsel,$i,'address_id'); 
    $exps_address = mysql_result($expsel,$i, "address"); 
    $exps_city = mysql_result($expsel,$i, "city"); 
    $exps_distr_val = mysql_result($expsel,$i, "district"); 
    echo "<option value=$exps_add_id data-num=$i data-exp_city = '$exps_city' data-exp_distr = '$exps_distr_val' data_exp_city_val = '$exps_city'>$exps_address</option>";  
} 
?> 
</select> 

と:あなたはオプションタグの値とデータ-numはATTを比較すると

<select onChange="Here is some JS to change the value of District and City inputs"> 
<option value=1 data-num=0 data-exp_city = 'BUCURESTI' data-exp_distr = 'Bucuresti' data_exp_city_val = 'BUCURESTI' [selected by default]>Another Address in RO</option><option value=2 data-num=1 data-exp_city = 'OTOPENI-IF' data-exp_distr = 'Ilfov' data_exp_city_val = 'OTOPENI-IF'>Some address in RO</option></select> 
<input name="exp_city" type="text" id="exp_city" style="text-align:left;" value="OTOPENI-IF" size="30" readonly="readonly"> 

、あなたは彼らがしていることがわかります。しかし、ここでのコードですユニットごとに違います、なぜでしょうか。オプションの最初の値はページランディングで選択されますが、都市入力にはクエリの次の値が表示されます。 どうすればこの問題を解決できますか? あらかじめありがとうございます。

+0

あなたの方法は、古代と醜いです。まず配列にデータを取得し、それを使ってHTMLを生成します。私はそれが副作用としてあなたの現在のすべての問題を解決すると確信しています –

+0

@ColShrapnelありがとうございました、私はPHPとMySQLの本を手に入れて、Arrayのalterntiveを取得する方法を見つける、残念ながら私はグラフです。デザイナー。 – sealview

答えて

0

非常に微妙ではありませんが、私はShrapnelのコメントに同意します。しかし、これは(私は実際にも、質問を理解していない)あなたの「問題」を解決するかもしれません:

for($i = 0; $i < $expselnum; $i++){ 

変化に:mysqlのからデータを取得する

for($i = 1; $i < $expselnum; $i++){ 
+0

ありがとう@rijk、この変更は、テーブルの最初のレコードを非表示にします。 – sealview