Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

foreach functionality

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
shandley14
Smarty Rookie


Joined: 20 Aug 2003
Posts: 10

PostPosted: Tue Feb 24, 2004 11:21 pm    Post subject: foreach functionality Reply with quote

i have an array that gives me this

Code:
Array (10)
0 => Array (1)
  STAT_CODE => A
1 => Array (2)
  STAT_CODE => B
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => 2
2 => Array (2)
  STAT_CODE => C
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => 1
3 => Array (1)
  STAT_CODE => D
4 => Array (1)
  STAT_CODE => E
5 => Array (2)
  STAT_CODE => F
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => -1
6 => Array (2)
  STAT_CODE => G
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => -3
7 => Array (2)
  STAT_CODE => H
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => 1
8 => Array (2)
  STAT_CODE => K
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => 1
9 => Array (1)
  STAT_CODE => L
10 => Array (2)
  STAT_CODE => N
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => 1


For some the Arrays there is a STAT_CODE and a PLAYER_STAT_VALUE
For Others there is only a STAT_CODE.

It is from this function

Code:
function smarty_function_get_stat_for_player($params, &$smarty)
{
    extract($params);

    if (empty($j_id)) {
        $smarty->trigger_error("get_stat_for_player: missing 'j_id' parameter");
        return;
    }
   if (empty($game_id)) {
        $smarty->trigger_error("get_stat_for_player: missing 'game_id' parameter");
        return;
    }
   
// FUNCTION BEGIN
mysql_select_db($dbName);
$query = "SELECT stat_code FROM stats ORDER BY stat_code";
$resp = mysql_query($query);

while ($data1 = mysql_fetch_array($resp))
{
    $new_line = array ();
    $new_line['STAT_CODE'] = $data1['stat_code'];

    $query = "SELECT player_id, player_l_name,
roster_jersey_id, roster_team_id, playing_time,
game_id, home_team_id,
team_id, team_name,
event_game_id, event_j_number, event_team_id, event_stat_code,
stat_name, stat_desc, stat_code, sum( stat_value ) AS player_stat_value
FROM  players, rosters, games, teams,`events` , stats
WHERE
game_id=event_game_id
AND event_game_id=roster_game_id
AND roster_game_id=".$game_id."
AND event_team_id=roster_team_id
AND roster_team_id=home_team_id
AND home_team_id=team_id
AND event_stat_code=stat_code
AND stat_code='".$new_line['STAT_CODE']."'
AND event_j_number=roster_jersey_id
AND roster_jersey_id=".$j_id."
AND roster_player_id=player_id
GROUP BY roster_jersey_id";

    $resp2 = mysql_query($query);

    while ($data2 = mysql_fetch_array($resp2))
   {
       $new_line['DATA2'][] = array (
      'PLAYER_STAT_VALUE'=>$data2['player_stat_value']
      
      );
    }
    $smarty->append ('DATA1', $new_line);//stores the new line.
}
 
 
}


And called to the Template like this:

Code:
{section name=nr loop=$games}
{get_stat_for_player j_id=$j_id game_id=$games[nr].game_id}

{foreach from=$DATA1 item=line name=sc}
{if $smarty.foreach.sc.first}
Game
<table border=1>
<tr>
{/if}
<td>{$line.STAT_CODE}<br>
{if $smarty.foreach.sc.last}
</table>
{/if}
{foreach from=$line.DATA2 item=info name=sv}
{$info.PLAYER_STAT_VALUE}
{/foreach}
</td>
{/foreach}
{/section}


How do i get it to give me a default value if there isn't a PLAYER _STAT_VALUE when using the foreach. Should i be using the {section}, if so do i need to change my function alot.

Thanks for the help
_________________
wolfram 30 --- 6-170 ---- upper v
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Feb 25, 2004 12:44 am    Post subject: Reply with quote

The default modifier should be helpful:

{$info.PLAYER_STAT_VALUE|default:"your default value"}
Back to top
View user's profile Send private message
shandley14
Smarty Rookie


Joined: 20 Aug 2003
Posts: 10

PostPosted: Wed Feb 25, 2004 1:40 am    Post subject: that doesn't work Reply with quote

that doesn't work ( i tried it already, hoping not to bug you guys untill desparate)

if we look at the array we'll see that the array[0] has a stat code value and thats it
The array[1] has stat code value AND a DATA2 array because the player has scored in that column.

Code:
Array (10)
0 => Array (1)
  STAT_CODE => A
1 => Array (2)
  STAT_CODE => B
  DATA2 => Array (1)
    0 => Array (1)
      PLAYER_STAT_VALUE => 2



AND my foreach in the template references DATA2

Code:
{foreach from=$line.DATA2 item=info name=sv}
{$info.PLAYER_STAT_VALUE}
{/foreach}


So i think that is why the default isn't working but not sure how to get around it
_________________
wolfram 30 --- 6-170 ---- upper v
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Feb 26, 2004 10:59 pm    Post subject: Re: that doesn't work Reply with quote

Ahh. I see now. Its not that the default isn't working, its that the foreach never gets executed for cases from DATA2 do not exist. You either need to make those results exist (even if null) or perhaps exploit the foreachelse functionality:
Code:
{foreach from=$line.DATA2 item=info name=sv}
{$info.PLAYER_STAT_VALUE}
{foreachelse}
    {* only enter here when DATA2 is empty *}
    default values...
{/foreach}
Back to top
View user's profile Send private message
shandley14
Smarty Rookie


Joined: 20 Aug 2003
Posts: 10

PostPosted: Fri Feb 27, 2004 3:20 am    Post subject: Thanks! it works Reply with quote

thank you very much! Laughing Laughing Laughing Laughing
_________________
wolfram 30 --- 6-170 ---- upper v
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP