unexpected T_IF error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • keley
    New Member
    • Jul 2007
    • 4

    unexpected T_IF error

    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.

    Code:
    ?php require_once('Connections/jma.php'); ?>
    <?php require_once("Connections/dbConnection.php"); ?>
    <?php
    $date = date('Y-m-d');
    $days_to_add = 7;
    $dateArray = explode("-",$date);
    $startTimestamp = mktime(0,0,0,$dateArray[1],$dateArray[2],$dateArray[0]);
    $endTimestamp = mktime(23,59,0,$dateArray[1],$dateArray[2]+$days_to_add,$dateArray[0]);
    // Create SQL to read in database records
    $sql  = "SELECT id,title,body, UNIX_TIMESTAMP(dateField) AS timestampField  ";
    $sql .= "FROM calendardata ";
    $sql .= "WHERE UNIX_TIMESTAMP(dateField) >= " . $startTimestamp . " AND UNIX_TIMESTAMP(dateField) <= " . $endTimestamp . " ";
    $sql .= "ORDER BY UNIX_TIMESTAMP(dateField) ASC";
    
    // Open Database Connection	
    $dbLink = mysql_connect($dbHost, $dbUser, $dbPass);
    if (!$dbLink){
      die ("Database: Couldn`t connect to mySQL Server");
    }
    mysql_select_db($dbName, $dbLink) 
      or die ("Database: Couldn`t open Database"); 
    
    // Read in Records from Database  
    $dbResult = mysql_query($sql, $dbLink)
      or die ("MySQL Error: " . mysql_error() );
    $numRecords = mysql_num_rows($dbResult);
    for($i=0;$i < $numRecords;$i++){
    	$temp = mysql_fetch_assoc($dbResult);
    	if (!get_magic_quotes_gpc()) {
    		$temp['title'] = stripslashes($temp['title']);
    		$temp['body'] = stripslashes($temp['body']);
    	}
    	$records[] = $temp;
    }
        
    // Close Database Connection  
    mysql_close($dbLink);
    ?>
    <?php 
    
    // Grab our config settings 
    require_once($_SERVER['DOCUMENT_ROOT'].'/config.php'); 
    
    // Grab the FreakMailer class 
    require_once($_SERVER['DOCUMENT_ROOT'].'/lib/MailClass.inc'); 
    
    // instantiate the class 
    $mailer = new FreakMailer(); 
    
    // Set the subject 
    $mailer->Subject = 'This is a test'; 
    
    // Body 
    $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> ';
    $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 .=  }; 
    $mailer->isHTML(true); 
    
    // Add an address to send to. 
    $mailer->AddAddress('keley@johnmilledge.org', 'Kendall Eley'); 
    
    if(!$mailer->Send()) 
    { 
      echo 'There was a problem sending this mail!'; 
    } 
    else 
    { 
      echo 'Mail sent!'; 
    } 
    $mailer->ClearAddresses(); 
    $mailer->ClearAttachments(); 
    ?>
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    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($record s 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.

    Comment

    • keley
      New Member
      • Jul 2007
      • 4

      #3
      Originally posted by mwasif
      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($record s 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.

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

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #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?

        Comment

        • keley
          New Member
          • Jul 2007
          • 4

          #5
          Originally posted by mwasif
          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.

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

          Comment

          • mwasif
            Recognized Expert Contributor
            • Jul 2006
            • 802

            #6
            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:l stephens@johnmi lledge.org\">Li nda 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($record s 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($record s as $record);[/PHP]
            This is how if else works

            Comment

            • keley
              New Member
              • Jul 2007
              • 4

              #7
              Thanks. I am slow, but I finally realized what you are talking about. I have it working fine. Thanks for the help.

              Comment

              • mwasif
                Recognized Expert Contributor
                • Jul 2006
                • 802

                #8
                Originally posted by keley
                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!

                Comment

                Working...