View previous topic :: View next topic |
Author |
Message |
jaxyeh Smarty Rookie
Joined: 07 Aug 2003 Posts: 6
|
Posted: Thu Aug 07, 2003 6:06 pm Post subject: pass MULTIDIMENSIONAL ARRAY VARIABLE in PHP |
|
|
I don't think it implied to Smarty Template Engine part, but more of PHP Array. I was trying to setup a variable to assign the follow SELECTED OPTIONS for {_______ selected=" " } ....
I didn't want to re-query the DATABASE, especially when the $USER_RESULTS already includes its variables. Sad enough, I am still new to PHP arrays....
I need to assign those variables from $user_results array...
[php:1:b428626c88]
$query = "SELECT * FROM users where userid='".$_GET['userid']."'";
$user_results = $db->array_query($query);
$smarty->assign('user_results', $user_results);
// how to assign these arrays below here??
$smarty->assign('group_id', $user_results['groupid']);
$smarty->assign('is_Admin', $user_results['isAdmin']);
$smarty->assign('is_Active', $user_results['isActive']);
[/php:1:b428626c88]
I need to assign those variables, so I can setup selected options in the forms.. I think I can add {if} functions in *.tpl, but I would prefer to assign it inside the php codes..
hope u can help this out!!
thanks,
Jax |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Thu Aug 07, 2003 11:27 pm Post subject: |
|
|
Hi,
with this line
[php:1:0fff81b1bb]$smarty->assign('user_results', $user_results); [/php:1:0fff81b1bb]
you already have assigned all the infos from the record.
In templates you can use associative array using the dot notation:
Code: | {$user_results.groupid}
{$user_results.isAdmin}
{$user_results.isActive}
|
Note, that these are now the original fieldnames from your users-table. If it contains more than you need, have SELECT fetch only the fields you actually require (which is always a good habit).
Because in PHP, the $user_results variable is just an regular array, you can add other keys to it (btw. NEVER accept any values directly from $_GET & Co. for your sql statements, always make sure they make sense or are of the correct type, before you use them or someone can inject some code with the URL):
[php:1:0fff81b1bb]<?php
$uid = (int)$_GET['userid']; // just a little tweak
$query = "SELECT * FROM users where userid='".$uid."'";
$user_results = $db->array_query($query);
if ($user_results['isAdmin']) {
$user_results['SelAdmin'] = " selected";
}
if ($user_results['isActive']) {
$user_results['SelActive'] = " selected";
}
$smarty->display('form.tpl');
?>[/php:1:0fff81b1bb]
and in the template you have (I left some extra space):
Quote: |
<option name="bla" {$user_results.SelAdmin} >Admin</option>
<option name="bla" {$user_results.SelActive} >Active</option>
|
The string " selected" will only appear if the *optional* array elements 'SelAdmin' or 'SelActive' are available.
Have fun,
CirTap |
|
Back to top |
|
jaxyeh Smarty Rookie
Joined: 07 Aug 2003 Posts: 6
|
Posted: Fri Aug 08, 2003 5:02 pm Post subject: |
|
|
Actually.. this is the answer I was looking for
[php:1:ef6496137b]
$smarty->assign('group_id', $user_results[0]['groupid']);
$smarty->assign('is_Admin', $user_results[0]['isAdmin']);
$smarty->assign('is_Active', $user_results[0]['isActive']);
[/php:1:ef6496137b]
because the original method you just explained didn't work for some reason so I will force it to first string array query...
by the way, I have learned something new about (int)$_GET ... Im going to put (int) on all php variable that requires INTGERS.. I truly appreicate that wonderful tweak!!
thanks,
Jax |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Aug 08, 2003 9:03 pm Post subject: |
|
|
jaxyeh wrote: | Actually.. this is the answer I was looking for
[php:1:b8ce34a14d]
$smarty->assign('group_id', $user_results[0]['groupid']);
$smarty->assign('is_Admin', $user_results[0]['isAdmin']);
$smarty->assign('is_Active', $user_results[0]['isActive']);
[/php:1:b8ce34a14d] |
maybe a combination of this and CirTap's one is perfect:
[php:1:b8ce34a14d]
$smarty->assign('user', $user_results[0]);
[/php:1:b8ce34a14d]
and then access
Code: |
{$user_results.groupid}
{$user_results.isAdmin}
{$user_results.isActive}
|
hopefully happily |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Fri Aug 08, 2003 9:09 pm Post subject: |
|
|
Hi,
you do not require (int) for a variable to be an integer. PHP will automagically handle "1" + 5 = 6.
It's only that on a string like "123;DELETE * FROM users;" (int) will return the number part only: 123. That's the only reason I put this in as I saw it in your dangerous SQL
It also allows "lazy" value checking:
$var1 = "123-blabla";
$var2 = 123;
if ( (int)$var1 == $var2) ) { ... }
or (my favourite)
settype($CanBeAnything, 'array');
foreach($CanBeAnything as $k => $v) {}
Have fun,
CirTap |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Aug 08, 2003 9:41 pm Post subject: |
|
|
@ClrTap: in your example:
if ( (int)$var1 == $var2) ) { ... }
wouldn't that have more merit as:
if ( (int)$var1 === $var2) ) { ... }
since $var1 will be coerced to the proper type in the first example? Just curious. |
|
Back to top |
|
CirTap Smarty Pro
Joined: 04 Jun 2003 Posts: 106
|
Posted: Fri Aug 08, 2003 11:53 pm Post subject: |
|
|
@boots:
using typecasting (int) and type check === is redundant here, at least if you know $var2 is a "proofed" integer.
$var1 will become a native 123 integer before === is executed, so there is no difference.
I just made a litlle test as I you made me curious then about case 'c' :[php:1:65d1cf36f0]<?php
$var1 = "123-blabla";
$var2 = 123;
if ( (int)$var1 == $var2) echo " a == (int)"; # 123 == 123
if ( (int)$var1 === $var2) echo " b === (int)"; # 123 === 123
if ( $var1 == $var2) echo " c =="; # no typecasting
if ( $var1 === $var2) echo " d ===";
// compare string and integer
$var3 = "123";
if ( $var2 === $var3) echo " e ===";
// cast integer to string and compare
if ( $var3 === (string)$var2) echo "\n e === (string)";
?>
Results:
a == (int)
b === (int)
c ==
f === (string)
[/php:1:65d1cf36f0]
It seems like PHP will do the type juggling in 'c'.
As expected, using === without casting the vars will fail in 'd' and 'e', and making 123 a string, will succeed again in 'f'.
CirTap |
|
Back to top |
|
|