根据所选的下拉选项填充表格

时间:2014-03-21 12:30:37

标签: php html mysql

我希望根据用户在下拉框中选择的选项填充表格。代码如下:

下面的代码调用selectBoxCategories函数,该函数成功填充下拉框,它当前显示我期望看到的结果。所有这些代码都在一个表单中,并且有一个提交按钮来提交表单以更新表格,如代码块中所示。

<form method="post">
          <?php echo selectBoxCategories(getCategories(), "category"); ?>
          <input value="Search" name="search" class='myButton' type="submit">

下面的代码生成表格,用于根据下拉框中的用户选择填充数据库

     <?php 

        $conn = new mysqli(localhost, root, PASSWORD, DBNAME);
        $sql = "SELECT * FROM reports WHERE subcategoryName = '".$selected."'";
        // Performs the $sql query on the server
        $result=mysqli_query($conn,$sql);
        $rows[] = array();

            echo '<table>';
            echo '<thead>';
            echo '<tr>';
            echo '<th> Report Name </th>';
            echo '<th> Category Name </th>';
            echo '<th> Sub Category Name </th>';
            echo '<th> Date Uploaded </th>';
            echo '</tr>';
            echo '</thead>';
            echo '<tbody>';
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {



            $rows[] = $row;
            echo '<tr>';
            echo '<td>'.$row['reportName'].'</td>'.'<td>'.$row['categoryName'].'</td>'.'<td>'.$row['subcategoryName'].'</td>'.'<td>'.$row['reportDateUploaded'].'</td>';
            echo '</tr>';




        };
        echo '</tbody>';
            echo '</table>';


        ?>

以下代码是一个用于填充下拉框的函数。

  <?php

    function selectBoxCategories($categories, $selectname = "categories", $selected = '') {
            $selectbox = '<select name="'.$selectname.'">'. "\n";
            for ($i=0; $i<count($categories); $i++) {
                $mainCat = $categories[$i]['categoryName']; // Top level category name

                $selectbox .= "\t\t\t\t\t\t\t".'<optgroup label="'.$mainCat.'">'."\n";

                for ($j=0; $j<count($categories[$i])-1; $j++) {
                    $sel = '';
                    if ($selected == $categories[$i][$j]) { $sel = ' selected'; }
                    $selectbox .= "\t\t\t\t\t\t\t\t".'<option value="'.$categories[$i][$j].'" '.$sel.'>&nbsp;&nbsp;&nbsp;'.$categories[$i][$j].'</option>'. "\n";
                }
                $selectbox .= "\t\t\t\t\t\t\t".'</optgroup>'."\n";
            }
            $selectbox .= '</select>';
            return $selectbox;
        }
        ?>

以下是获取selectBoxCategories功能中使用的类别本身的代码:

 <?php
        function getCategories() {
            $conn = new mysqli(localhost, root, PASSWORD, DBNAME);
            $categories = array();

            $sql = "SELECT categoryName FROM reportcategorys";
            $maincat = $conn->query($sql);
            if(!$maincat){
            echo $conn->error;
            }

            while($row = $maincat->fetch_array(MYSQLI_ASSOC)) {
                // do something with the $row
                array_push($categories, $row);
            }
            $sql1 = "SELECT * FROM reportsubcategorys";
            $subcats = $conn->query($sql1);

            // Loop through sub categories and append to parent array
            while($row = $subcats->fetch_array(MYSQLI_ASSOC)) {
                $parent = $row['categoryName'];
                $name =   $row['subCategoryName'];

                // Append subcategory name as child to the parent category
                for ($i=0; $i<count($categories); $i++) {
                    if ($categories[$i]['categoryName'] == $parent) {
                        array_push($categories[$i], $name);
                    }
                }
            }
            //print_r($categories);
            return $categories;    
        }
        ?>
</form> 

所有代码都存在于同一个php文件中。任何帮助将不胜感激。

由于

0 个答案:

没有答案