By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

unexpected T_IF error

P: 4
I am trying to use a script to send a list of items from a database. The code is listed below. I get an unexpected T_IF error on the second $mailer->Body line (line 55). If I comment out the 9 $mailer->Body lines with php code (55-62 and 64), the script works correctly.

I am relatively new to coding, so I am sure I have left something out, I just do not know what. Any help would be appreciated.

Expand|Select|Wrap|Line Numbers
  1. ?php require_once('Connections/jma.php'); ?>
  2. <?php require_once("Connections/dbConnection.php"); ?>
  3. <?php
  4. $date = date('Y-m-d');
  5. $days_to_add = 7;
  6. $dateArray = explode("-",$date);
  7. $startTimestamp = mktime(0,0,0,$dateArray[1],$dateArray[2],$dateArray[0]);
  8. $endTimestamp = mktime(23,59,0,$dateArray[1],$dateArray[2]+$days_to_add,$dateArray[0]);
  9. // Create SQL to read in database records
  10. $sql  = "SELECT id,title,body, UNIX_TIMESTAMP(dateField) AS timestampField  ";
  11. $sql .= "FROM calendardata ";
  12. $sql .= "WHERE UNIX_TIMESTAMP(dateField) >= " . $startTimestamp . " AND UNIX_TIMESTAMP(dateField) <= " . $endTimestamp . " ";
  13. $sql .= "ORDER BY UNIX_TIMESTAMP(dateField) ASC";
  14.  
  15. // Open Database Connection    
  16. $dbLink = mysql_connect($dbHost, $dbUser, $dbPass);
  17. if (!$dbLink){
  18.   die ("Database: Couldn`t connect to mySQL Server");
  19. }
  20. mysql_select_db($dbName, $dbLink) 
  21.   or die ("Database: Couldn`t open Database"); 
  22.  
  23. // Read in Records from Database  
  24. $dbResult = mysql_query($sql, $dbLink)
  25.   or die ("MySQL Error: " . mysql_error() );
  26. $numRecords = mysql_num_rows($dbResult);
  27. for($i=0;$i < $numRecords;$i++){
  28.     $temp = mysql_fetch_assoc($dbResult);
  29.     if (!get_magic_quotes_gpc()) {
  30.         $temp['title'] = stripslashes($temp['title']);
  31.         $temp['body'] = stripslashes($temp['body']);
  32.     }
  33.     $records[] = $temp;
  34. }
  35.  
  36. // Close Database Connection  
  37. mysql_close($dbLink);
  38. ?>
  39. <?php 
  40.  
  41. // Grab our config settings 
  42. require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); 
  43.  
  44. // Grab the FreakMailer class 
  45. require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc'); 
  46.  
  47. // instantiate the class 
  48. $mailer = new FreakMailer(); 
  49.  
  50. // Set the subject 
  51. $mailer->Subject = 'This is a test'; 
  52.  
  53. // Body 
  54. $mailer->Body = '<p>This is a list of all events (field trips, guest speakers, assemblies, etc) occurring at John Milledge in the next seven days. Any events that will affect the normal school day should be listed here. If you know of an event that should be listed, please email the event to <a href=\"mailto:lstephens@johnmilledge.org\">Linda Stephens</a>. Please keep in mind that all of these events do not appear on the John Milledge website.</p><p><br> ';
  55. $mailer->Body .=  if ($numRecords <> 0) { ;
  56. $mailer->Body .= foreach($records as $record);
  57. $mailer->Body .= { $time=date("h:i a", $record['timestampField']) ;
  58. $mailer->Body .= if ($time=="12:00 pm"){ echo date("l m-d", $record['timestampField'])} ;
  59. $mailer->Body .= else {echo date("l m-d h:i a", $record['timestampField'])} echo $record['title'] ;
  60. $mailer->Body .= if($record['title']==$record['body']) {""};
  61. $mailer->Body .= else { echo $record['body']} . '</p><hr> ' .  } } ;
  62. $mailer->Body .= if ($numRecords == 0) {  ;
  63. $mailer->Body .= '</p> There are no events planned in the next seven days.';
  64. $mailer->Body .=  }; 
  65. $mailer->isHTML(true); 
  66.  
  67. // Add an address to send to. 
  68. $mailer->AddAddress('keley@johnmilledge.org', 'Kendall Eley'); 
  69.  
  70. if(!$mailer->Send()) 
  71.   echo 'There was a problem sending this mail!'; 
  72. else 
  73.   echo 'Mail sent!'; 
  74. $mailer->ClearAddresses(); 
  75. $mailer->ClearAttachments(); 
  76. ?> 
  77.  
Jul 25 '07 #1
Share this Question
Share on Google+
7 Replies


mwasif
Expert 100+
P: 801
What do you want to do in the following code (line 55 to 64)? The problem lies in these line

[PHP]$mailer->Body .= if ($numRecords <> 0) { ;
$mailer->Body .= foreach($records as $record);
$mailer->Body .= { $time=date("h:i a", $record['timestampField']) ;
$mailer->Body .= if ($time=="12:00 pm"){ echo date("l m-d", $record['timestampField'])} ;
$mailer->Body .= else {echo date("l m-d h:i a", $record['timestampField'])} echo $record['title'] ;
$mailer->Body .= if($record['title']==$record['body']) {""};
$mailer->Body .= else { echo $record['body']} . '</p><hr> ' . } } ;
$mailer->Body .= if ($numRecords == 0) { ;
$mailer->Body .= '</p> There are no events planned in the next seven days.';
$mailer->Body .= }; [/PHP]

Take a look at this to learn how to concatenate string in PHP.
Jul 25 '07 #2

P: 4
What do you want to do in the following code (line 55 to 64)? The problem lies in these line

[PHP]$mailer->Body .= if ($numRecords <> 0) { ;
$mailer->Body .= foreach($records as $record);
$mailer->Body .= { $time=date("h:i a", $record['timestampField']) ;
$mailer->Body .= if ($time=="12:00 pm"){ echo date("l m-d", $record['timestampField'])} ;
$mailer->Body .= else {echo date("l m-d h:i a", $record['timestampField'])} echo $record['title'] ;
$mailer->Body .= if($record['title']==$record['body']) {""};
$mailer->Body .= else { echo $record['body']} . '</p><hr> ' . } } ;
$mailer->Body .= if ($numRecords == 0) { ;
$mailer->Body .= '</p> There are no events planned in the next seven days.';
$mailer->Body .= }; [/PHP]

Take a look at this to learn how to concatenate string in PHP.
The code I list below is where I got the idea for the code above. The code below outputs a list of dates and events to a webpage. If there are no events, the code outputs There are no events planned in the next seven days.

What I want is to email this list daily to a specific email address. I thought that this script would be the best way to do that.

Expand|Select|Wrap|Line Numbers
  1. <?php if ($numRecords <> 0) { // Show if recordset not empty ?>
  2. <?php foreach($records as $record){ ?>
  3.           <?php $time=date("h:i a", $record['timestampField']); 
  4.                               if ($time=="12:00 pm"){ echo date("l m-d", $record['timestampField']);} else {echo date("l m-d h:i a", $record['timestampField']);}?>
  5. &nbsp;          <?php echo $record['title']; ?>
  6.           <?php if($record['title']==$record['body']) {"";}else { echo $record['body'];} ?>
  7.  </p>
  8.  <hr>
  9.         <?php } ?>
  10. <?php } // Show if recordset not empty ?>
  11.         <?php if ($numRecords == 0) { // Show if recordset empty ?>
  12. </p>
  13. There are no events planned in the next seven days.
  14. <?php } // Show if recordset empty ?>
Jul 25 '07 #3

mwasif
Expert 100+
P: 801
In your code, you are trying to display and email the content at the same time. First assign the content to a variable and then use that variable to email and display the message.

Did you read my provided link?
Jul 25 '07 #4

P: 4
In your code, you are trying to display and email the content at the same time. First assign the content to a variable and then use that variable to email and display the message.

Did you read my provided link?
I read the link and I have amended the code as follows but I get the same result. What am I missing? I am sure it is obvious, I am just not smart enough to see it.

Expand|Select|Wrap|Line Numbers
  1. $content = ' <p>This is a list of all events (field trips, guest speakers, assemblies, etc) occurring at John Milledge in the next seven days. Any events that will affect    the normal school day should be listed here. If you know of an event that should be listed, please email the event to <a href=\"mailto:lstephens@johnmilledge.org\">Linda Stephens</a>. Please keep in mind that all of these events do not appear on the John Milledge website.</p>  <p><br> ' ;
  2. $content .=  if ($numRecords <> 0) { ;
  3. $content .= foreach($records as $record);
  4. $content .= { $time=date("h:i a", $record['timestampField']) ;
  5. $content .= if ($time=="12:00 pm"){ echo date("l m-d", $record['timestampField'])} ;
  6. $content .= else {echo date("l m-d h:i a", $record['timestampField'])} echo $record['title'] ;
  7. $content .= if($record['title']==$record['body']) {""};
  8. $content .= else { echo $record['body']} . '</p><hr> ' .  } } ;
  9. $content .= if ($numRecords == 0) {  ;
  10. $content .= '</p> There are no events planned in the next seven days.';
  11. $content .=  };
  12.  
  13. $mailer->Body = $content;
  14.  
Jul 25 '07 #5

mwasif
Expert 100+
P: 801
The syntax will be something like

[PHP]$content = ' <p>This is a list of all events (field trips, guest speakers, assemblies, etc) occurring at John Milledge in the next seven days. Any events that will affect the normal school day should be listed here. If you know of an event that should be listed, please email the event to <a href=\"mailto:lstephens@johnmilledge.org\">Linda Stephens</a>. Please keep in mind that all of these events do not appear on the John Milledge website.</p> <p><br> ' ;
if($numRecords <> 0)
{
foreach($records as $record)
{
$time=date("h:i a", $record['timestampField']) ;
if ($time=="12:00 pm")
{
$content.= date("l m-d", $record['timestampField']);
// rest of the code
}
// some code here
}
}[/PHP]
This is invalid code
[PHP]$mailer->Body .= if ($numRecords <> 0) { ;
$mailer->Body .= foreach($records as $record);[/PHP]
This is how if else works
Jul 25 '07 #6

P: 4
Thanks. I am slow, but I finally realized what you are talking about. I have it working fine. Thanks for the help.
Jul 25 '07 #7

mwasif
Expert 100+
P: 801
Thanks. I am slow, but I finally realized what you are talking about. I have it working fine. Thanks for the help.
No problem, post back whenever you need help!
Jul 25 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.