我如何使用$ _get和build_categories_options函数 [英] How Can i use $_get with build_categories_options function
问题描述
现在我已经创建了类别并创建了发布成功,但是当我编辑我的帖子时,如果编辑我的帖子有问题,我将失去我的类别。我需要将我的类别放入表格中,并且可以更改它。
Now I have create categories and create post is successful but when I edit my post I have problem if edit my post I will lose my category .. I need to get my category in table and I can change it
<?php
$sql = "SELECT catid, catname, parentid FROM categories";
$res = mysql_query($sql);
// initialize $categories to make sure it is an array
$categories = array();
while ($row = mysql_fetch_assoc($res)) {
$parent = intval($row['parentid']);
$categories[$parent][] = $row;
}
?>
<table border="0" cellpadding="10" cellspacing="0">
<tr>
<td valign="top">
<?php
$category_string = "";
function build_categories_options($parent, $categories, $level) {
global $category_string;
if (isset($categories[$parent]) && count($categories[$parent])) {
$level .= " - ";
foreach ($categories[$parent] as $category) {
$opt_value = substr($level.$category['catname'],3);
$category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';
build_categories_options($category['catid'], $categories, $level);
}
$level = substr($level, -3);
}
return $category_string;
}
$category_options = build_categories_options(0, $categories, '');
$category_options = '<select class="chosen" name="categories" id="categories">'.$category_options.'</select>';
echo $category_options;
?>
</td>
我的问题位于第25行
My problem located in line 25
$category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';
我需要首先获取我的类别并显示其余结果。
I need to get my category in first and show the rest of the results.
推荐答案
对于每个类别,您显示两个选项,一个为空选项,另一个为类别信息:
For each category, you are displaying two options, an empty option and one with the category information:
$category_string .= '<option value=""></option><option value="'.$category['catid'].'">'.$opt_value.'</option>';
这是在你的循环中。所以每次循环迭代时,都会创建两个选项。一个空白的,一个与你的类别。我打赌你只需在< select>
开头的一个空白选项。我认为这是你想要的:
This is inside your loop. So every time your loop iterates, two options will be created. A blank one and one with your category. I'm betting you just need one blank option at the very beginning of the <select>
. I think this is what you wanted:
// notice we are initializing $category_string with an empty option here
$category_string = '<option value=""></option>';
function build_categories_options($parent, $categories, $level) {
global $category_string;
if (isset($categories[$parent]) && count($categories[$parent])) {
$level .= " - ";
foreach ($categories[$parent] as $category) {
$opt_value = substr($level.$category['catname'],3);
// removed extra empty category and put it in $category_string initialization
$category_string .= '<option value="'.$category['catid'].'">'.$opt_value.'</option>';
build_categories_options($category['catid'], $categories, $level);
}
$level = substr($level, -3);
}
return $category_string;
}
另外,正如评论中提到的@MoeTsao,尽量避免使用 mysql _ *
功能,因为他们的使用是由PHP不鼓励。请改用 mysqli _ *
或 PDO 。
Also, as @MoeTsao mentioned in the comments, try to avoid using mysql_*
functions, as their use is discouraged by PHP. Instead, use mysqli_*
or PDO.
这篇关于我如何使用$ _get和build_categories_options函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!