dongzi1209 2015-02-16 18:36
浏览 63
已采纳

使用PHP填充HTML选择

My goal is to populate a html select box from mysql using a php function. I started by putting the code directly on the html page, and I got it working.

<label for="product_Category">Product Category</label>
<?
$field_Name = "category_Name";
$table_Name = "Product_Category";

$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category"</option>;
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. "</option>";
}
echo "</select>";
?>

I have multiple select boxes, so I thought it would be easier to create a function and pass the table name, and field name as arguments

I moved the php function to its own file, and use an include statement in my html. But once I tried to call the PHP function, the select box wont populate. The select box shows up on the form like it should, but its empty.

PHP

<? 
function Populate_Select($table_Name, $field_Name){ 

$sql = "Select " . $field_Name . " From " . $table_Name;
$results = mysqli_query($link, $sql);
echo "<select>";
echo "<option value = \" \" >Select Category </option>";
while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
    echo "<option value = ' " .$row[$field_Name] . "'>" . $row[$field_Name]. "    </option>";
}
echo "</select>";
}
?>

DB Config

<?
$host = "localhost";
$db_userName = "root";
$db_Password = "root";
$db_Name = "mydb";

$link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
     if (!$link){
        die("Database Connection failed " . mysqli_connect_error);
     }

?>

HTML Code

<? include 'PopulateSelect.php' ?>
<? include 'DB_Config.php ?>
<!--Rest of HTML CODE-->
<label for="product_Category">Product Category</label>              
<? Populate_Select('Product_Category', 'category_Name'); ?>

Where did I go wrong when I called the function?

Is it better practice to use a function or am I better off just writing separate code for each select box?

*This is my first time posting, so I apologize if my post is not correct or formatted poorly.

  • 写回答

3条回答 默认 最新

  • doufang8965 2015-02-16 18:58
    关注

    The problem is that your $link variable is not defined. You need to create a class, OOP makes life easier, like this:

    //myclass.php
    
    class MyClass {
    
        private $link;
    
        function __construct() {
            $host = "localhost";
            $db_userName = "root";
            $db_Password = "root";
            $db_Name = "mydb";
            $this->link = mysqli_connect($host, $db_userName, $db_Password, $db_Name);
            if (!$this->link) {
                die("Database Connection failed " . mysqli_connect_error);
            }
        }
    
        function __destruct() {
            mysqli_close($this->link);
        }
    
        function Populate_Select($table_Name, $field_Name) {
            $sql = "Select $field_Name From $table_Name";
            $results = mysqli_query($this->link, $sql);
            $select = "<select>";
            $select .= "<option value=''>Select Category</option>";
            while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
                $select .= "<option value='$row[$field_Name]'>$row[$field_Name]</option>";
            }
            $select .= "</select>";
            return $select;
        }
    }
    

    Then you can call this on your other pages by including that .php file and accessing the class, like this:

    //your some_html.php
    
    include 'myclass.php';
    $obj = new MyClass();
    $select = $obj->Populate_Select("your_table_name", "your_field_name");
    echo $select;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了