Calling a method on array iteration, from an object - PHP -


what i'm trying do, iterate through array (which in case mysql result), , output it, else data @ same.

doing using procedural methods easy - put in foreach loop.

but, i'm wondering if there way integrated object.

so, example, wanted put first field session, this:

<?php     class myclass {          public $myarray=array();          public function __construct() {              //..             //mysql query $stmt             //..             $this->myarray=$stmt->fetchall(pdo::fetch_assoc);          }      }      $obj=new myclass;     $i=0;     foreach($obj->myarray $row) {         ?>             <!-- output html formatted data -->         <?         $_session[$i]=$row['firstfield'];         $i++;     } ?> 

but, takes task away class.

i put foreach loop in class, so:

<?php     class myclass {          public $myarray=array();          public function __construct() {              //..             //mysql query $stmt             //..             $this->myarray=$stmt->fetchall(pdo::fetch_assoc);             $i=0;             foreach($this->myarray $row) {                 $_session[$i]=$row['firstfield'];                 $i++;             }          }      }      $obj=new myclass;      foreach($obj->myarray $row) {         ?>             <!-- output html formatted data -->         <?     } ?> 

but, have 2 loops on same data-set. doubling time same task.

is there way create method when array being looped through? making data-set have looped through once ...

edit

also, forgot mention, reason can't build html within object, because used on different pages different html layouts.

how this

<?php     class myclass {          public $myarray=array();         public $htm = null;          public function __construct(&$format=null) {             //..             //mysql query $stmt             //..             $this->myarray=$stmt->fetchall(pdo::fetch_assoc);             $i=0;             foreach($this->myarray $row) {                 switch ($format) {                   case 'page1' :                      $this->htm .= $this->format1($row);                      break;                   case 'page2' :                      $this->htm .= $this->format2($row);                      break;                   default:                      $this->htm .= $this->format_default($row);                 }                 $_session[$i]=$row['firstfield'];                 $i++;             }         }          private function format1($row) {            return // formatted html         }          private function format2($row) {            return // formatted html         }          private function format_default($row) {            return // formatted html         }      }      $obj=new myclass('page1');      echo $obj->htm; ?> 

alternatively subclass myclass many subclasses need formats require.

class mybaseclass {      public $myarray=array();      public function __construct() {          //..         //mysql query $stmt         //..         $this->myarray=$stmt->fetchall(pdo::fetch_assoc);      }  }  class format1class extends mybaseclass {     public $htm;      public function __construct() {         parent::_construct();          $i=0;         foreach($this->myarray $row) {             $this->htm .= // format specific class              $_session[$i]=$row['firstfield'];             $i++;         }     } }  class format2class extends mybaseclass {     public $htm;      public function __construct() {         parent::_construct();          $i=0;         foreach($this->myarray $row) {             $this->htm .= // format specific class              $_session[$i]=$row['firstfield'];             $i++;         }     } } 

now depending on format require in script instantiate required class.

$obj = new format2class();  echo $obj->htm; 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -