Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing array in smarty

Tags:

php

mysql

smarty

I am trying to reference an array through a function I am calling

$bio['results'] = $db->query(sprintf('SELECT * FROM `athlete_bio_results` WHERE `PersonID` = %d ORDER BY `athlete_bio_results`.`Type` DESC, `athlete_bio_results`.`Date` DESC;', $id));

    $smarty->assign(value($params, 'to', 'athlete'), $bio);

In my view I am referencing through a foreach loop some of the fields through this array.

How would I reference a field outside of this foreach loop for checking..For instance in the foreach loop I can go {$result.Type) which will bring up everything in the db under that field. I want to check for a certain type before the foreach loop. My problem is referencing that piece of data outside the foreach loop. How would I achieve this? Thanks!

{if $athlete.results['Type'] == 'National'} <---- This is not working for me.
    {foreach $athlete.results as $result}
        {$result.Date|substr:0:-6} 
        {$result.Event} - 
        {$result.Result}
            {if $result.Junior == 'no' || $result.Junior == ''}
            {else}
                {'(Jr. Div.)'}
            {/if}
        <br />
    {/foreach}
{/if}

EDIT

This is the code that works

{foreach $athlete.results as $result}
    {if $result['Type'] == 'National'}
        {$result.Date|substr:0:-6} 
        {$result.Event} - 
        {$result.Result}
            {if $result.Junior == 'no' || $result.Junior == ''}
            {else}
                {'(Jr. Div.)'}
            {/if}
    <br />
    {/if}
{/foreach}  

Thanks for your help in guiding me to the right solution :)

like image 564
wowzuzz Avatar asked May 21 '26 21:05

wowzuzz


1 Answers

Since you're accessing the first element of the array, use the key [0]:

{if $athlete.results[0]['Type'] == 'National'}

Actually I'm not certain that will work. If it doesn't, try this syntax instead.

{if $athlete.results[0].Type == 'National'}

EDIT Should you be checking Type inside the foreach?

{foreach $athlete.results as $result}
  {* Check inside the loop... *}
  {if $result['Type'] == 'National'}
    {$result.Date|substr:0:-6} 
    {$result.Event} - 
    {$result.Result}
        {if $result.Junior == 'no' || $result.Junior == ''}
        {else}
            {'(Jr. Div.)'}
        {/if}
    <br />
  {/if}
{/foreach}
like image 133
Michael Berkowski Avatar answered May 24 '26 14:05

Michael Berkowski