我希望输出像这样显示:
类别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();
最佳答案
我看到的一个问题是:
$this->file = str_replace('{'.$key.'}', $value, $this->file);
您在每次迭代中都会覆盖您的 $file
变量,除此之外,您从未真正设置 $file
所以据我所知,它应该是运行代码后为空。
关于php foreach 仅使用动态数据循环一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5653558/