php smarty 中显示分类。问题 var_dump($arr);var_dump($arr1);都打印出来看看有数据吗 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主的代码问题不小,参考下面的代码吧:mysql_connect('localhost','root','root');mysql_select_db('test');mysql_query('set names gb2312');$result_uplei=mysql_query("select leiname from lei where uplei='0'");if($result_uplei){ $i=0; while($row_uplei=mysql_fetch_row($result_uplei)){ $result_sublei=mysql_query("select leiname from lei where uplei='{$row_uplei[0]}'"); if($result_sublei) while($row_sublei=mysql_fetch_row($result_sublei)) $sublei[]=$row_sublei[0]; $uplei[]=array('leiname'=>$row_uplei[0],'sublei'=>$sublei); unset($sublei); }}require('smarty/Smarty.class.php');$smarty=new Smarty();$smarty->template_dir='./';$smarty->assign('uplei',$uplei);$smarty->display('lei.htm');{if $uplei} <ul> {foreach from=$uplei item=i} <li>{$i.leiname} {if $i.sublei} <dl> {foreach from=$i.sublei item=ii} <li>{$ii}</li> {/foreach} </dl> {/if} </li> {/foreach} </ul>{else} 无分类{/if} 抛个砖,照楼主的改了改,不过不会用section,经常用foreach,未测试,呵呵希望对楼主有帮助。require "include/conndata.php";require "./classinclude.php";$rs=mysql_query("select * from lei where uplei='0'"); //找出一级分类$i=0;$num=mysql_num_rows($rs);while($i< mysql_num_rows($rs)){ $arr=mysql_fetch_array($rs); $i++; $leiname=$arr["leiname"]; $rows=mysql_query("select * from lei where uplei='$leiname'"); $j=0; while($j < mysql_num_rows($rows)) { $arr['subs']=mysql_fetch_array($rows); $leiname=$arr1["leiname"]; //$smarty->assign("arr1",$arr1); $j++; } $smarty->append("arr",$arr);}$smarty->display("lei.html");{section name=key loop=$arr}<ul style="margin:0px;"><li><img src="image/jjbg.gif" />{$arr[key].leiname}</li> {section name=k loop=$arr[key].subs} <dl style="list-style-type:none; padding:5px;"> <li style="margin:5px; margin-left:10px;">{$arr1[k].leiname}</li> </dl> {/section} </ul>{/section} tpl里{$arr1[k].leiname}是不是应该改成{$arr[key].subs[k].leiname}对section太迷糊了,呵呵 大概看了下 问题可能出在红色标记的地方<?php require "include/conndata.php";require "./classinclude.php"; $rs=mysql_query("select * from lei where uplei='0'"); //找出一级分类 $i=0; $num=mysql_num_rows($rs); while($i< mysql_num_rows($rs)){ $arr=mysql_fetch_array($rs); $i++; $leiname=$arr["leiname"]; $smarty->assign("arr",$arr); $rows=mysql_query("select * from lei where uplei='$leiname'"); $j=0; while($j < mysql_num_rows($rows)) { $arr1=mysql_fetch_array($rows); $leiname=$arr1["leiname"]; $smarty->assign("arr1",$arr1); $j++; } } $smarty->display("lei.html"); ?>因为smarty的section的接收参数 arr1是一个数组,你传进去的只是1个获得的值,现在的代码获取出来的就应该是 $arr1 => array(uplei=>"计算机科学" leiname=>"人体科学");这样的数组 然后你在smarty里再取 $arr1[0].leiname当然不对我觉得该$secondClassName = array(); while($j < mysql_num_rows($rows)) { $arr1=mysql_fetch_array($rows); $leiname=$arr1["leiname"]; $secondClassName [] = $ arr1; $j++; } } $smarty->assign("arr1",$secondClassName);$smarty->display("lei.html");这样,小弟有什么说错的地方请各位指点。。 smarty中可以使用 $var|var_dump 把变量输出瞅瞅 问题还是出在基础没有打。有两个方面是:一是对二维数组的理解 二是对smarty的运行机制的理解。 php<?phprequire_once("./include/conndata.php");$rs=mysql_query("select * from lei where uplei='0'"); //找出一级分类if($rs){$i=0;$num=mysql_num_rows($rs);while($i< mysql_num_rows($rs)){$arr=mysql_fetch_array($rs);$i++;$leiname=$arr["leiname"];$rows=mysql_query("select * from lei where uplei='$leiname'");if($rows){$j=0;while($j < mysql_num_rows($rows)) { $arr1=mysql_fetch_array($rows); $zilei=$arr1["leiname"]; $lei["$leiname"][]=$zilei; $j++; }}}}require_once("./include.php");$smarty->assign("arr",$lei);$smarty->display("lei.html");?>html:{foreach item=i from=$arr key=key}<ul style="margin:0px;"><li><img src="image/jjbg.gif" />{$key}</li><dl style="list-style-type:none; padding:5px;">{foreach item=k from=$i} <li style="margin:5px; margin-left:10px;">{$k}</li>{/foreach} </dl></ul>{/foreach} =>,->这些是什么意思呀? 请教作过UC方面的高手!!!!! 求一个门户的开源项目 Warning: Missing argument 1 判断一个字符在另一个串里存不存在的方法 realpath问题 关于PHP中的链接。请熟悉PHP的朋友进来帮个忙。 php是否可以读取ASP写的COOKIE,如何读?急,请大家帮帮我! 一个菜鸟问题 name是变量,如何把VALUE写入数据库 php中 如何在一个服务器上导入另一个服务器里的php文件啊 为什么提示session错误?
mysql_select_db('test');
mysql_query('set names gb2312');$result_uplei=mysql_query("select leiname from lei where uplei='0'");
if($result_uplei){
$i=0;
while($row_uplei=mysql_fetch_row($result_uplei)){
$result_sublei=mysql_query("select leiname from lei where uplei='{$row_uplei[0]}'");
if($result_sublei)
while($row_sublei=mysql_fetch_row($result_sublei))
$sublei[]=$row_sublei[0];
$uplei[]=array('leiname'=>$row_uplei[0],'sublei'=>$sublei);
unset($sublei);
}
}require('smarty/Smarty.class.php');
$smarty=new Smarty();
$smarty->template_dir='./';
$smarty->assign('uplei',$uplei);
$smarty->display('lei.htm');{if $uplei}
<ul>
{foreach from=$uplei item=i}
<li>{$i.leiname}
{if $i.sublei}
<dl>
{foreach from=$i.sublei item=ii}
<li>{$ii}</li>
{/foreach}
</dl>
{/if}
</li>
{/foreach}
</ul>
{else}
无分类
{/if}
不过不会用section,经常用foreach,未测试,呵呵
希望对楼主有帮助。require "include/conndata.php";
require "./classinclude.php";
$rs=mysql_query("select * from lei where uplei='0'"); //找出一级分类
$i=0;
$num=mysql_num_rows($rs);
while($i< mysql_num_rows($rs)){
$arr=mysql_fetch_array($rs);
$i++;
$leiname=$arr["leiname"];
$rows=mysql_query("select * from lei where uplei='$leiname'");
$j=0;
while($j < mysql_num_rows($rows))
{
$arr['subs']=mysql_fetch_array($rows);
$leiname=$arr1["leiname"];
//$smarty->assign("arr1",$arr1);
$j++;
}
$smarty->append("arr",$arr);
}
$smarty->display("lei.html");{section name=key loop=$arr}
<ul style="margin:0px;"><li><img src="image/jjbg.gif" />{$arr[key].leiname}</li>
{section name=k loop=$arr[key].subs}
<dl style="list-style-type:none; padding:5px;">
<li style="margin:5px; margin-left:10px;">{$arr1[k].leiname}</li>
</dl>
{/section}
</ul>
{/section}
对section太迷糊了,呵呵
require "include/conndata.php";
require "./classinclude.php";
$rs=mysql_query("select * from lei where uplei='0'"); //找出一级分类 $i=0;
$num=mysql_num_rows($rs);
while($i< mysql_num_rows($rs)){
$arr=mysql_fetch_array($rs);
$i++;
$leiname=$arr["leiname"];
$smarty->assign("arr",$arr);
$rows=mysql_query("select * from lei where uplei='$leiname'");
$j=0;
while($j < mysql_num_rows($rows)) {
$arr1=mysql_fetch_array($rows);
$leiname=$arr1["leiname"];
$smarty->assign("arr1",$arr1);
$j++;
}
}
$smarty->display("lei.html"); ?>因为smarty的section的接收参数 arr1是一个数组,你传进去的只是1个获得的值,现在的代码获取出来的就应该是 $arr1 => array(uplei=>"计算机科学" leiname=>"人体科学");这样的数组 然后你在smarty里再取 $arr1[0].leiname当然不对我觉得该
$secondClassName = array();
while($j < mysql_num_rows($rows)) {
$arr1=mysql_fetch_array($rows);
$leiname=$arr1["leiname"];
$secondClassName [] = $ arr1;
$j++;
}
}
$smarty->assign("arr1",$secondClassName);
$smarty->display("lei.html");
这样,小弟有什么说错的地方请各位指点。。
有两个方面是:一是对二维数组的理解
二是对smarty的运行机制的理解。 php
<?php
require_once("./include/conndata.php");
$rs=mysql_query("select * from lei where uplei='0'"); //找出一级分类
if($rs){
$i=0;
$num=mysql_num_rows($rs);
while($i< mysql_num_rows($rs)){
$arr=mysql_fetch_array($rs);
$i++;
$leiname=$arr["leiname"];
$rows=mysql_query("select * from lei where uplei='$leiname'");
if($rows){
$j=0;
while($j < mysql_num_rows($rows))
{
$arr1=mysql_fetch_array($rows);
$zilei=$arr1["leiname"];
$lei["$leiname"][]=$zilei;
$j++;
}
}
}
}
require_once("./include.php");
$smarty->assign("arr",$lei);
$smarty->display("lei.html");
?>
html:{foreach item=i from=$arr key=key}
<ul style="margin:0px;"><li><img src="image/jjbg.gif" />{$key}</li>
<dl style="list-style-type:none; padding:5px;">
{foreach item=k from=$i}
<li style="margin:5px; margin-left:10px;">{$k}</li>
{/foreach}
</dl>
</ul>
{/foreach}