PHP:回显变量并通过selectbox显示输出

时间:2012-05-26 07:35:09

标签: php mysql

我很难掌握通过php从数据库中回显变量的概念。而是以字符串格式获取变量,我得到一个整数。如何通过选择框显示类别字符串变量?

这是我数据库中的信息:

CREATE TABLE `category` (
   `c_id` tinyint(4) not null auto_increment,
   `category` varchar(30) not null,
   PRIMARY KEY (`c_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=5;

INSERT INTO `category` (`c_id`, `category`) VALUES ('1', 'Animals >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('2', 'Humans >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('3', 'Technology >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('4', 'Oceans >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('5', 'Desert >');
INSERT INTO `category` (`c_id`, `category`) VALUES ('6', 'Jungles >');

这是我的php:

<select name="category" size="6" onChange="autoSubmit();">
        <?php

        //POPULATE DROP DOWN MENU WITH CATEGORIES
        $conn = new PDO($DSN, $USERNAME, $PASSWORD);
        $sql = "SELECT * FROM category ORDER BY c_id";

        while($row = mysql_fetch_array($categories))
        {        
            echo ("<option value=\"$row[c_id]\" " . ($category == $row["c_id"] ? " selected" : "") . ">$row[category]</option>");        
        }

        ?>


    <option value="1" <?php if($category == 1) echo " selected"; ?>><? echo $category; ?></option>
    <option value="2" <?php if($category == 2) echo " selected"; ?>><? echo $category; ?></option>
    <option value="3" <?php if($category == 3) echo " selected"; ?>><? echo $category; ?></option>
    <option value="4" <?php if($category == 4) echo " selected"; ?>><? echo $category; ?></option>
    <option value="5" <?php if($category == 5) echo " selected"; ?>><? echo $category; ?></option>
    <option value="6" <?php if($category == 6) echo " selected"; ?>><? echo $category; ?></option>
    </select>

4 个答案:

答案 0 :(得分:4)

升级到答案

  1. <select></select>标记应位于PHP代码块的任一侧;
  2. 您应该从SQL中删除WHERE子句;和
  3. 您不需要手动包含<option>..</option>代码,因为这是由PHP构建/输出的。
  4. 例如,使用PDO:

    <select name="category" size="6" onChange="autoSubmit();">
    <?php
      $dbh = new PDO($DSN, $USERNAME, $PASSWORD);
      $qry = $dbh->query('SELECT * FROM category ORDER BY c_id');
    
      while ($row = $qry->fetch()) {
        echo '<option value="' . $row['c_id'] . '"';
        if ($row['c_id'] == $category) echo ' selected';
        echo '>' . htmlentities($row['category']) . '</option>'
      }
    ?>
    </select>
    

答案 1 :(得分:2)

这可能会帮助你并记住eggyal提到的第三点

  

3)您无需手动包含..代码   因为这是由PHP构建/输出的。

<select name="category" size="6" onChange="autoSubmit();">
        <?php

        //POPULATE DROP DOWN MENU WITH CATEGORIES
        $dbh = new PDO($DSN, $USERNAME, $PASSWORD);
        $qry = $dbh->query('SELECT * FROM category ORDER BY c_id');

        while ($row = $qry->fetch()) {
        {        
            if($row[c_id] == $category)
                $selected = "selected=\"selected\"";            
            echo ("<option value=\"{$row['c_id']}\" {$selected}>{$row['category']}</option>");        
        }
        ?>

答案 2 :(得分:2)

以下是您的示例数据集的公式:

  1. Connect database并准备查询(使用PDO extension):

    $dbh = new PDO('mysql:host=localhost;dbname=my_database', $user, $pass);
    $query = 'SELECT `c_id`, `category` FROM `category`';
    
  2. Execute query并将其推送到PHP array

    foreach ($dbh->query($query) as $row) {
        $categories[$row['c_id']] = $row['category'];
    }
    
  3. Loop through the array并输出HTML:

    <?php $my_selected_category = 3; ?>
    <select name="category" size="6" onChange="autoSubmit();">
        <?php foreach($categories as $id => $name) { ?>
        <option value="<?php echo $id; ?>" <?php echo $id == $my_selected_category ? 'selected="selected"' : ''; ?>><?php echo $name; ?></option>
        <?php } ?>
    </select>
    

    请注意,我使用了ternary operator代替if-else控件结构来有条件地回显selected="selected"。在某些情况下,操作员可以减轻您的生活。

答案 3 :(得分:1)

我可以看到一个问题 - 不确定这是否是您的问题?这就是你选择“选择”位的方法。

<option value="1" <?php if($category == 1) echo ' "selected=selected"'; ?>><? echo $category; ?></option>

但我们需要查看您的代码,您可以在其中分配$ category变量以了解更多