$query = “SELECT id,name,name_cn FROM di_flag ORDER BY id desc”; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $query2=”SELECT id, name, name_cn,flag FROM di_sort WHERE di_sort.flag =$row[id] ORDER BY id desc”; $result2=mysql_query($query2); while($row2 = mysql_fetch_array($result2)) { $post[]=array(’sid’=>$row2[’id’], ’sortname’=>$row2[’name’], ); } $row_array[] = array(’cid’=>$row[’id’], ‘cat_name’=>$row[’name’], ‘topic’=>$post ); unset($post); } $smarty->assign(”forum”,$row_array); unset($row_array);
很簡單,以上是一個二維數組,然後再看如何嵌套循環,這回用foreach咯,看代碼:
{foreach from=$forum ietm=value} <div class=”sort_list”> <a href=”products.php?flag={$value.cid}” mce_href=”products.php?flag={$value.cid}” title=”{$value.cat_name}”>{$value.cat_name}</a> {foreach from=$value.topic ietm=value2} <div class=”sort_list02″><a href=”products.php?sort={$value2.sid}” mce_href=”products.php?sort={$value2.sid}” title=”{$value2.sortname}”>{$value2.sortname}</a></div> {/foreach} </div> {/foreach}
然後用section:
test.php <?php require_once('./include/db_fns.php'); include_once("./Smarty/libs/Smarty.class.php"); //包含Smarty類文件 $smarty = new Smarty(); //建立Smarty實例對象$Smarty $smarty->template_dir = "./templates/dedecms";//設置模板目錄 $smarty->compile_dir = "templates/templates_c"; //設置編譯目錄 $smarty->assign("template_url", "./"); $smarty->assign("$site_url", "http://www.gotop.net.ru"); $smarty->assign("$site_name", "文章管理系統"); $smarty->left_delimiter = "<{"; //設置左邊界符 $smarty->right_delimiter = "}>"; //設置右邊界符 $db_conn = db_connect(); $query = "Select cat_id,cat_name FROM categories orDER BY cat_ID DESC"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $query2="Select post_id, post_title, post_date FROM post Where post.post_category =$row[cat_id] AND post_status <> 'unpublish' orDER BY post_date DESC LIMIT 5"; //這裏加一個 LIMIT 5 不要用什麼$i來控制 $result2=mysql_query($query2); while($row2 = mysql_fetch_array($result2)) { $post[]=array('post_id'=>$row2['post_id'], 'post_title'=>$row2['post_title'], 'post_date'=>date('m-d',strtotime($row2[post_date])) ); } $row_array[] = array('cat_id'=>$row['cat_id'], 'cat_name'=>$row['cat_name'], 'post'=>$post ); unset($post); //$post[]在使用完後要註銷,因爲使用賦值語句對$post進行賦值時不會 //將它清空,而是將新數組作爲它的一個元素增加,爲了不產生副作用,這裏使用unset()是必需的. //如果不註銷,第二次循環的結果會包含第一循環的數據,如分類2的文章跑到分類1中來。 } echo"</br>row_array數組:</br>"; print_r($row_array);//打出來看看有沒有數據 $smarty->assign("row_array", $row_array); $smarty->display("test2.htm"); ?>
test.htm <?php <html> <head> <title>巢狀循環測試</title> </head> <body> <{section name=sec1 loop=$row_array}> <table width="322" border="1" align="center" cellpadding="3" cellspacing="0"> <tr> <td colspan="2"><{$row_array[sec1].cat_id}></td> </tr> <{section name=sec2 loop=$row_array[sec1].post}> <tr> <td width="160">post_title:<{$row_array[sec1].post[sec2].post_title}> </td> <td width="160">post_date:<{$row_array[sec1].post[sec2].post_date}></td> </tr> <{/section}> </table> <{/section}> </body> </html> ?>
在網上找到一個是由手機在線提供的一個不錯的簡單實用的接口。 該網站提供的接口包括如下類型: 通過 WebService 調用獲取數據 通過 HTTP Post/Get 獲取 XML 格式的數據 通過 HTTP Post/Get 獲取 JS