我在获取类别和子类别时遇到问题

发布于 2024-10-01 19:10:51 字数 2603 浏览 1 评论 0原文

这是我的数据库:

id         name       parent_id

1          Computers   NULL
2          Apple       1
3          Books       1
4          Music       NULL
5          CDs         4
6          Records     4

我的类别功能:

 public function showCategories($parent_id = 0){
        if($parent_id == 0){
            $sql = "SELECT * FROM categories WHERE parent_id IS NULL";
        } else {
            $sql = "SELECT * FROM categories WHERE parent_id =:parentid";
        }

        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':parentid', $parent_id);
        $stmt->execute();

        $categories = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            array_push($categories, array($row['id'] => $row['name']));
        }
        return $categories;
    }

这是我的类别页面:

<?php
//Instantiate categories class
$categories = new categories($db);
$categoriesMain = $categories->showCategories(0);
?>
<html>
   <head></head>
   <body>
       <form action="" method="post">
           <?php //Get parent categories and put them into a select box ?>
           <select name="categoriesMain">
                <?php for($i=0;$i<count($categoriesMain);$i++){ ?>
                      <option value="<?php echo $i; ?>">
                           <?php echo $categoriesMain[$i]; ?>
                      </option>
                <?php } ?>
           </select>
       <input type="submit" name="submit" value="submit"/>
       </form>

       <?php //if form submits then show sub categories ?>
          <?php if(isset($_POST['submit'])){
                    $categoriesSub = $categories->showCategories($_POST['categoriesMain']);
                    for($i=0;$i<count($categoriesSub);$i++){
                        echo $categoriesSub[$i];
                    }
          } ?>
    </body>
</html>

让我尝试解释一下我遇到的问题。我认为我的整个设计不合适,因为感觉就像那样,但我现在脑子有障碍。

在函数中,我返回一个类似于 Array ( [0] => Array ( [1] => Computers ) [1] => Array ( [4] => Music ) ).如果您认为这是错误的退货方式,请告诉我。好的,那么您看到 CategoriesMain 了吗?我正在使用 for 循环来输出这个数组,而 for option=value im echoing $i 但是这个 $i 就像 1, 2, 3, 4 但是我想要该值是父类别的值,例如 1, 4 ,以便我可以在下一个 for 循环中使用 $_POST['cateogoriesMain'] 收集该值正在显示 cateogriesSub ,它将获取那些 parent_id = 之前在选择框中选择的内容 的数据库行。我希望这是有道理的。

Here's my database:

id         name       parent_id

1          Computers   NULL
2          Apple       1
3          Books       1
4          Music       NULL
5          CDs         4
6          Records     4

My categories function:

 public function showCategories($parent_id = 0){
        if($parent_id == 0){
            $sql = "SELECT * FROM categories WHERE parent_id IS NULL";
        } else {
            $sql = "SELECT * FROM categories WHERE parent_id =:parentid";
        }

        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':parentid', $parent_id);
        $stmt->execute();

        $categories = array();
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            array_push($categories, array($row['id'] => $row['name']));
        }
        return $categories;
    }

Here's my category page:

<?php
//Instantiate categories class
$categories = new categories($db);
$categoriesMain = $categories->showCategories(0);
?>
<html>
   <head></head>
   <body>
       <form action="" method="post">
           <?php //Get parent categories and put them into a select box ?>
           <select name="categoriesMain">
                <?php for($i=0;$i<count($categoriesMain);$i++){ ?>
                      <option value="<?php echo $i; ?>">
                           <?php echo $categoriesMain[$i]; ?>
                      </option>
                <?php } ?>
           </select>
       <input type="submit" name="submit" value="submit"/>
       </form>

       <?php //if form submits then show sub categories ?>
          <?php if(isset($_POST['submit'])){
                    $categoriesSub = $categories->showCategories($_POST['categoriesMain']);
                    for($i=0;$i<count($categoriesSub);$i++){
                        echo $categoriesSub[$i];
                    }
          } ?>
    </body>
</html>

Let me try to explain what im having trouble with. I think my whole design is out of place because it feels like that but im having a brain block at the moment.

In the function I'm returning an array like Array ( [0] => Array ( [1] => Computers ) [1] => Array ( [4] => Music ) ). If you think this is the wrong way to return it let me know. Ok then do you see CategoriesMain? I'm using a for loop to output this array and for option=value im echoing $i however this $i goes like 1, 2, 3, 4 however I want the value to be the value of the parent category e.g. 1, 4 so that I can collect the value using $_POST['cateogoriesMain'] in the next for loop where I'm displaying cateogriesSub where it will get the database rows for those with the parent_id = to whatever was selected in the selectbox previously. I hope it makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-10-08 19:10:52

您应该使用数组的键作为选项值,如下所示:

   <select name="categoriesMain">
            <?php foreach ($categoriesMain as $k => $v) { ?>
                  <option value="<?php echo $k; ?>">
                       <?php echo $v; ?>
                  </option>
            <?php } ?>
   </select>

edit 还要更改 php 函数内的以下行,而不是:

array_push($categories, array($row['id'] => $row['name']));

do

$categories[$row['id']] = $row['name'];

You should use the key of the array for the option value, like this:

   <select name="categoriesMain">
            <?php foreach ($categoriesMain as $k => $v) { ?>
                  <option value="<?php echo $k; ?>">
                       <?php echo $v; ?>
                  </option>
            <?php } ?>
   </select>

edit also change the following line inside the php function, instead of:

array_push($categories, array($row['id'] => $row['name']));

do

$categories[$row['id']] = $row['name'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文