0

I'm extremely new to using AMPScript, and wonder if any of you AMPScript savvies can help me.

I am trying to create an AMPScript inside of an AMPScript.

The point to this HTML is to present the user a graph of his last 5 weeks of stats.

If the user doesn’t have stat for 5 weeks but only 4 or 3 or 2 the stat’s graph will present only the weeks he have stat in.

In my DE I have a column of flag_weekstart that helps me to determine how many weeks to present in the graph.

The 5Total attribute represent the total stat a user has in week number 5 (5 weeks ago).

I am using this code:

%%[

VAR @5Total
SET @5Total = AttributeValue("5Total")

VAR @weekstart1

IF [flag_weekstart] == "5" THEN

 SET @weekstart1 = '
 <table width="90%" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td width="20%" style="">
      <span style="">Aug 28</span>
    </td>
    <td width="20%" style="">
      <span style="">Sep 04</span>
    </td>
    <td width="20%" style="">
      <span style="">Sep 11</span>
    </td>
    <td width="20%" style="">
      <span style="">Sep 18</span>
    </td>
    <td width="20%" style="">
      <span style="">Sep 25</span>
    </td>
  </tr>
</table>
<table width="90%" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td width="100%" style="">
      <span style=""><center>Total Measurements</center></span>
    </td>
  </tr>
</table>
<table width="90%" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td width="20%">
      <span style=""><center>%%=v(@5Total)=%%<br>&nbsp;</center></span>
    </td>
    <td width="20%">
      <span style=""><center>%%4Total%%<br>&nbsp;</center></span> 
    </td>
    <td width="20%">
      <span style=""><center>%%3Total%%<br>&nbsp;</center></span>  
    </td>
    <td width="20%">
      <span style=""><center>%%2Total%%<br>&nbsp;</center></span>  
    </td>
    <td width="20%">
      <span style=""><center>%%1Total%%<br>&nbsp;</center></span>  
    </td>
  </tr>
</table>'
]%%

The code is continuing with: IF [flag_weekstart] == "4" THEN

Until 1.

I tried puling the 5Total attribute using %%=v(@5Total)=%% and %%5Total%% with no luck.

The value I am getting is %%5Total%% like the AMPScript doesn't know from where to pull the data from.

Any ideas?

1 Answer 1

2

I think you can approach this with a loop instead of using HTML in AMPScript variables. I'd recommend using a loop based on flag_weekstart. Something like this:

%%[

var @Total5, @Total4, @Total3, @Total2, @Total1, @flag_weekstart, @today
set @today = now(1)
set @Total5 = AttributeValue("5Total")
set @Total4 = AttributeValue("4Total")
set @Total3 = AttributeValue("3Total")
set @Total2 = AttributeValue("2Total")
set @Total1 = AttributeValue("1Total")
set @flag_weekstart = AttributeValue("flag_weekstart")

/* for testing */
set @Total5 = "123"
set @Total4 = "234"
set @Total3 = "456"
set @Total2 = "789"
set @Total1 = "012"
set @flag_weekstart = 4

]%%

<table border="1">

  %%[

  /* output headings */

  for @i = @flag_weekstart downto 1 do

    if @i == @flag_weekstart then
      output(concat("<tr>"))
    endif

    set @headingDate = formatDate(dateadd(@today,multiply(-7,subtract(@i,1)),"D"),"YYYY-MM-DD")

    ]%%
      <td>Week of %%=v(@headingDate)=%%</td>
    %%[

    if @i == 1 then
      output(concat("</tr>"))
    endif

  endif

  next @i

  /* output row */
  for @i = @flag_weekstart downto 1 do

    if @i == @flag_weekstart then
      output(concat("<tr>"))
    endif

    if @i == 1 then
    ]%%
      <td>%%=v(@Total1)=%%</td>
    %%[
    endif
    if @i == 2 then
    ]%%
      <td>%%=v(@Total2)=%%</td>
    %%[
    endif
    if @i == 3 then
    ]%%
      <td>%%=v(@Total3)=%%</td>
    %%[
    endif
    if @i == 4 then
    ]%%
      <td>%%=v(@Total4)=%%</td>
    %%[
    endif
    if @i == 5 then
    ]%%
      <td>%%=v(@Total5)=%%</td>
    %%[
    endif

    if @i == 1 then
      output(concat("</tr>"))
    endif

  endif

  next @i

  ]%%

</table>

Output

+------------------+------------------+------------------+------------------+ 
|Week of 2017-09-19|Week of 2017-09-26|Week of 2017-10-03|Week of 2017-10-10|
+------------------+------------------+------------------+------------------+
|234               |456               |789               |012               |
+------------------+------------------+------------------+------------------+
3
  • Hi Adam, Thanks for that! I made the changes you suggested with my own adjustments and the code. My only problem is that when @flag_weekstart is more than 1 the output shows all the results from 5 down to 1. Any advice? Commented Oct 11, 2017 at 7:44
  • sprigs Maybe there us a way to add hide after a criteria is being match like: if i == flag_weekstart then output(concat("<tr>")) endif and hide other results Commented Oct 11, 2017 at 7:56
  • You'll want to remove the variable declarations after the /* for testing */ part. Since I didn't have your data set, I hardcoded the values. Commented Oct 11, 2017 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .