我对 php 和 mysql 的了解有限,我正在尝试一边学习一边从头开始创建一个非常简单的 CMS,以便与我的 PHPBB 论坛结合起来。
CMS 可以正常运行,但我只能创建 1 级类别。我无法拥有多个级别的类别,任何帮助将不胜感激。
我有以下列的类别 - cID、标题、描述、级别、取决于。根据需要包含父类别的 cID 值,Level 应为父类别的 level 值+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。我什至不知道在手册中哪里可以找到这个。
最佳答案
问题已解决。对于那些可能遇到类似问题的人来说,代码如下。
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 /*
}
任务完成:)
关于php - 在html中为<option value ="...">提供多个值,并使用PHP将这些值排序到MySQL中的相应列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9007268/