smarty循环

smarty一般的循环写法

 

$sql_content = "SELECT guid, times, id, ip, app_name FROM stats WHERE date = '{$date}' AND type = '1' GROUP BY crc LIMIT $offset,$Page_size";
$result_cotent = mysql_query($sql_content,$db->conn);

 

while($row = mysql_fetch_array($result_cotent)){

$row_content[] = array('app_name'=>$row['app_name'], 'times'=>date("Y-m-d h:i:s", $row['times']), 'ip'=>$row['ip'], 'guid'=>$row['guid']);

$smarty->assign("row_content",$row_content);

 

或者

 

$sql_content = "SELECT guid, times, id, ip, app_name FROM stats WHERE date = '{$date}' AND type = '1' GROUP BY crc LIMIT $offset,$Page_size";
$result_cotent = mysql_query($sql_content,$db->conn);

$i = 0;

 

while($row = mysql_fetch_array($result_cotent)){

 

 $row_array[$i]['guid']           = $rowsoft['guid'];
 $row_array[$i]['app_name']       = $rowsoft['app_name'];
 $row_array[$i]['counttotal']     = $countsoft1['total'];
 $row_array[$i]['todaytotal']     = $counttoday['total'];
 $row_array[$i]['yesterdaytotal'] = $countyesterday['total'];

 

}

$smarty->assign("row_content",$row_content);

这两个是相等的

 

还有一种简单的方法

 

$sql_content = "SELECT guid, times, id, ip, app_name FROM stats WHERE app_name = '{$app_name}' AND type = '1' GROUP BY crc LIMIT $offset,$Page_size";
$result_cotent = mysql_query($sql_content,$db->conn);

 

$row_content = array();
while($row = mysql_fetch_array($result_cotent)){
 
 
  $row_content[] = $row;

 

}

 

$smarty->assign("row_content",$row_content);

 

还有当一个循环下面有好多的子查询的时候

 

$sqlsoft = "SELECT guid, times, id, ip, app_name FROM stats WHERE type = '1' GROUP BY app_name";
$result_cotentsoft = mysql_query($sqlsoft,$db->conn);
$i = 0;


$row_array = array();
while($rowsoft = mysql_fetch_array($result_cotentsoft)){

 $countsoft1 = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT crc) AS total FROM stats WHERE type = '1' AND app_name = '$rowsoft[app_name]'",$db->conn));
 $counttoday = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT crc) AS total FROM stats WHERE type = '1' AND app_name = '$rowsoft[app_name]' AND date = '{$date}'",$db->conn));
 $countyesterday = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT crc) AS total FROM stats WHERE type = '1' AND app_name = '$rowsoft[app_name]' AND date = '{$dates}'",$db->conn));
 
 $rowsoft['counttotal'] = $countsoft1['total'];
 $rowsoft['todaytotal'] = $counttoday['total'];
 $rowsoft['yesterdaytotal'] = $countyesterday['total'];
 
 
 
 
 $row_array[] = $rowsoft;

 

 

 

这样可以集成到   $row_array  这个数组中

 

$smarty->assign("row_content",$row_content);

 

这种很完美,还可以封装到一个function中




smarty嵌套循环

test2.htm
<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>

test2.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");


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值