php-general Digest 2 May 2008 21:15:37 -0000 Issue 5437

Topics (messages 273801 through 273817):

Re: problem with for loop
        273801 by: Peter Ford
        273802 by: Craige Leeder

Any Running Simple Ajax Sample for Php
        273803 by: Craige Leeder
        273816 by: Jon L.

transfer list in textarea to comma delimited string
        273804 by: afan pasalic
        273805 by: Stut
        273806 by: afan pasalic

set_error_handler help
        273807 by: Thiago Pojda
        273808 by: Richard Heyes
        273810 by: Craige Leeder

Assigning functions
        273809 by: Philip Thompson
        273811 by: Craige Leeder
        273812 by: Nathan Nobbe
        273813 by: Craige Leeder
        273814 by: Nathan Nobbe
        273815 by: Nathan Nobbe
        273817 by: David Otton

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
Richard Kurth wrote:
Way does my for loop not complete the task if there are 4 emails it only process 3 emails through the foreach loop if there is 3 it only process 2

    #  Connect up
    $host ="domain.com";
    $port ="110";
    $mailtype = "pop3";
    $mailbox ="INBOX";
    $username ="[EMAIL PROTECTED]";
    $password ="boat1234";
$conn = @imap_open("{" . $host . ":" . $port . "/" . $mailtype . "/notls}" . $mailbox, $username, $password);

$number=imap_num_msg($conn);
for($i = 1; $i <= $number; $i++) {
$file="C:\web\bouncehandler\eml\em$i";
imap_savebody($conn,$file,$i);


$file=file_get_contents("C:\web\bouncehandler\eml\em$i");
$multiArray = Bouncehandler::get_the_facts($file);

$EMAIL = $the['recipient'];
foreach($multiArray as $the){
       switch($the['action']){
       case 'failed':
$sql="UPDATE contacts SET emailstatus = 'Fatal-Bounced' WHERE emailaddress = '$EMAIL'"; mysql_query($sql) or die("Invalid query: " . mysql_error()); break;
       case 'transient':
$sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE emailaddress = '$EMAIL'";
       mysql_query($sql) or die("Invalid query: " . mysql_error());
       break;
       case 'autoreply':
$sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE emailaddress = '$EMAIL'";
       mysql_query($sql) or die("Invalid query: " . mysql_error());
       break;
       default:
       //don't do anything
       break;
       }
   }

}



I think you need to check the boundary conditions on your loop.
As you write it,

        for($i = 1; $i <= $number; $i++)

if $number is 4 then $i will have the values 1,2,3,4.

Perhaps message list is zero-based, and you actually need to count from zero:

        for($i = 0; $i < $number; $i++)

so you would get $i to read 0,1,2,3

The manual page doesn't explicitly say the the message number is one-based, and most real programming languages these days use zero-based arrays...

--
Peter Ford                              phone: 01580 893333
Developer                               fax:   01580 893399
Justcroft International Ltd., Staplehurst, Kent

--- End Message ---
--- Begin Message ---
I think Peter is probably right. In the case he is not however, can
 you post a print_r of $multiArray.

 - Craige



 On Fri, May 2, 2008 at 7:24 AM, Peter Ford <[EMAIL PROTECTED]> wrote:
 >
 > Richard Kurth wrote:
 >
 > > Way does my for loop not complete the task if there are 4 emails it only
 > process 3 emails through the foreach loop if there is 3 it only process 2
 > >
 > >    #  Connect up
 > >    $host ="domain.com";
 > >    $port ="110";
 > >    $mailtype = "pop3";
 > >    $mailbox ="INBOX";
 > >    $username ="[EMAIL PROTECTED]";
 > >    $password ="boat1234";
 > >       $conn = @imap_open("{" . $host . ":" . $port . "/" . $mailtype .
 > "/notls}" . $mailbox, $username, $password);
 > >
 > > $number=imap_num_msg($conn);
 > > for($i = 1; $i <= $number; $i++) {
 > > $file="C:\web\bouncehandler\eml\em$i";
 > > imap_savebody($conn,$file,$i);
 > >
 > >
 > > $file=file_get_contents("C:\web\bouncehandler\eml\em$i");
 > > $multiArray = Bouncehandler::get_the_facts($file);
 > >
 > > $EMAIL = $the['recipient'];
 > > foreach($multiArray as $the){
 > >       switch($the['action']){
 > >       case 'failed':
 > >       $sql="UPDATE contacts SET emailstatus = 'Fatal-Bounced' WHERE
 > emailaddress = '$EMAIL'";
 > >       mysql_query($sql) or die("Invalid query: " . mysql_error());
 > break;
 > >       case 'transient':
 > >       $sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE emailaddress
 > = '$EMAIL'";
 > >       mysql_query($sql) or die("Invalid query: " . mysql_error());
 > >       break;
 > >       case 'autoreply':
 > >       $sql="UPDATE contacts SET emailstatus = 'Bounced' WHERE emailaddress
 > = '$EMAIL'";
 > >       mysql_query($sql) or die("Invalid query: " . mysql_error());
 > >       break;
 > >       default:
 > >       //don't do anything
 > >       break;
 > >       }
 > >   }
 > >
 > > }
 > >
 > >
 > >
 >
 >  I think you need to check the boundary conditions on your loop.
 >  As you write it,
 >
 >
 >         for($i = 1; $i <= $number; $i++)
 >
 >  if $number is 4 then $i will have the values 1,2,3,4.
 >
 >  Perhaps message list is zero-based, and you actually need to count from
 > zero:
 >
 >         for($i = 0; $i < $number; $i++)
 >
 >  so you would get $i to read 0,1,2,3
 >
 >  The manual page doesn't explicitly say the the message number is one-based,
 > and most real programming languages these days use zero-based arrays...
 >
 >  --
 >  Peter Ford                              phone: 01580 893333
 >  Developer                               fax:   01580 893399
 >  Justcroft International Ltd., Staplehurst, Kent
 >
 >
 >
 >  --
 >  PHP General Mailing List (http://www.php.net/)
 >  To unsubscribe, visit: http://www.php.net/unsub.php
 >
 >

--- End Message ---
--- Begin Message ---
Hi Heysem,

 So what you want is an ajax script that will call a php page every
 minute, and check for a result (assuming boolean) of 1 or 0? That
 shouldn't be too hard. First, I need to know more information about
 the page, such as the id tag of the element you want updated, and how
 severe the change. If it's a simple word, it's easy to do.

 If you could post more information (and possible what you have tried
 already, and maybe we can just fix that), I should be able to help you
 out more.

 Regards,
 - Craige



 On Fri, May 2, 2008 at 5:13 AM, Heysem KAYA
<[EMAIL PROTECTED]> wrote:
 > Hi,
 >
 >  I would like to check each minute whether the user credit is finished and
 >  update the relevant place on page. So I have downloaded however was not able
 >  to run some sample ajax code.
 >
 >  Is there anyone who has such a simple code to implement?
 >
 >
 >
 >  Thanks,
 >
 >
 >
 >  Heysem Kaya
 >
 >

--- End Message ---
--- Begin Message ---
If you aren't already, I recommend putting to use a JS library with Ajax
support.
Nothing else, they can reduce the browser compatibility overhead coding
you'll need to do.

You can pick any of many, but I'm more familiar with Prototype:
http://prototypejs.org/

## credits.php
--------------------
<?php
/* ... */  // determine value for $credits
print min($credits, 0);
?>

## credits.html
---------------------
<html>
  <body>
    <div id="credits">Checking credits...</div>

    <script src="prototype.js"></script>
    <script type="text/javascript">
    /*** Reference ***/
    //// http://prototypejs.org/api/periodicalExecuter
    //// http://prototypejs.org/api/ajax/request
    //// http://prototypejs.org/api/element/observe

    function creditsCheck(pe) {
      new Ajax.Request('foobar.php', {
        method: 'get',
        parameters: {ts: (new Date()).getTime()}, // cache prevention
        onSuccess: function (transport) {
          var elem = $('credits');
          if (Number(transport.responseText) > 0) {
            elem.update('Credits remaining: ' + transport.responseText);
          } else {
            elem.update('Credits have expired.').setStyle({background:
'#f00'});
            pe.stop();
          }
        },
        onFailure: function () {
          $('credits').update('Could not determine
credits.').setStyle({background: '#f00'});
        }
      });
    }

    document.observe('dom:loaded', function () {
      var pe = new PeriodicalExecuter(creditsCheck, 60);
      creditsCheck(pe); // first check
    });
    </script>
  </body>
</html>

- Jon L.

On Fri, May 2, 2008 at 4:13 AM, Heysem KAYA <[EMAIL PROTECTED]>
wrote:

> Hi,
>
> I would like to check each minute whether the user credit is finished and
> update the relevant place on page. So I have downloaded however was not
> able
> to run some sample ajax code.
>
> Is there anyone who has such a simple code to implement?
>
>
>
> Thanks,
>
>
>
> Heysem Kaya
>
>

--- End Message ---
--- Begin Message ---
hi,
I have one textarea field in a registration form where visitor enters
keywords. even there is a not next to the field "please enter keywords
as comma delimited string", they enter as  a list, below each other.

I tried to convert the list into comma delimited string with several
solutions but non of them works:
$keywords = eregi_replace('\n', '.', $keywords);
$keywords = eregi_replace('\r', '.', $keywords);
$keywords = eregi_replace('\n\r', '.', $keywords);
$keywords = eregi_replace('<br>', '.', $keywords);

any help here?

thanks

-afan


--- End Message ---
--- Begin Message ---
On 2 May 2008, at 16:22, afan pasalic wrote:
I have one textarea field in a registration form where visitor enters
keywords. even there is a not next to the field "please enter keywords
as comma delimited string", they enter as  a list, below each other.

I tried to convert the list into comma delimited string with several
solutions but non of them works:
$keywords = eregi_replace('\n', '.', $keywords);
$keywords = eregi_replace('\r', '.', $keywords);
$keywords = eregi_replace('\n\r', '.', $keywords);
$keywords = eregi_replace('<br>', '.', $keywords);

any help here?

1) str_replace is more than capable of doing this in a single statement - a regex is overkill.

2) You need to use double quotes around escape sequences (\n and \r) or they'll be ignored.

$replacements = array("\r", "\n", "\n\r", '<br>');
$keywords = str_replace($replacements, '.', $keywords);

-Stut

--
http://stut.net/

--- End Message ---
--- Begin Message ---
Stut wrote:
> On 2 May 2008, at 16:22, afan pasalic wrote:
>> I have one textarea field in a registration form where visitor enters
>> keywords. even there is a not next to the field "please enter keywords
>> as comma delimited string", they enter as  a list, below each other.
>>
>> I tried to convert the list into comma delimited string with several
>> solutions but non of them works:
>> $keywords = eregi_replace('\n', '.', $keywords);
>> $keywords = eregi_replace('\r', '.', $keywords);
>> $keywords = eregi_replace('\n\r', '.', $keywords);
>> $keywords = eregi_replace('<br>', '.', $keywords);
>>
>> any help here?
>
> 1) str_replace is more than capable of doing this in a single
> statement - a regex is overkill.
>
> 2) You need to use double quotes around escape sequences (\n and \r)
> or they'll be ignored.
>
> $replacements = array("\r", "\n", "\n\r", '<br>');
> $keywords = str_replace($replacements, '.', $keywords);
>
> -Stut
>

works like a charm!
:D

thanks stut!

-afan

--- End Message ---
--- Begin Message ---
Hi guys,

 

I'm trying some custom error handling functions in order to get emails when
fatal errors come up in my production website. The thing is, I went to
phpclasses and there was a good one, but it was a class.

 

I'm very slow today, so I could not figure out how to get it working. So, I
just removed it from the classs and used the function part. Which worked,
but it got me confused:

 

Is there any way to use a class to handle errors? I've tried some stuff like
set_error_handler("Error_Handler::logError" and such, but with no luck.

 

 

Any hints?

 

Thanks! 


--- End Message ---
--- Begin Message ---
Is there any way to use a class to handle errors? I've tried some stuff like
set_error_handler("Error_Handler::logError" and such, but with no luck.

It accepts a "callback type, which is a pseudo type. Basically an array containg the object and the method to use. Eg.

$obj = new ErrorHandlingObject();
set_error_handler(array($obj, 'myMethod'));

--
Richard Heyes

+----------------------------------------+
| Access SSH with a Windows mapped drive |
|    http://www.phpguru.org/sftpdrive    |
+----------------------------------------+

--- End Message ---
--- Begin Message ---
I beleive you can also do

set_error_handler(array('classname', 'myMethod'));

for static methods.

- Craige
On Fri, May 2, 2008 at 2:26 PM, Richard Heyes <[EMAIL PROTECTED]> wrote:
>
> > Is there any way to use a class to handle errors? I've tried some stuff
> like
> > set_error_handler("Error_Handler::logError" and such, but with no luck.
> >
>
>  It accepts a "callback type, which is a pseudo type. Basically an array
> containg the object and the method to use. Eg.
>
>  $obj = new ErrorHandlingObject();
>  set_error_handler(array($obj, 'myMethod'));
>
>  --
>  Richard Heyes
>
>  +----------------------------------------+
>  | Access SSH with a Windows mapped drive |
>  |    http://www.phpguru.org/sftpdrive    |
>  +----------------------------------------+
>
>  --
>  PHP General Mailing List (http://www.php.net/)
>  To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message --- Hi all. I have several classes. Within each class, a new class is called. Is there a way to assign a function in a *deeper* class to be called in the first class? Example to follow......

<?php
class A {
    function __construct () {
        $this->b = new B ();
        // I want to do the following. This does not work, of course.
        $this->doSomething = $this->b->c->doSomething;
    }
}

class B {
    function __construct () {
        $this->c = new C ();
    }
}

class C {
    function __construct () { }
    function doSomething () { echo "¡Hi!"; }
}

$a = new A ();
// Instead of doing this,
$a->b->c->doSomething();

// I want to do this.
$a->doSomething(); // ¡Hi!
?>

Basically, it's just to shorten the line to access a particular function. But, is it possible?!

Thanks,
~Philip

--- End Message ---
--- Begin Message ---
Hello Philip

First thing first: design patterns are your friend. A good reference
for which, is:

http://www.fluffycat.com/PHP-Design-Patterns/

Second of all. What is the situation in which you are trying to do
this? I can't really think of one where you would do such a thing.

- Craige

On Fri, May 2, 2008 at 3:09 PM, Philip Thompson <[EMAIL PROTECTED]> wrote:
> Hi all. I have several classes. Within each class, a new class is called. Is
> there a way to assign a function in a *deeper* class to be called in the
> first class? Example to follow......
>
>  <?php
>  class A {
>     function __construct () {
>         $this->b = new B ();
>         // I want to do the following. This does not work, of course.
>         $this->doSomething = $this->b->c->doSomething;
>     }
>  }
>
>  class B {
>     function __construct () {
>         $this->c = new C ();
>     }
>  }
>
>  class C {
>     function __construct () { }
>     function doSomething () { echo "¡Hi!"; }
>  }
>
>  $a = new A ();
>  // Instead of doing this,
>  $a->b->c->doSomething();
>
>  // I want to do this.
>  $a->doSomething(); // ¡Hi!
>  ?>
>
>  Basically, it's just to shorten the line to access a particular function.
> But, is it possible?!
>
>  Thanks,
>  ~Philip
>  --
>  PHP General Mailing List (http://www.php.net/)
>  To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
On Fri, May 2, 2008 at 1:09 PM, Philip Thompson <[EMAIL PROTECTED]>
wrote:

> Hi all. I have several classes. Within each class, a new class is called.
> Is there a way to assign a function in a *deeper* class to be called in the
> first class? Example to follow......
>
> <?php
> class A {
>    function __construct () {
>        $this->b = new B ();
>        // I want to do the following. This does not work, of course.
>        $this->doSomething = $this->b->c->doSomething;
>    }
> }
>
> class B {
>    function __construct () {
>        $this->c = new C ();
>    }
> }
>
> class C {
>    function __construct () { }
>    function doSomething () { echo "¡Hi!"; }
> }
>
> $a = new A ();
> // Instead of doing this,
> $a->b->c->doSomething();
>
> // I want to do this.
> $a->doSomething(); // ¡Hi!
> ?>
>
> Basically, it's just to shorten the line to access a particular function.
> But, is it possible?!


i cant remember what the term is for it phillip (ill look later), but thats
sort of considered a bad practice..  primarily since c is composed by b and
a doesnt really know about it, then the way a should talk to c is through
b.  so i would create a wrapper method in b (you have many implementation
options here) as a simple example something like this

class B {
  //...
  function doSomething() {
      return $this->c->doSomething();
  }
}

which allows you this in A instances

$this->b->doSomething();

this is the preferred approach, since A and C instances are loosely coupled.

of course, if you wanted a to 'know' about c then you could do something
like this,

class B {
  // ..
  function getC() {
     return $this->c;
  }
}

giving you the ability to do this in A instances

$this->b->getC()->doSomething();

of course now A's knows about C's and your system is more tightly coupled.

-nathan

--- End Message ---
--- Begin Message ---
ons here) as a simple example something like this
>
>  class B {
>   //...
>   function doSomething() {
>       return $this->c->doSomething();
>   }
>  }
>
>  which allows you this in A instances
>
>  $this->b->doSomething();
>
>  this is the preferred approach, since A and C instances are loosely coupled.
>
>  of course, if you wanted a to 'know' about c then you could do something
>  like this,
>
>  class B {
>   // ..
>   function getC() {
>      return $this->c;
>   }
>  }
>
>  giving you the ability to do this in A instances
>
>  $this->b->getC()->doSomething();
>
>  of course now A's knows about C's and your system is more tightly coupled.
>
>  -nathan
>

Why don't you just do a registry pattern instance then? IE:

class Registry
{
  private satic objs;

  public function __construct()
 {

    self::$objs = function_get_args();
  }

  public static function __get($obj)
  {
    return self::$objs[$obj];
  }
}

class A
{
...
}

class B
{
...
}

$reg = new Registry( new A(), new B());

Now A and B can access each other through Registry::A and Registry::B

(that code may not function. It's just a general example)

--- End Message ---
--- Begin Message ---
On Fri, May 2, 2008 at 2:03 PM, Craige Leeder <[EMAIL PROTECTED]> wrote:

> ons here) as a simple example something like this
> >
> >  class B {
> >   //...
> >   function doSomething() {
> >       return $this->c->doSomething();
> >   }
> >  }
> >
> >  which allows you this in A instances
> >
> >  $this->b->doSomething();
> >
> >  this is the preferred approach, since A and C instances are loosely
> coupled.
> >
> >  of course, if you wanted a to 'know' about c then you could do something
> >  like this,
> >
> >  class B {
> >   // ..
> >   function getC() {
> >      return $this->c;
> >   }
> >  }
> >
> >  giving you the ability to do this in A instances
> >
> >  $this->b->getC()->doSomething();
> >
> >  of course now A's knows about C's and your system is more tightly
> coupled.
> >
> >  -nathan
> >
>
> Why don't you just do a registry pattern instance then? IE:
>
> class Registry
> {
>  private satic objs;
>
>  public function __construct()
>  {
>
>    self::$objs = function_get_args();
>  }
>
>  public static function __get($obj)
>  {
>    return self::$objs[$obj];
>  }
> }
>
> class A
> {
> ...
> }
>
> class B
> {
> ...
> }
>
> $reg = new Registry( new A(), new B());
>
> Now A and B can access each other through Registry::A and Registry::B
>
> (that code may not function. It's just a general example)


that seems like overkill to me, and anyway, A instances need to get at C
instances.. the Registry would need to be globally available and even then,
if C's and A's could talk to each other through it, it still presents the
issue where A instances know about C instances.  its a design decision to be
sure, but likely the cleanest solution will have A instances not knowing
about C instances.

-nathan

--- End Message ---
--- Begin Message ---
On Fri, May 2, 2008 at 1:48 PM, Nathan Nobbe <[EMAIL PROTECTED]> wrote:

> On Fri, May 2, 2008 at 1:09 PM, Philip Thompson <[EMAIL PROTECTED]>
> wrote:
>
>> Hi all. I have several classes. Within each class, a new class is called.
>> Is there a way to assign a function in a *deeper* class to be called in the
>> first class? Example to follow......
>>
>> <?php
>> class A {
>>    function __construct () {
>>        $this->b = new B ();
>>        // I want to do the following. This does not work, of course.
>>        $this->doSomething = $this->b->c->doSomething;
>>    }
>> }
>>
>> class B {
>>    function __construct () {
>>        $this->c = new C ();
>>    }
>> }
>>
>> class C {
>>    function __construct () { }
>>    function doSomething () { echo "¡Hi!"; }
>> }
>>
>> $a = new A ();
>> // Instead of doing this,
>> $a->b->c->doSomething();
>>
>> // I want to do this.
>> $a->doSomething(); // ¡Hi!
>> ?>
>>
>> Basically, it's just to shorten the line to access a particular function.
>> But, is it possible?!
>
>
> i cant remember what the term is for it phillip (ill look later)
>

i remember what it is know; the principal of least knowledge ;)

http://en.wikipedia.org/wiki/Law_of_Demeter

-nathan

--- End Message ---
--- Begin Message ---
2008/5/2 Philip Thompson <[EMAIL PROTECTED]>:
> Hi all. I have several classes. Within each class, a new class is called. Is
> there a way to assign a function in a *deeper* class to be called in the
> first class? Example to follow......
>
>  <?php
>  class A {
>     function __construct () {
>         $this->b = new B ();
>         // I want to do the following. This does not work, of course.
>         $this->doSomething = $this->b->c->doSomething;
>     }
>  }
>
>  class B {
>     function __construct () {
>         $this->c = new C ();
>     }
>  }
>
>  class C {
>     function __construct () { }
>     function doSomething () { echo "¡Hi!"; }
>  }
>
>  $a = new A ();
>  // Instead of doing this,
>  $a->b->c->doSomething();
>
>  // I want to do this.
>  $a->doSomething(); // ¡Hi!
>  ?>
>
>  Basically, it's just to shorten the line to access a particular function..
> But, is it possible?!

Inheritance - http://uk.php.net/extends

class A {
  function methodOfA() {}
}

class B extends A {}

B::methodOfA();

(use sparingly - too much inheritance results in a tightly-coupled
hierarchy of classes, which are brittle and hard to re-use).

Or simply express the method in the interface of the exposed class:

class A {
  function methodOfA() {}
}

class B {
  function methodOfA() {
    A::methodOfA();
  }
}

b::methodOfA();

--- End Message ---

Reply via email to