Correct way to check if an array element exists

Author Message

Eirik Alfstad Johansen

Tuesday 15 March 2005 4:10:49 am

Hi,

I'm using the following template code to decide which option in a drop down box that should be selected:

{section name=Child loop=$partner_accounts}
<option value="{$:key}"  {switch match=$level.partner_objects[$:key]}{case match=true}SELECTED{/case}{case}{/case}{/switch}>{$:item}</option>
{/section}

The code works fine, but the debug gives the following error:

No such attribute for array(1): *$:key*

Of course, the attribute with the specific key will not always be present, and that's exactly the purpose of the check. So, how can I make my code "debug-friendly"?

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

liu spider

Tuesday 15 March 2005 9:11:49 am

try this:
you can use isset($level.partner_objects[$:key]} to test whether the variable is availalbe.

http://liucougar.scim-im.org
SCIM Input Method Platform
http://scim.sf.net
SJSD Online Editor
http://sf.net/projects/sjsd

Hans Melis

Tuesday 15 March 2005 9:19:17 am

Actually, the template function is: <b>is_set()</b>. The isset() function is the PHP variant.

Hans
http://blog.hansmelis.be

Eirik Alfstad Johansen

Wednesday 16 March 2005 12:57:21 am

Thanks a lot, guys! I changed it, and now the debug output looks nice and tidy. Oh, happy days. :)

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 19 2025 00:21:00
Script start
Timing: Jan 19 2025 00:21:00
Module start 'layout'
Timing: Jan 19 2025 00:21:00
Module start 'content'
Timing: Jan 19 2025 00:21:00
Module end 'content'
Timing: Jan 19 2025 00:21:00
Script end

Main resources:

Total runtime0.0210 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0076 588.0469152.6406
Module start 'layout' 0.00760.0047 740.687539.4766
Module start 'content' 0.01230.0063 780.164193.4922
Module end 'content' 0.01860.0024 873.656334.3047
Script end 0.0210  907.9609 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002913.7359140.0002
Check MTime0.00125.8938140.0001
Mysql Total
Database connection0.00083.969310.0008
Mysqli_queries0.004119.446930.0014
Looping result0.00000.090810.0000
Template Total0.00199.010.0019
Template load0.00104.554810.0010
Template processing0.00094.465110.0009
Override
Cache load0.00073.117110.0007
General
dbfile0.00115.335580.0001
String conversion0.00000.046540.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs