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

How to check an Array-value...

 
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
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed May 14, 2003 3:04 pm    Post subject: How to check an Array-value... Reply with quote

Hi!

Smarty is and should not be as powerful as PHP itself, but now I have a problem:

In my php-script I do:

Code:
$tpl->assign('all', $db->getAll('SELECT...')); // PEAR::DB
$tpl->assign('users', $_SESSION['users']);
$tpl->display('lt_tabelle.tpl');


in my template-File:

Code:
{foreach from=$all item="row"}
    <tr>
      <td align="center" valign="top">&&
        <input type="checkbox">
      </td>
      <td>{$row->user_id}</td>
      <td>{$row->tel}</td>
      <td><a href="#">{$row->email}</a></td>
    </tr>
{/foreach}


That works fine, but I want to have the checkbox checked, if the user_id of a row is a value of $_SESSION['users'], which is something like: array(1,5,99...);

In my template I can access it by {$users}, how could I build this in? I must build in {if} into the <input> tag, but what would be the right if-expression?

Or how could I change the variables in PHP so I can use something like

Code:
{if $users.$row->user_id} checked{/if}


The problem is that I want to give back the data from database(all) as objects, so I cannot write in it anymore from PHP.

Any ideas?

regards
Andreas


Last edited by andreas on Wed May 05, 2010 10:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed May 14, 2003 4:00 pm    Post subject: Reply with quote

If I got you correctly, you can write a modifier to wrap PHP's in_array. Then use your modifier to check for values in a given array.
Back to top
View user's profile Send private message
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed May 14, 2003 4:20 pm    Post subject: Reply with quote

You think of something like:
Code:
$smarty->register_modifier("contains","in_array");


But I need 2 parameters for in_array(), how can I do this?

Would be nice if I could assign the array to the modifier in PHP, so that I can use it in my templates like:
Code:
{if $var|contains}


is this possible?

Thank you!

________
extreme vaporizer review

Last edited by andreas on Fri Feb 04, 2011 9:00 am; edited 2 times in total
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed May 14, 2003 4:58 pm    Post subject: Reply with quote

yes: see the manual on plugin modifiers. For example, here's a super simple LINK modifier that accepts 4 parameters:

Code:
function smarty_modifier_link($title, $href='', $id='', $target='')
{
    if ( !empty($target) ) {
        $_id = (empty($id)) ? '' : 'id="'.$id.'" ';
        $__id = (empty($id)) ? '' : "#$id";
       return '<a '.$_id.'target="'.$target.'" href="'.$href.$__id.'">'.$title.'</a>';
    }
    if ( !empty($id) ) {
        $_id = (empty($id)) ? '' : 'id="'.$id.'" ';
        $__id = (empty($id)) ? '' : "#$id";
       return '<a '.$_id.'href="'.$href.$__id.'">'.$title.'</a>';
    }
    if ( !empty($href) ) {
       return '<a href="'.$href.'">'.$title.'</a>';
    }
   return '<a>'.$title.'</a>';
}


eg:
{"My Site"|link:"myreference.php":"myid":"_blank"}

I'd give you an example more specific to your task, but I'm in a rush at the moment and anyhow, it makes a good excercise--modifiers rock!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed May 14, 2003 5:00 pm    Post subject: Reply with quote

{if $var|in_array:$array}... works
you don't even have to register a modifier for that.

if you want opposit order, you have to write a modifier to wrap it:
modifier.contains.php:
Code:

function smarty_modifier_contains($haystack, $needle) {
  return in_array($needle, $haystack);
}


but be careful to call it
{if $array|@contains:$var}...

(note the @!) without the @ the modifier isn't called for $array but for each element of $array, which is not what you want.

the order is a matter of taste. i think "contains" reads nicer than "in_array", but the "@" is not nice (and easily forgotten) and not necessary for "in_array"

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
andreas
Smarty Pro


Joined: 22 Apr 2003
Posts: 106

PostPosted: Wed May 14, 2003 5:33 pm    Post subject: Reply with quote

Thank you!

I tried some things before and it doesn't work as I expected, so I rewrote the DB-Reqest, now I do this:
Code:
while ($row = $result-&gt;fetchRow()) {
    if($users[$row['user_id']]) {
        $row[checked] = TRUE;
    }
    $data[] = $row;
}
$tpl-&gt;assign('data', $data);


and in smarty
Code:
{if $row.checked} checked{/if}


This looks better for me, I don't like using PHP-functions from smarty, this way it works fine, or what do you think about it?

________
full melt hash

Last edited by andreas on Fri Feb 04, 2011 9:00 am; edited 2 times in total
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed May 14, 2003 6:38 pm    Post subject: Reply with quote

yes, that looks smartest! Smile let php be the workhorse and keep the template free from function-calls and stuff.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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