TransWikia.com

Random image - corresponding text

Salesforce Asked by user48743 on December 22, 2020

Is there a way to display text that corresponds to random image in another part of an email? My code so far:

VAR @random, @num1, @num3
SET @num1 = 1 
SET @num3 = 3 


SET @random = random(@num1, @num3) 
]%% 

%%[IF @random == "1" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 1 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152367" src="Image 1 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

%%[ELSEIF @random =="2" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 2 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152368" src="Image 2 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

%%[ELSEIF @random =="3" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 2 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152366" src="Image 3 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

%%[ENDIF]%%

Couldnt find what i needed here – unless i missed something Showing random image in email

2 Answers

This will display text under the image:

%%[IF @random == "1" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 1 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152367" src="Image 1 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

<p>Some random text related to image 1</p>

%%[ELSEIF @random =="2" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 2 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152368" src="Image 2 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

<p>Some random text related to image 2</p>

%%[ELSEIF @random =="3" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 2 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152366" src="Image 3 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

<p>Some random text related to image 3</p>

%%[ENDIF]%%

If you need to display the text in a completely different place, just add it to the main script:

%%[
VAR @random, @num1, @num3, @text
SET @num1 = 1 
SET @num3 = 3 
    
SET @random = random(@num1, @num3) 

IF @random == "1" THEN
   set @text = "some text 1"
ELSEIF @random =="2" THEN 
   set @text = "some text 2"
ELSEIF @random =="3" THEN 
   set @text = "some text 3"
ENDIF

]%% %%[IF @random == "1" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 1 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152367" src="Image 1 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

%%[ELSEIF @random =="2" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 2 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152368" src="Image 2 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

%%[ELSEIF @random =="3" THEN ]%% 
<table width="100%" cellspacing="0" cellpadding="0"><tr><td align="center">
<a href="Image Link 2 here" title="" alias="" conversion="false" data-linkto="https://"> <img data-assetid="152366" src="Image 3 URL here" alt="" height="390" width="700" style="display: block; padding: 0px; text-align: center; height: 390px; width: 700px; border: 0px;"></a></td></tr></table>

%%[ENDIF]%%

And include %%=v(@text)=%% anywhere where this text should be displayed.

Correct answer by zuzannamj on December 22, 2020

What I might do to account for potential growth of random information, is to store all the information inside of a data extension.

I would have the following in the DE:

ID   |   ImageURL   |   Text
---------------------------------
1    | myImageURL1  |  Some Text1
2    | myImageURL2  |  Some Text2

Where ID is the primary key of the DE. This would store all the dynamic info you want associated together, so you can add/remove fields as needed.

Then inside the email, I would do the same Random function, but I would also do a LookupRows() and then set the values via ROW() and Field().

See below:

First, you need to get the size of the DE for the @max value using DataExtensionRowCount function:

SET @min = 1
SET @max = DataExtensionRowCount('myDE')

This will set the total number of rows in the DE to the max for the random function.

Second, you will gather the random number via AMPScript:

SET @random = Random(@min,@max)

This will grab a random number between 1 and the row count of your content DE. For the sake of this example, let's say the number is 3.

Next, now that we have the random number, what do we do? We then do a LookupRows() to retrieve the row from your DE.

SET @randRS = LookupRows('myDE',"ID",@random)

Finally, this will then return a rowset of the identified record. You can then use Row() and Field() to gather the info you need.

SET @row = Row(@randRS,1)
SET @image = Field(@row,"ImageURL")
SET @text = Field(@row,"Text")

You then just put the image/text as variable calls. Like so:

<img src="%%=v(@image)=%%" />
<p>%%=v(@text)=%%</p>

no need to use any if statements, etc.

Answered by Gortonington on December 22, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP