Re: [SMARTY] Bit code

From: Date: Sat, 29 Mar 2003 15:20:05 +0000
Subject: Re: [SMARTY] Bit code
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-8987@lists.php.net to get a copy of this message
On Sat, Mar 29, 2003 at 08:13:14AM -0600, Richard Thomas wrote: > putting (int) in front fixed it However the wierd thing is, thats not > the real code, The real code loops through an array with 8 values and > the problem only happens on a couple of the 8 spots, But the information > is pulled the same way so all the variables are exactly the same. > > > {section name=feature loop=$domain_features} > <td class=noborder><input {if $domains.features & > (int)$domain_features[feature].bitvalue} > CHECKED {/if} class=buttons type=checkbox name=domainfeatures[] > value={$domain_features[feature].bitvalue} {$readonly}>{$doma > {if $smarty.section.feature.iteration is div by 8} > </tr><tr> > {/if} > {/section} > > > Now the way your explaining it this without the (int) all of them should > of been checked, not just a couple of them. > > Results without the int have bitfields 1 , 16, 32, 128 checked when > domain.features = 1 not necessarily. if you do "$a & $b" with $a and $b being *strings* of digits the ascii-representation is bit-wise anded. this means "'2' & '1' = '0'" ( %00110010 & %00110001 = %00110000). after that php the result is the string containing "0". php automagically converts this to false if asked for the boolean representation (like in the if-statement). tricky, not? conclusion: php's type-auto-conversion can be a pain in the ass and you encountered a situation where it is more than counter-intuitive. for your actual problem: if it is feasable in your current application you may be best of converting your numbers to arrays with a php-function and assign those arrays to smarty, instead of the plain numbers. you can use the {html_checkboxes}-function that comes with smarty-2.5.0-RC2, then. see: http://smarty.php.net/manual/en/language.function.html.checkboxes.php you just have to convert your arrays back to numbers when processing the form-data that is submitted. if you don't want to use RC2 yet, you can copy shared.escape_special_chars.php and function.html_checkboxes.php from the RC2-distribution (or from CVS) to your 2.4.2-plugins-directory. i didn't test that, but i don't see a reason why this function shouldn't work with Smarty-2.4.2. (but the RC-2 is good anyway, believe me :) greetings messju > On Sat, 2003-03-29 at 08:05, messju mohr wrote: > > On Sat, Mar 29, 2003 at 01:58:08PM +0000, messju mohr wrote: > > > On Sat, Mar 29, 2003 at 07:38:29AM -0600, Richard Thomas wrote: > > > > Having some problems creating dynamic checkboxes > > > > > > > > > > > > Ok, the following code should produce the same results, Nothing.. Value > > > > wise they are the exact same thing.. However if you run the code you > > > > will see "First" shows up and not second.. > > > > > > > > > > > > {assign var="bitvalue" value='1'} > > > > {assign var="item" value='16'} > > > > {if $bitvalue & $item} First{/if} > > > > {if $bitvalue & 16} Second{/if} > > > > > > i think the bug is in the automatic type handling of php. 16 is an int > > > while $item is a string > > > > actually it is not a bug. php does a bitwise-and if both operands are > > strings. so '1' and '16' results in the string "1".chr(0) > > this leads to > > true for the "if". > > > > HTH > > messju > > > (smarty treats every scalar value as a string > > > in the first place). > > > > > > example: > > > > > > printf("%d\n", '16' & '1'); // says "1" > > > wrong > > > printf("%d\n", 16 & 1); // says "0" good > > > > > > > > > do: > > > > > > {if (int)$bitvalue & (int)$item} First{/if} > > > {if (int)$bitvalue & 16} Second{/if} > > > > > > and it works as expected. > > > > > > greetings > > > messju > > > > > > -- > > > Smarty General Mailing List (http://smarty.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > -- > > Stockoptions: Die Geschäftsleitung behält sich vor, die Prügelstrafe > > einzuführen. > > Manfred Worm Schäfer > -- Stockoptions: Die Geschäftsleitung behält sich vor, die Prügelstrafe einzuführen. Manfred Worm Schäfer

« previous php.smarty.general (#8987) next »