Jump to content

[SOLVED] Unexpected T_FOREACH


40esp

Recommended Posts

Im getting an unexpcted error in my string, and was wondering if I can get some help.

 

 <?php 
echo '<td align="center" valign="middle" nowrap="nowrap" class="border_2">    [email protected]   <br />
    <br />
    Newsletters Subscribed To:<br />'.foreach($newsletter2 as $key2=>$title2){ if (strstr($is_subscribed2, "[$key2]")){ echo $title2."<br />";}}.'</tr>
  <tr valign="baseline">
    <td align="right" valign="middle" nowrap="nowrap" bgcolor="#000000"> </td>' ?>
  </tr>
</table>

 

It must be the way I have it set up.

Link to comment
https://forums.phpfreaks.com/topic/113791-solved-unexpected-t_foreach/
Share on other sites

You can't concatenate foreach on your echo, as foreach doesn't return value.

You should separate your command, like so:

 

echo '<td align="center" valign="middle" nowrap="nowrap" class="border_2">    [email protected]   <br /><br />Newsletters Subscribed To:<br />';

foreach($newsletter2 as $key2=>$title2){ 
  if (strstr($is_subscribed2, "[$key2]")) { 
    echo $title2."<br />";
  }
}

echo '</tr><tr valign="baseline"><td align="right" valign="middle" nowrap="nowrap" bgcolor="#000000"> </td>';

I think it might serve my frustration better to post the whole code, so you guys can see what I'm trying to achieve:

 

<?php
if ((isset($_POST['add_email']) && ($_POST['add_email'] <> ""))) {  
$is_subscribed2 = $row_topics2['title'];
$to = "[email protected]";
$subject = "Newsletter Subscription Notification";
$message = ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.border_2 {border-top: hidden;
border-right: hidden;
border-left: hidden;
border-bottom: 1px solid #000000; }
.border_3 {border: 1px solid #000000; }
.style1 {
color: #FFFFFF;
font-weight: bold;
}
-->
</style></head>

<body>
<div align="center">This is a reminder that a new subscription has been added to the fgdfgdg website. The sign-up email address is displayed below. To view the newsletters this person signed up for, please follow this link.<br />
  <br />
</div>
<table align="center" cellspacing="0" bgcolor="#f5f5f5" class="border_3">
  <tr valign="baseline">
    <td width="329" height="22" align="left" valign="middle" nowrap="nowrap" bgcolor="#7b2114"><span class="style1">Sign-up Information</span></td>
  </tr>
  <tr valign="baseline">
    <td align="center" valign="middle" nowrap="nowrap" class="border_2">    [email protected]   <br />
    <br />
    Newsletters Subscribed To:<br />'.foreach($newsletter2 as $key2=>$title2){ 
  if (strstr($is_subscribed2, "[$key2]")) { 
    echo $title2."<br />";
  }
}.'</tr>
  <tr valign="baseline">
    <td align="right" valign="middle" nowrap="nowrap" bgcolor="#000000"> </td>
  </tr>
</table>
</body>
</html>
');
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 'From: [email protected]' . "\r\n" . 'MIME-Version: 1.0' . "\r\n";
mail($to, $subject, $message, $headers);
}
?>

So you have to concatenate.

 

<?php
$message = ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.border_2 {border-top: hidden;
border-right: hidden;
border-left: hidden;
border-bottom: 1px solid #000000; }
.border_3 {border: 1px solid #000000; }
.style1 {
color: #FFFFFF;
font-weight: bold;
}
-->
</style></head>

<body>
<div align="center">This is a reminder that a new subscription has been added to the fgdfgdg website. The sign-up email address is displayed below. To view the newsletters this person signed up for, please follow this link.<br />
  <br />
</div>
<table align="center" cellspacing="0" bgcolor="#f5f5f5" class="border_3">
  <tr valign="baseline">
    <td width="329" height="22" align="left" valign="middle" nowrap="nowrap" bgcolor="#7b2114"><span class="style1">Sign-up Information</span></td>
  </tr>
  <tr valign="baseline">
    <td align="center" valign="middle" nowrap="nowrap" class="border_2">    [email protected]   <br />
    <br />
    Newsletters Subscribed To:<br />';

foreach($newsletter2 as $key2=>$title2){ 
  if (strstr($is_subscribed2, "[$key2]")) { 
    $message .= $title2."<br />";
  }
}

$message .= '</tr>
  <tr valign="baseline">
    <td align="right" valign="middle" nowrap="nowrap" bgcolor="#000000"> </td>
  </tr>
</table>
</body>
</html>
');
?>

So you have to concatenate.

 

<?php
$message = ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.border_2 {border-top: hidden;
border-right: hidden;
border-left: hidden;
border-bottom: 1px solid #000000; }
.border_3 {border: 1px solid #000000; }
.style1 {
color: #FFFFFF;
font-weight: bold;
}
-->
</style></head>

<body>
<div align="center">This is a reminder that a new subscription has been added to the fgdfgdg website. The sign-up email address is displayed below. To view the newsletters this person signed up for, please follow this link.<br />
  <br />
</div>
<table align="center" cellspacing="0" bgcolor="#f5f5f5" class="border_3">
  <tr valign="baseline">
    <td width="329" height="22" align="left" valign="middle" nowrap="nowrap" bgcolor="#7b2114"><span class="style1">Sign-up Information</span></td>
  </tr>
  <tr valign="baseline">
    <td align="center" valign="middle" nowrap="nowrap" class="border_2">    [email protected]   <br />
    <br />
    Newsletters Subscribed To:<br />';

foreach($newsletter2 as $key2=>$title2){ 
  if (strstr($is_subscribed2, "[$key2]")) { 
    $message .= $title2."<br />";
  }
}

$message .= '</tr>
  <tr valign="baseline">
    <td align="right" valign="middle" nowrap="nowrap" bgcolor="#000000"> </td>
  </tr>
</table>
</body>
</html>
');
?>

 

Wow, I just learned something new... lol. Thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.