![](/img/trans.png)
[英]How to create HTML template with images for newsletter in Zend Framework 2
[英]Randomly show image + link in html newsletter template
假設我有一個包含 4 張圖像的列表,並且每次加載時事通訊時我都嘗試隨機顯示其中的 2 張。
我有一個包含以下代碼的文件 show_image.php:
$images = array(
0 => array(
'image' => 'http://example.com/img/partner1.jpg',
'link' => 'http://www.example1.com'
),
1 => array(
'image' => 'http://example.com/img/partner2.jpg',
'link' => 'http://www.example2.com'
),
2 => array(
'image' => 'http://example.com/img/partner3.jpg',
'link' => 'http://www.example3.com'
),
3 => array(
'image' => 'http://example.com/img/partner4.jpg',
'link' => 'http://www.example4.com'
)
);
$i = 0
foreach($images as $image)
{
$i++;
$zones[$i][] = $image;
if($i == 2)
$i = 0;
}
if(!empty($zones[$_GET['zone']]))
{
$zone = $zones[$_GET['zone']];
$random_index = array_rand($zone);
$partner = $zone[$random_index];
if($_GET['field'] == 'image')
{
$file = getFullPath($partner['image']);
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
}
elseif($_GET['field'] == 'link')
{
wp_redirect( $partner['link'], 301);
exit();
}
}
在我目前的情況下,(html)時事通訊模板中的圖像如下所示:
<a href="http://example.com/show_image.php?zone=1&field=link">
<img src="http://example.com/show_image.php?zone=1&field=image">
</a>
<a href="http://example.com/show_image.php?zone=2&field=link">
<img src="http://example.com/show_image.php?zone=2&field=image">
</a>
如您所見,隨機圖像和鏈接的調用是分開的,導致 php 腳本以與隨機圖像不匹配的隨機鏈接進行響應。
誰能指出我正確的方向如何隨機顯示具有正確相應鏈接的圖像?
首先,您的代碼中存在語法錯誤。 您所有的子數組都缺少逗號:
0 => array(
'image' => 'http://example.com/img/partner1.jpg' // <-- Error
'link' => 'http://www.example1.com'
)
應該:
0 => array(
'image' => 'http://example.com/img/partner1.jpg', // <-- Fixed
'link' => 'http://www.example1.com'
)
您應該使用rand()隨機獲取圖像:
$images = array(
0 => array(
'image' => 'http://example.com/img/partner1.jpg',
'link' => 'http://www.example1.com'
),
1 => array(
'image' => 'http://example.com/img/partner2.jpg',
'link' => 'http://www.example2.com'
),
2 => array(
'image' => 'http://example.com/img/partner3.jpg',
'link' => 'http://www.example3.com'
),
3 => array(
'image' => 'http://example.com/img/partner4.jpg',
'link' => 'http://www.example4.com'
)
);
$total_images = count($images) - 1; // Get total number of images. Deducted one because arrays are zero-based
$random_img = rand(0, $total_images); // Get a random number between 0 and $total_images
echo $images[$random_img]['image'] . '<br />';
echo $images[$random_img]['link'] . '<br />';
可能有多種解決方案,它們都有積極和消極的一面:
1.1. 您可以使用 iframe 加載圖像和鏈接表單 php 服務器
如果您必須使用靜態 html 和 javascript,您可以使用 javascript 對 php 執行 ajax 調用,這將再次獲取圖像和鏈接並使用它們生成 html 代碼(document.write 或 innerHTML)
您可以嘗試使用 cookie 或 session 機制,在這種情況下,在 php 代碼中,您將擁有分支,例如if number for zone is not generated yet - generate and store in cookie/session; return link or image for number from cookies/session
if number for zone is not generated yet - generate and store in cookie/session; return link or image for number from cookies/session
要修改 #3 的代碼,您需要替換
$random_index = array_rand($zone);
類似於(沒有實際的 php 編寫,因此可能會出現語法錯誤):
$cook = 'zone' . $_GET['zone'];
$random_index = isset($_COOKIE[$cook]) ? $_COOKIE[$cook] : array_rand($zone);
setcookie($cook, $random_index);
注意 - 由您對來自 GET 或 COOKIE 的任何變量進行適當的驗證
在電子郵件客戶端的情況下 - 他們中的大多數限制執行 javascript 代碼並且不存儲 cookie(從用戶的角度來看,他們這樣做非常好),無論如何你可以嘗試這樣的事情:
<a href="http://..../?...&id=UUID"><img src="http://.../?..&id=UUID"></a>
注意 - 一些電子郵件客戶端可以強制緩存圖像,忽略 http 標頭,因此這種方案將失敗
其他注意事項:
<noscript>
和一些隨機選擇的單個廣告的嵌入圖像
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.