1. <?
  2.  
  3. class Iteration extends Base{
  4.  
  5.  
  6. /*
  7. example
  8. $iteration->forObjects("user")->query("select * from users where active")->displayFields("ID,name,lastlog|date")->displayTable("user_table")
  9. */
  10.  
  11. private $_iteration_methods;
  12. private $_current_method;
  13. private $_current_class;
  14. private $_display_fields = array();
  15. public $_modifiers = array();
  16. public $rows = array();
  17.  
  18. public function __construct(){
  19. $this->_iteration_methods = $this->enum(ITERATION_OBJECTS,ITERATION_ROWS);
  20. }
  21.  
  22. public function forObjects($class){
  23. $this->_current_method = ITERATION_OBJECTS;
  24. $this->_current_class = $class;
  25. return $this;
  26. }
  27.  
  28. public function forRows(){
  29. $this->_current_method = ITERATION_ROWS;
  30. return $this;
  31. }
  32.  
  33. public function query($sql){
  34. $this->rows = getQueryRows($sql);
  35. return $this;
  36. }
  37.  
  38. public function display($type,$cssid){
  39. switch($type){
  40. case "table":
  41. $r = $this->displayTable($cssid);
  42. break;
  43. }
  44. return $this;
  45. }
  46.  
  47. public function displayFields($fields){
  48. $fields = explode(",",$fields);
  49. foreach($fields as $field){
  50. if(strpos($field,"|")){
  51. $field_and_modifiers = explode("|", $field);
  52. $this->_display_fields[] = $field_and_modifiers[0];
  53. for($i = 1; $i < count($field_and_modifiers);$i++) $this->_modifiers[$field_and_modifiers[0]][] = $field_and_modifiers[$i];
  54. }
  55. else{
  56. $this->_display_fields[] = $field;
  57. }
  58. }
  59. return $this;
  60. }
  61.  
  62. public function displayTable($cssid){
  63. echo "<table id=" . $cssid . "><tr>\n";
  64. foreach($this->_display_fields as $field) echo "<th>" . strtoupper($field) . "</th>";
  65. echo "</tr>";
  66. switch($this->_current_method){
  67. case ITERATION_OBJECTS:
  68. foreach($this->rows as $row){
  69. echo "<tr id=" . $cssid . "_row_" . $row['ID'] . ">";
  70. $obj = new $this->_current_class((int)$row['ID']);
  71. foreach($this->_display_fields as $field){
  72. $value = $obj->$field();
  73. if($this->_modifiers[$field]) $value = $this->modify($field, $value);
  74. echo "<td id=" . $cssid . "_" . $field . "_" . $row['ID'] . ">" . $value . "</td>";
  75. }
  76. echo "</tr>\n";
  77. unset($obj);
  78. }
  79. break;
  80. case ITERATION_ROWS:
  81. foreach($this->rows as $row){
  82. echo "<tr id=" . $cssid . "_row_" . $row['ID'] . ">";
  83. foreach($this->_display_fields as $field){
  84. $value = $row[$field];
  85. if($this->_modifiers[$field]) $value = $this->modify($value);
  86. echo "<td id=" . $cssid . "_" . $field . "_" . $row['ID'] . ">" . $value . "</td>";
  87. }
  88. echo "</tr>\n";
  89. unset($obj);
  90. }
  91.  
  92.  
  93. }
  94. echo "</table>\n";
  95. return $this;
  96. }
  97.  
  98. function modify($field, $value){
  99. if(!$value) return $value;
  100. foreach($this->_modifiers[$field] as $modifier){
  101. switch($modifier){
  102. case "date":
  103. $value = date("m-d-Y",(int)$value);
  104. break;
  105. }
  106. }
  107. return $value;
  108. }
  109.  
  110.  
  111. }
  112. ?>