为<选项值=“...”>赋予多个值在 html 中,并使用 PHP 将值排序到 MySQL 中的相应列中

发布于 2024-12-29 08:13:46 字数 1642 浏览 0 评论 0 原文

我对 php 和 mysql 的了解有限,我正在尝试一边学习一边从头开始创建一个非常简单的 CMS,以便与我的 PHPBB 论坛结合起来。

CMS 可以正常运行,但我只能创建 1 级类别。我无法拥有多个级别的类别,任何帮助将不胜感激。

我有以下列的类别 - cID、标题、描述、级别、取决于。根据需要包含父类别的 cID 值,级别应为父类别的级别值 +1。

这是我到目前为止所拥有的,但没有运气- 功能:

function  addCat($cName, $cDesc, $property) {
$property = "$cLevel, $cDepend";
$query = mysql_query("INSERT INTO categories VALUES(null,'$cName','$cDesc', '$cLevel', '$cDepend')") or die(mysql_error());
}

类别选择器的 html:

<form action="doAddC.php" method="post">
<td><label for="">Category</label></td>
    <td><select name="'CatLevel''CatDepend'">
        <?php
            $result = mysql_query("SELECT * FROM categories");
            while($cat = mysql_fetch_assoc($result)){
        ?>
            <option value="'CatLevel':'<?php echo $cat['Level']; ?>','CatDepend':'<?php echo $cat['cID']; ?>'"><?php echo $cat['Title']; ?></option>
        <?php
            }
        ?>
    </select></td>

doAddC.php

if(isset($_POST['submit'])) {
if(isset($_POST['CatName'])) {
    if(isset($_POST['CatDesc'])) {
        addCat($_POST['CatName'], $_POST['CatDesc'], $_POST['CatLevel']['CatDepend']);
        header("Location: cats.php");
    } else {
        echo "Please Add the Category Description!";
        include('addCat.php');
    }
} else {
    echo "Please Set the Category Name!";
    include('addCat.php');
}
} else {
header("Location: addCat.php");
}

在最好的情况下,数据库中的 Level 和 dependent 等于 0。我什至不知道在手册中哪里可以找到这个。

I have limited knowledge of php and mysql, I am trying to learn it as I go while creating a very simple CMS from scratch to combine with my PHPBB forum.

The CMS is functional, yet I can only create 1 level of categories. I am failing on having multiple levels of categories, any help would be appreciated.

I have the following columns in categories - cID, Title, Description, Level, Depending. Depending needs to include cID value of the father-category, Level should be +1 to the level value of the father-category.

This is what I have so far with no luck-
The function:

function  addCat($cName, $cDesc, $property) {
$property = "$cLevel, $cDepend";
$query = mysql_query("INSERT INTO categories VALUES(null,'$cName','$cDesc', '$cLevel', '$cDepend')") or die(mysql_error());
}

The html for the category selector:

<form action="doAddC.php" method="post">
<td><label for="">Category</label></td>
    <td><select name="'CatLevel''CatDepend'">
        <?php
            $result = mysql_query("SELECT * FROM categories");
            while($cat = mysql_fetch_assoc($result)){
        ?>
            <option value="'CatLevel':'<?php echo $cat['Level']; ?>','CatDepend':'<?php echo $cat['cID']; ?>'"><?php echo $cat['Title']; ?></option>
        <?php
            }
        ?>
    </select></td>

doAddC.php

if(isset($_POST['submit'])) {
if(isset($_POST['CatName'])) {
    if(isset($_POST['CatDesc'])) {
        addCat($_POST['CatName'], $_POST['CatDesc'], $_POST['CatLevel']['CatDepend']);
        header("Location: cats.php");
    } else {
        echo "Please Add the Category Description!";
        include('addCat.php');
    }
} else {
    echo "Please Set the Category Name!";
    include('addCat.php');
}
} else {
header("Location: addCat.php");
}

In the best scenario Level and Depending equal to 0 in the database. I don't even know where to look for this in manuals.

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

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

发布评论

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

评论(1

┈┾☆殇 2025-01-05 08:13:46

问题已经解决了。对于那些可能遇到类似问题的人来说,代码如下。

HTML 快照:

<form action="doAddC.php" method="post">
<td><select name="CatDepend">
        <option value="<?php
        $depending = "0"; /* Tells that there is no master category */
        $level = "1"; /* Tells that this is the master category (Level 1) */
        $str = "$depending $level";
        echo $str ?>">None</option>
    <?php
        $result = mysql_query("SELECT * FROM categories");
        while($cat = mysql_fetch_assoc($result)){
    ?>
        <option value="<?php
        $depending = $cat['ID']; /* Tells that the ID of the master category is a variable for Depending */         
        $level = ($cat['Level'] + 1); /* Tells that the category level of a new category is +1 */
        $str = "$depending $level";
        echo $str ?>"><?php echo $cat['Title']; ?></option>
    <?php
        }
    ?>
</select></td>

doAddC.php 的快照:

if(isset($_POST['submit'])) {
    if(isset($_POST['CatName'])) {
        if(isset($_POST['CatDesc'])) {
            addCat($_POST['CatName'], $_POST['CatDesc'], $_POST['CatDepend']);
            header("Location: cats.php");
        } else {
            echo "Please Add the Category Description!";
            include('addCat.php');
        }
    } else {
        echo "Please Set the Category Name!";
        include('addCat.php');
    }
} else {
    header("Location: addCat.php");
}

函数的快照:

function  addCat($cName, $cDesc, $cOrder) {
    $part = explode(" ",$cOrder); /* Using explode() break down the value of $str (html snapshot), where a space works as a separator */
    $query = mysql_query("INSERT INTO categories VALUES(null,'$cName','$cDesc', '$part[0]', '$part[1]')") or die(mysql_error());    /* $part[0] and $part[1] put the values into the database as needed /*
}

任务完成:)

The problem has been solved. The code for those who might be struggling with a similar problem as follows.

HTML snapshot:

<form action="doAddC.php" method="post">
<td><select name="CatDepend">
        <option value="<?php
        $depending = "0"; /* Tells that there is no master category */
        $level = "1"; /* Tells that this is the master category (Level 1) */
        $str = "$depending $level";
        echo $str ?>">None</option>
    <?php
        $result = mysql_query("SELECT * FROM categories");
        while($cat = mysql_fetch_assoc($result)){
    ?>
        <option value="<?php
        $depending = $cat['ID']; /* Tells that the ID of the master category is a variable for Depending */         
        $level = ($cat['Level'] + 1); /* Tells that the category level of a new category is +1 */
        $str = "$depending $level";
        echo $str ?>"><?php echo $cat['Title']; ?></option>
    <?php
        }
    ?>
</select></td>

Snapshot of doAddC.php:

if(isset($_POST['submit'])) {
    if(isset($_POST['CatName'])) {
        if(isset($_POST['CatDesc'])) {
            addCat($_POST['CatName'], $_POST['CatDesc'], $_POST['CatDepend']);
            header("Location: cats.php");
        } else {
            echo "Please Add the Category Description!";
            include('addCat.php');
        }
    } else {
        echo "Please Set the Category Name!";
        include('addCat.php');
    }
} else {
    header("Location: addCat.php");
}

Snapshot of function:

function  addCat($cName, $cDesc, $cOrder) {
    $part = explode(" ",$cOrder); /* Using explode() break down the value of $str (html snapshot), where a space works as a separator */
    $query = mysql_query("INSERT INTO categories VALUES(null,'$cName','$cDesc', '$part[0]', '$part[1]')") or die(mysql_error());    /* $part[0] and $part[1] put the values into the database as needed /*
}

Mission accomplished :)

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