Select-Option num value vs SQL num_row

时间:2011-09-12 07:32:13

标签: php mysql html while-loop mysql-num-rows

我有一个很大的困境,我正在将数据从MySQL生成为一个表单,问题是当SQL生成的第一行为0时,表单中的select选项将第一个值解释为1。 如果我不想从此查询中丢弃一些数据,那不会有任何问题。但这是代码:

<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>

这是生成的HTML代码:

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

如果你比较选项标签中的值和data-num att,你会发现它们是一个单位不同,为什么。在页面着陆时选择选项的第一个值,但城市输入显示查询中的下一个值。 我怎么能解决这个问题呢? 提前谢谢你们。

1 个答案:

答案 0 :(得分:0)

虽然没有说明非常微妙,但我同意Shrapnel的评论。但是,这可能会解决您的“问题”(但我真的不明白这个问题):

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

更改为:

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