View previous topic :: View next topic |
Author |
Message |
elrobbo6 Smarty Rookie
Joined: 01 Feb 2016 Posts: 9
|
Posted: Thu Dec 03, 2020 7:37 pm Post subject: strpos does not seem to be working. nor {break} |
|
|
Iv'e tried about 5 different ways for doing this but none of them seem to work. So I am hoping someone on here can spot what is wrong. here is the basic code:
Code: |
{assign var="qa" value=$question.answer}
{foreach from=$uanswers item=ans}
{$qa} : {$ans} <br>
{assign var="correct" value=$qa|strpos:$ans}
{if $correct}
right
{else}
wrong
{/if}
{/foreach}
|
the output is:
3.5 : 3 wrong
3.5 : 5 right
this is obviously not correct as both 3 and 5 are in the string. the string can also have 3 digits like 2.3.6. But that shouldn't matter for a strpos. I have tried a few variations of this and none of them work. $correct will be used to determine text displayed on the page. I have also tried using {php}{/php} with real php code but it doesn't work either. But it does work in a regular php file. I know I will be told it should not be done in the .tpl file but for this it needs to be. the tpl is being sent an array of questions and answers and is iterated through on the tpl to show results. Any help would be great, thanks. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Dec 04, 2020 7:30 pm Post subject: |
|
|
The PHP function strpos returns an integer that is the offset into the haystack where needle was found -- which could be the zeroth position -- and returns false only if not found.
The variable is being tested for false-like evaluation, which includes zero.
Please try to test for exactly false. |
|
Back to top |
|
elrobbo6 Smarty Rookie
Joined: 01 Feb 2016 Posts: 9
|
Posted: Fri Dec 04, 2020 7:50 pm Post subject: |
|
|
bsmither I will try that, I thought I had tried that with a different combination. Thanks |
|
Back to top |
|
elrobbo6 Smarty Rookie
Joined: 01 Feb 2016 Posts: 9
|
Posted: Fri Dec 04, 2020 8:26 pm Post subject: |
|
|
bsmither wrote: | The PHP function strpos returns an integer that is the offset into the haystack where needle was found -- which could be the zeroth position -- and returns false only if not found.
The variable is being tested for false-like evaluation, which includes zero.
Please try to test for exactly false. |
this worked! thank you. I must not have tried the false check while also using the correct strpos code. The issue though now is when I put a {break} in there I get a 500 error. i need to break out if any of the options come back as false since this means the whole question is wrong. According to the documentation on foreach loops this should work but it 500's.
Code: |
{assign var="qa" value=$question.answer}
{foreach from=$uanswers item=ans}
{$qa} : {$ans} <br>
{assign var="correct" value=$qa|strpos:$ans}
{if $correct == false}
{break} <----
{/if}
{/foreach}
<strong style="color: {if $correct == false}#990000{else}green{/if}">{if $correct == false}Incorrect{else}Correct{/if}</strong>
|
|
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Dec 04, 2020 10:51 pm Post subject: |
|
|
I would like for you to use:
Code: | {if $correct === false} |
Using two equal signs is testing for false-like. (Earlier, simply testing $correct was testing for empty-like.)
But the code did not throw an error for me. (Using 3.1.33)
Last edited by bsmither on Fri Dec 04, 2020 11:00 pm; edited 2 times in total |
|
Back to top |
|
elrobbo6 Smarty Rookie
Joined: 01 Feb 2016 Posts: 9
|
Posted: Fri Dec 04, 2020 10:53 pm Post subject: |
|
|
bsmither wrote: | I would like for you to use:
Code: | {if $correct === false} |
Using two equal signs is testing for false-like. (Earlier, simply testing $correct was testing for empty-like.) |
Yeah I have since changed that to ===. Still the {break} just creates an error for some reason |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Dec 04, 2020 11:19 pm Post subject: |
|
|
Have you examined PHP's error log? If you don't have access to the PHP script, then you may or may not have access to the log.
Without the ability to write your own exception handler, finding Smarty's throwing of exceptions may be difficult. |
|
Back to top |
|
|