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

Empty value in Foreach - causes onscreen error

 
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
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sun May 15, 2022 9:55 am    Post subject: Empty value in Foreach - causes onscreen error Reply with quote

Hello.

I'm getting an error in a foreach loop.

Warning: Creating default object from empty value in PATHNAME/templates/compiled/d69f786086b63424b6203cc9ba5bba679235ae76_0.file.view_cats_list.tpl.php on line 113

CODE:

3. <?php echo $_smarty_tpl->tpl_vars['DOB1']->value;?>
<br />
<?php if (preg_match("/(\d\d\d\d+)\/(\d+)\/(\d+)/",$_smarty_tpl->tpl_vars['v']->value['DOB'],$_smarty_tpl->tpl_vars['match']->value)) {?>

(the BR tag is 112 & the line after is 113).

TEMPLATE:

{foreach from=$data.cats key=k item=v}


1. {$v.DOB}<br />
{if ! isset($v.DOB)}{assign var=$v.DOB value=""}{/if}
2. {$v.DOB}<br />

{assign var='DOB1' value=$v.DOB}
3. {$DOB1}<br />
{if "/(\d\d\d\d+)\/(\d+)\/(\d+)/"|preg_match:$DOB1:$match} // LINE 113

{assign var="M1" value=$match.1}
{assign var="M2" value=$match.2}
{assign var="M3" value=$match.3}

4. {$M3}/{$M2}/{$M1}<br />

{assign var="DOB1" value="$M3/$M2/$M1"}
5. {$DOB1}<br />
{/if}
<tr><td>{$k}</td><td>{$v.name}</td><td>{$v.gender}</td><td>{$DOB1}</td><td>{$v.colour}</td><td>{$v.hair}</td><td>{$v.rescued}</td><td>{$v.fosterhome}</td></tr>
{/foreach}

ISSUES

1) I store the dates in the adtabase as yyyy/mm/dd & dispalying them on-screen as dd/mm/yyyy
2) after '1' I test if $v.DOB is set or not and if not, make it set as a blank. (no date)

I then assign $v.DOB to $DOB1 (does that make a difference in the preg routine?)

I then do the preg check.

with all my echos the template,i cannot see any unsettted (is that a word) values they're all set or blank. but blank values shouldn't break a Preg ??

Any ideas?


Here is my OUTPUT:-

( 4 & 5 only appear if they meet the preg match. Ive checked the MYSQL database & there's only 1 entry that does meet the preg criteria (so far).

i'm not missing any data from the database that's not appearing on screen. My database has 5 records so steps 1-3 appear multiple times, one per record.

(I was going to do a MODIFY routine to fix the incorrect data - But I then saw the error message, not sure how / why. its not a live part of the website (yet)

OUTPUT:

1. 2022
2. 2022
3. 2022

Warning: Creating default object from empty value in PATHNAME/templates/compiled/d69f786086b63424b6203cc9ba5bba679235ae76_0.file.view_cats_list.tpl.php on line 113
1. 0
2. 0
3. 0
1. 0
2. 0
3. 0
1. 2022
2. 2022
3. 2022
1. 2022/05/01
2. 2022/05/01
3. 2022/05/01
4. 01/05/2022
5. 01/05/2022
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sun May 15, 2022 10:35 am    Post subject: Reply with quote

Ive changed the tempalte slightly, But still get the error in the Preg.

1. {$v.DOB}<br />
{if isset($v.DOB)}
{assign var="DOB1" value=$v.DOB}
{else}
{assign var="DOB1" value=""}
{/if}
2. {$v.DOB}<br />

(the rest of the lines are the same as my 1st post above)
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Mon May 16, 2022 9:34 am    Post subject: Reply with quote

Fixed it - adjusting the main PHp script. (removed preg from main template).
Back to top
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Mon May 16, 2022 1:01 pm    Post subject: Reply with quote

Ive got the same error in another tenplate now.

fixed it in the main PHP script (I didn't want to wait a few days to get it fixed).

Does anyone know the cause - with the template?

Ive never had this error before
Back to top
View user's profile Send private message
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