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

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用