在PHP中使用Foreach语句

时间:2016-05-20 08:51:27

标签: php foreach

我正在使用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>
}

现在我无法获得&#34; 标题&#34;和&#34; title2 &#34;。

我已尝试过以下编码,但它不起作用。

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个数组,但我会添加它,这就是为什么需要帮助。

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你有四个具有相应数据的数组,你需要循环它们吗?

实现这一目标的最佳方法是使用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>';
}

答案 1 :(得分:0)

考虑您有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>}

希望它适合你。