Author Topic: <TMPL_IF> in a loop  (Read 1069 times)

0 Members and 1 Guest are viewing this topic.

JSPASH

  • Guest
<TMPL_IF> in a loop
on: Tue, 25. February 2003 - 20:15:26
I am having trouble with a <TMPL_UNLESS> tag in a loop. Please note that this a very stripped down version of the code and template.  Everything works except that only the first part of the <TMPL_IF> is ever used.

Code: [Select]

$tmpl-newLoopmain_loop;

$yesterday = -99;

while$q-next_record

$today = $q-fdate; // Order Date

//Display cumulative summary line when date changes

if$today $yesterday

  $tmpl-setVarsummary_line, 1;

  $xsummary_line = $total;

  $yesterday = $today;

else

  $tmpl-setVarsummary_line, 0;

  $xguff = abc;

  $xmore_guff = 123;

  $xeven_more_guff = 666;

  $total += $xmore_guff;



    $tmpl-addRow$x;



$tmpl-addLoop;



And the template
Code: [Select]

table

tr class=header

  thCOLUMN 1/th

  thCOLUMN 2/th

  thCOLUMN 3/th

/tr

TMPL_LOOP NAME=main_loop

TMPL_IF NAME=display_summary

tr

  td colspan=3TMPL_VAR NAME=summary_line/td

/tr

TMPL_ELSE

tr

  tdTMPL_VAR NAME=guff/td

  tdTMPL_VAR NAME=more_guff/td

  tdTMPL_VAR NAME=even_more_guff/td

/tr

/TMPL_IF

/TMPL_LOOP

/table


releasedj

  • Guest
<TMPL_IF> in a loop
Reply #1 on: Wed, 26. February 2003 - 15:20:25
From what I can tell, I think you meant to put $tmpl->setVar("display_summary", 1);

Anyway, if the display_summary variable is supposed to be set for every loop, then you should set it in the "$x" array, i.e.:

Code: [Select]

$tmpl-newLoopmain_loop;

$yesterday = -99;

while$q-next_record

  �$today = $q-fdate; // Order Date

  �//Display cumulative summary line when date changes

  �if$today $yesterday

      $xdisplay_summary = 1;

      $xsummary_line = $total;

      $yesterday = $today;

  � else

      $xdisplay_summary = 0;

      $xguff = abc;

      $xmore_guff = 123;

      $xeven_more_guff = 666;

      $total += $xmore_guff;

  �

    $tmpl-addRow$x;



$tmpl-addLoop;





otherwise, you just resetting the global var "display_summary" to whatever the last action is in the loop.

I hope this makes sense.

Kelvin

JSPASH

  • Guest
<TMPL_IF> in a loop
Reply #2 on: Wed, 26. February 2003 - 16:42:25
I realise that I wasn't very clear on my intentions.  To simplify (ignoring the previous code) I would like to have a loop of order details, but at the end of each day display a summary line of that day's orders.

EX.
{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{TOTAL_VALUE_FOR_ORDERS_DAY_1}

{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{TOTAL_VALUE_FOR_ORDERS_DAY_2}

{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{ORDER_ID}{ORDER_VALUE}{EMAIL_ADDRESS}
{TOTAL_VALUE_FOR_ORDERS_DAY_3}

So I figured I could use one loop to iterate through the orders, cumulate the total order value, and if the day changed, display a summary line instead of the normal line.  Using the <TMPL_IF> tag.

Am I wrong in assuming that a <TMPL_VAR> can have more than 1 value in subsequent loop iterations?

My real problem is much more detailed than this, but this covers the basics.

BTW-Thanks for the quick replies.  And thank you for what I consider the best PHP template engine out there!

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
<TMPL_IF> in a loop
Reply #3 on: Wed, 26. February 2003 - 20:35:11
I am not sure what you want to use TMPL_IF for but I think you have a classic programming problem.

If I assume correctly, you have 365 days with X orders?
Days + Orders

I had the same problem with
Users + Posts

I solved it that way:

Code: [Select]
$select_posts = SELECT p_id, p_post, p_u_id FROM gb_posts ORDER BY p_u_id;

$select_users = SELECT u_id, u_name FROM gb_users ORDER BY u_id;

$result_posts = mysql_db_query$dbname, $select_posts, $connect;

$result_users = mysql_db_query$dbname, $select_users, $connect;



$row_posts = mysql_fetch_array$result_posts;



$outer = array;

while $row_users = mysql_fetch_array$result_users



  $inner = array;

  while $row_postsp_u_id == $row_usersu_id

  

   $eintrag = substr$row_postsp_post, 0, 15;

   array_push$inner, array

    'p_id' = $row_postsp_id,

    'eintrag' = $eintrag

    

   ;

   $row_posts = mysql_fetch_array$result_posts;

  

  

  array_push$outer, array

    'u_id' = $row_usersu_id,

    'u_name' = $row_usersu_name,

    'inner' = $inner

    

  ;





$tmpl-setloop'outer', $outer;



$tmpl-pparse;

?



pre

? print_r$outer; ?

/pre


Template:
Code: [Select]
tmpl_loop name='outer'

bUser tmpl_var name='u_id' - tmpl_var name='u_name'/b br

  tmpl_loop name='inner'

   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p_id tmpl_var name='p_id' - tmpl_var name='eintrag'br

  /tmpl_loop

/tmpl_loop


Note: I had to program in COBOL this problems a lot. They are called "Gruppenwechsel" (Group change) in German. COBOL S*CKS!

releasedj

  • Guest
<TMPL_IF> in a loop
Reply #4 on: Fri, 28. February 2003 - 10:36:17
Have a close look at the code I wrote in the first post.

Note that you cannot set a VAR in a loop using setVar(). You must build the loop in an array (as you are already doing).

Note also that what TMPL_IF does is test a VAR to see if it evaluates to true, so you shouldn't treat VARs that you wish to display any differently than VARs that you use in an IF statement.

I hope this helps, if not then try sending an email to vlib at activefish dot com with the code you're trying top execute, I could then have a look and see where you're going wrong.

Also, don't forget to set $yesterday to the value of $today in each loop.

Kind regards,

Kelvin