dongwu8653 2013-08-04 16:23
浏览 63
已采纳

WHILE中的IF语句 - PHP,MySQL和HTML下拉选择

I'll try to be short with this one. Most probably I'm missing something obvious so any input would be greatly appreciated.

SQL structure/columns:

  1. id_user (primary key)
  2. income_input
  3. income_select

PHP Code:

<?php
$con = mysql_connect("localhost","dbname","pwd");
if (!$con)
  {
  die('Could not connect to the database: ' . mysql_error());
  }

mysql_select_db("dbname", $con);

global $user;            // Drupal 7 global
$userid =  $user->uid;   // Drupal 7 currently logged-in user id

$result = mysql_query("SELECT * FROM my_table WHERE id_user='$userid'");

if($result === FALSE) {
    die(mysql_error());
}

if ($user->uid) {
?>

<form>
  <label for="edit-1">Label</label>
  <input id="edit-1" maxlength="60" size="60" type="text" value="<?php while($row = mysql_fetch_array($result)){ echo $row['income_input']; } ?>">
  <select id="select-edit-1">
     <option value="daily" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'daily') echo 'selected="selected"'; }?>>Daily</option>
     <option value="weekly" <?php while($row = mysql_fetch_array($result)){ $income_select = $row['income_select']; if ($income_select == 'weekly') echo 'selected="selected"'; }?>>Weekly</option>
  </select> 
</form>

<?php   
  }
  mysql_close($con);
?>

The input value (income_select) is fetched properly from the database, but the dropdown (income_select) won't return the selected state. If I include the entire form into the WHILE statement it's working fine and I can echo quickly the DB values and the select dropdown "selected" state is working fine too. I would opt for this method but I have to keep the form as it is and only populate it with data from the DB if there is any entry for the currently logged-in user.

I hope the above one makes sense and finally thank you in advance for your help!

  • 写回答

3条回答 默认 最新

  • doulun5683 2013-08-04 16:27
    关注

    You do not need a while if you want to fetch just one row.

    First, fetch the row and get what option should be selected.

    <?php 
    
    // better use 'fetch_assoc' here
    $row = mysql_fetch_assoc($result) or die(mysql_error());
    
    $income_select = $row['income_select'];
    
    $daily = ($income_select=='daily');
    $weekly = ($income_select=='weekly');
    
    ?>
    

    And in form, use it like this:

    <option value="daily" <?php if($daily) echo 'selected'; ?>>Daily</option>
    <option value="weekly" <?php if($weekly) echo 'selected'; ?>>Weekly</option>
    

    If your PHP settings and version allow it, you can even use this:

    <option value="daily" <?= $daily ? 'selected' : '' ?>>Daily</option>
    <option value="weekly" <?= $weekly ? 'selected' : '' ?>>Weekly</option>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料