【发布时间】:2011-04-13 18:04:21
【问题描述】:
我希望输出显示如下:
类别1 子类别1.1 子类1.2
类别2 子类 2.1 子类2.2
等等……
我认为这是 str_replace() 的问题 我不确定问题出在哪里,有人可以帮忙吗?
代码如下:
class temp
{
var $file;
function get_file($temp){
$this->file = get_file_contents($temp)
return $this->file;
}
function new_list($forum_list)
{
foreach($forum_list as $key => $value)
{
$this->file = str_replace('{'.$key.'}', $value, $this->file);
}
return $this->file;
}
function display()
{
echo $this->file;
}
}
$temp = new temp();
$temp->get_file('file.html');
mysql_select_db($database_config, $config);
$query_cat = "SELECT * FROM category ORDER BY dsp ASC";
$cat = mysql_query($query_cat, $config) or die(mysql_error());
while ($row_cat = mysql_fetch_array($cat)){
$temp->new_list(array(
'CAT_TITLE' => $row_cat['title'],
'CAT_DESCRIPTION' => $row_cat['description']));
$cid = $row_cat['id'];
mysql_select_db($database_config, $config);
$query_cat = "SELECT * FROM subcategory WHERE cid={$cid}";
$subcat = mysql_query($query_subcat, $config) or die(mysql_error());
while ($row_subcat = mysql_fetch_array($subcat)){
$temp->new_list(array(
'SUBCAT_TITLE' => $row_subcat['title'],
'SUBCAT_DESCRIPTION' => $row_subcat['description']));
}
}
$temp->display();
【问题讨论】:
-
如果数据库设置正确,您应该能够以您希望它们显示的方式从数据库中提取类别。我建议您参考:dev.mysql.com/tech-resources/articles/hierarchical-data.html
-
我也试过了,但没有用,而且我在两个表中的字段都有很多相似的名称,比如标题和描述。
标签: php mysql foreach while-loop str-replace