簡體   English   中英

在PHP中使用Foreach語句

[英]Using Foreach Statement in PHP

我正在使用foreach但有問題。

以下一項工作正常。 但是它還不夠,我想再添加兩個數組。

foreach (array_combine($image, $link) as $pic => $url) {
<a href="http://www.onlinedealsindia.in/deal/' .$url. '"><img src="' .$pic. '" style="max-height:90px;max-width:90px"></a>
}

我想要滿足我需求的4個陣列。

foreach (array_combine($image, $link) as $pic => $url) {
<a href="http://www.onlinedealsindia.in/deal/' .$url. '"><img src="' .$pic. '" style="max-height:90px;max-width:90px"></a>

<p>" .$title. "</p>
<p>" .$title2. "</p>
}

現在我無法獲得“ title ”和“ title2 ”。


我已經嘗試了以下編碼,但無法正常工作。

foreach (array_combine($image, $link) as $pic => $url) {
foreach (array_combine($txt, $txt2) as $title => $title2) {
<a href="http://www.onlinedealsindia.in/deal/' .$url. '"><img src="' .$pic. '" style="max-height:90px;max-width:90px"></a>

<p>" .$title. "</p>
<p>" .$title2. "</p>
}
}

我已盡力而為,但一無所知。 如果有人給出正確答案並能幫助我,我將不勝感激。 我也有禮物送給他們。 謝謝


好吧,我用它來通訊:-

<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "**";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$id1 = $_POST["id1"];
$id2 = $_POST["id2"];
$id3 = $_POST["id3"];
$id4 = $_POST["id4"];
$id5 = $_POST["id5"];
$id6 = $_POST["id6"];
$id7 = $_POST["id7"];
$id8 = $_POST["id8"];
$id9 = $_POST["id9"];
$id10 = $_POST["id10"];

$sql = "SELECT * FROM deals where id in ($id1, $id2, $id3, $id4, $id5, $id6, $id7, $id8, $id9, $id10)";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {

$name[] = $row["title"];
$image[] = $row["image"];
$dp[] = $row["dealprice"];
$mrp[] = $row["mrp"];
$id[] = $row["slug"];

}}else {}
$conn->close();
?>

<?php if(isset($_POST['send'])){
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "**";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT email, subid FROM subscribers";
$result = $conn->query($sql);

if ($result->num_rows > 0) {while($row = $result->fetch_assoc()){

$subid = $row["subid"];
$subject = $_POST["subject"];

$email_to = $row["email"];
$subject = "Newsletter | OnlineDealsIndia";
$header = "From: OnlineDealsIndia <noreply@onlinedealsindia.in>\r\n"; 
$header.= "MIME-Version: 1.0\r\n"; 
$header.= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
$header.= "X-Priority: 1\r\n"; 

$message ='<table style="width:100%;background-color: #71CCBE;height: 80px">';
$message .='<td>';
$message .='<center><a href="http://www.onlinedealsindia.in"><img src="http://www.onlinedealsindia.in/images/Logo.png" width="190px" align="middle"></a></center>';
$message .='</td>';
$message .='</table>';
$message .='<table style="width:100%>';
$message .='<tbody style="width:100%; bgcolor="#FAFAFA">';

foreach (array_combine($image, $name) as $pic => $title) {
$message .='<table style="border-bottom:2px solid #ebebeb" align="center" bgcolor="#ffffff" border="0" cellpadding="0" cellspacing="5" width="100%">';
$message .='<tbody>';
$message .='<tr>';
$message .='<td style="padding:10px" align="center" width="20%">';
$message .='<a href="http://www.onlinedealsindia.in/deal/' .$id. '"><img src="' .$pic. '" style="max-height:90px;max-width:90px"></a>';
$message .='</td>';
$message .='<td style="padding:10px" align="center" width="60%">';
$message .='<a href="http://www.onlinedealsindia.in/deal-' .$id. '" style="font-size:14px;color:#333333;text-decoration:none;line-height:20px" target="_blank">' .$title. '</a>';
$message .='</td>';
$message .='<td style="padding:10px" align="center" width="60%">';
$message .='</td>';
$message .='</tr>';
$message .='</tbody>';
$message .='</table>';
}

$message .='</tbody>';
$message .='<a href="http://www.onlinedealsindia.in/newsletter/unsubscribe/?subid=' .$subid. '&action=unsubscribe" class="btn">Click Here</a> To Unsubscribe';

mail($email_to,$subject,$message,$header);
$msg = '<div class="alert alert-success" style="margin:10px"><b>Success!</b> Newsletter Sent!</div>';
}}else {$msg = '<div class="alert alert-danger" style="margin:10px"><b>Error!</b></div>';}

$conn->close();
}
?>

目前在上面的代碼中,我正在使用2-3個數組,但是我將添加其中,這就是為什么需要幫助的原因。

如果我理解正確,您有四個具有對應數據的數組,是否需要遍歷它們?

實現此目的的最佳方法是使用for循環並使用索引獲取數據。

for ($i = 0; $i < count($image); $i++) {
    echo '<a href="http://www.onlinedealsindia.in/deal/' . $link[$i] . '"><img src="' . $image[$i] . '" style="max-height:90px;max-width:90px"></a>';

    echo '<p>"' . $txt[$i] . '"</p>';
    echo '<p>"' . $txt2[$i] . '"</p>';
}

考慮您有4個具有相同元素數量的數組

$ image_src,$ url,$ title,$ title2。 在這里,您可以將所有這些數組首先組合成單個數組,然后可以遍歷它進行顯示,如下所示:

 for($i=0; $i<count($image_src); $i++) {
   $image[$i]['src'] = $image_src[$i];
   $image[$i]['url'] = $url[$i];
   $image[$i]['title'] = $title[$i];
   $image[$i]['title2'] = $title2[$i];
}

現在,您可以使用$ image數組,並且可以像

foreach($image as $img) {<a href="http://www.onlinedealsindia.in/deal/' .$img['url']. '"><img src="' .$img['src']. '" style="max-height:90px;max-width:90px"></a><p>" .$img['title']. "</p><p>" .$img['title2']. "</p>}

希望它對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM