Page 1 of 1

My loop is causing an error

Posted: Mon Feb 18, 2008 10:14 am
by micknc
I am trying to loop a page that handles sending email from our database. It basically bundles a set of invoices based on their customer id, sends them out as an attachment, then deletes them from a temp table that is used.

All works great when I run it manually. I am trying to loop it so that it will send the first batch and then second and so on but I am getting a class error. I know why I am getting the error but I am not sure about how to fix it.

The loop is very simple:

Code: Select all

 
<?
include 'dbconnect.php';
$queryivhead = "SELECT * FROM ivheadtemp order by CUST_ID ASC";
            $result=mysql_query($queryivhead);
            $rowivhead = mysql_fetch_assoc($result);
 
do {
            include 'runTestOfAttachment.php';
 
} while ($rowivhead['CUST_ID'] != "");
 
?>
The error is happening on the line that calls the class for runtestofattachment.php:

Code: Select all

 
$runner = new testofattachment();
 
Now, I know that is happening because it is trying to use it each time it loops but what can I do to solve the problem? I have thought about dynamically naming the class each time the loop runs but all of my syntax that I have tried fails. Should I try to close the connection in some way? I am stuck.
Any ideas?

Re: My loop is causing an error

Posted: Mon Feb 18, 2008 10:24 am
by John Cartwright
what is the error?

Re: My loop is causing an error

Posted: Mon Feb 18, 2008 10:27 am
by micknc
Sorry:
Fatal error: Cannot redeclare class testofattachment

Re: My loop is causing an error

Posted: Mon Feb 18, 2008 10:30 am
by John Cartwright
Wherever you include() the class definition, change that to include_once()

Re: My loop is causing an error

Posted: Mon Feb 18, 2008 10:52 am
by micknc
It was originally:

Code: Select all

class testofattachment extends Runner
 
I have tried:

Code: Select all

 
include_once ('class testofattachment extends Runner');
include_once "class testofattachment extends Runner";
 
 
but none work. I get a parse error. I read php.net on include once and that should work. What is wrong?

I hate doing it this way but here is the code. If you have swift mailer you will notice it as one of the tests. I haven't bothered changing anything until I get the error corrected.

Code: Select all

 
<?php
require_once dirname(__FILE__) . "/components/Runner.php";
 
class testofattachment extends Runner
{
  protected $to;
  protected $from;
 
 
 
  public function go()
  {
    try {
      Swift_ClassLoader::load("Swift_Cache_Disk");
      Swift_Cache_Disk::setSavePath(TestConfiguration::WRITABLE_PATH);
      Swift_CacheFactory::setClassName("Swift_Cache_Disk");
      $swift = new Swift($this->getConnection(), null, Swift::ENABLE_LOGGING);
      $this->setSwiftInstance($swift);
            ob_start(); 
            include("invoiceloop.php"); //
            $htmlbody = ob_get_contents(); // adding output to the variable
            ob_end_clean();
      $message = new Swift_Message("Smoke Test 3 - Attachment");
      $message->attach(new Swift_Message_Part("This message contains an attachment"));
      $message->attach(new Swift_Message_Part("This message contains an <em>attachment</em>", "text/html"));
      $message->attach(new Swift_Message_Attachment($htmlbody, "so.html", "application/html"));
      $to = new Swift_Address(TestConfiguration::TO_ADDRESS, TestConfiguration::TO_NAME);
      $from = new Swift_Address(TestConfiguration::FROM_ADDRESS, TestConfiguration::FROM_NAME);
      $swift->send($message, $to, $from);
      $this->to = $to->build();
      $this->from = $from->build();
    } catch (Exception $e) {
      $this->failed = true;
      $this->setError($e->getMessage());
    }
    $this->render();
  }
  
  public function paintTestName()
  {
    echo "Test of Sending Attachment";
  }
  
  public function paintTopInfo()
  {
    echo "An email containing a PDF attachment will be sent from Swift, to the account given in the test configuration.  Open up the email & its attachment and " .
    "check that the details given below are accurate: " .
    "<ul><li>The message body is<br />\"<em>This message contains an an attachment</em>\"</li>" .
    "<li>There is an attachment included named <em>Authors_CV.pdf</em></li>" .
    "<li>The attachment opens successfully in a PDF reader such as Adobe Acrobat Reader</li></ul>";
  }
  
  public function paintImageName()
  {
    echo "smoke3.png";
  }
}
$runner = new testofattachment()
$runner->go();
?>
The second to last line generates the error but I thought it was called at the top so that is where I tried the include once.
Thanks for taking a look

Re: My loop is causing an error

Posted: Mon Feb 18, 2008 10:58 am
by John Cartwright
You are creating multiple instances of the class definition, it is not that line actually causing the php parse error.

also, include_once()'s expects a filename, not the class name

include_once "/path/to/testofattachment.php";

Re: My loop is causing an error

Posted: Mon Feb 18, 2008 11:03 am
by micknc
So what do you think the answer is? How can I safely loop this page?

Re: My loop is causing an error

Posted: Mon Feb 18, 2008 11:16 am
by John Cartwright
I think the question is, why are you running the tests for each row?