简体   繁体   中英

Smarty foreach mysql issue

Have some trouble with foreach in smarty.

php

$rows4 = array(); 
   $result4 = mysql_query("SELECT * FROM stats_follow ORDER BY id DESC LIMIT 5"); 
   while($row4 = mysql_fetch_assoc($result4)) { 
   $rows4[] = $row4; 
    } 

   $rows45 = array(); 
   $result45 = mysql_query("SELECT * FROM stats_follow ORDER BY id DESC LIMIT 5 OFFSET 1"); 
   while($row45 = mysql_fetch_assoc($result45)) { 
   $rows45[] = $row45; 
    } 

/** assign variable */ 

$smarty->assign('rows4', $rows4); 
$smarty->assign('rows45', $rows45); 

tpl

{foreach $rows4 as $row4} 
    <tr> 
       <td><center>{$row4.datum|truncate:10:""}</center></td> 
       <td><center>{$row4.count|truncate:10:""} {$row45.count}</center></td> 
     </tr> 
      {/foreach}

But how can I assign the second query in this foreach now, this dont work:

{foreach $rows4 as $row4 || $rows45 as $row45} 
    <tr> 
       <td><center>{$row4.datum|truncate:10:""}</center></td> 
       <td><center>{$row4.count|truncate:10:""} {$row45.count}</center></td> 
     </tr> 
      {/foreach}

Try this:

$rows445 = array();
$result4 = mysql_query("SELECT * FROM stats_follow ORDER BY id DESC LIMIT 5");
while ($row4 = mysql_fetch_assoc($result4)) {
    $rows445[]['row4'] = $row4;
}

$result45 = mysql_query("SELECT * FROM stats_follow ORDER BY id DESC LIMIT 5 OFFSET 1");
while ($row45 = mysql_fetch_assoc($result45)) {
    $rows445[]['row45'] = $row45;
}

/** assign variable */    
$smarty - > assign('rows445', $rows445);

I you get the same number of results in $row4 and $row45 then you can use an increment:

    {assign var=i value=0}
    {foreach $rows4 as $row4} 
    {assign var=row45 value=$rows45[i]}
        <tr> 
            <td><center>{$row4.datum|truncate:10:""}</center></td> 
            <td><center>{$row4.count|truncate:10:""} {$row45.count}</center></td> 
        </tr> 
    {assign var=i value=$i+1}
    {/foreach}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM