The Code-Bin
Links
Home
Add your code!
All Listings
About
Latest Entry
Featured Scripts
Author's Website
Latest Entries
FFMPEG Thumbnail Scr...
PHP, 0.8KB
Jul. 29, 10:24pm
John
Z80 Assembler, 190 bytes
Feb. 17, 3:36am
John
Z80 Assembler, 176 bytes
Sep. 13, 2:19am
John
Z80 Assembler, 77 bytes
Sep. 13, 2:18am
John
Z80 Assembler, 209 bytes
Sep. 13, 2:17am
Iteration Class
Posted by: Matt Donohue | July 5, 2010 @ 3:35pm
PHP Code
[
Download
]
<? class Iteration extends Base{ /* example $iteration->forObjects("user")->query("select * from users where active")->displayFields("ID,name,lastlog|date")->displayTable("user_table") */ private $_iteration_methods; private $_current_method; private $_current_class; private $_display_fields = array(); public $_modifiers = array(); public $rows = array(); public function __construct(){ $this->_iteration_methods = $this->enum(ITERATION_OBJECTS,ITERATION_ROWS); } public function forObjects($class){ $this->_current_method = ITERATION_OBJECTS; $this->_current_class = $class; return $this; } public function forRows(){ $this->_current_method = ITERATION_ROWS; return $this; } public function query($sql){ $this->rows = getQueryRows($sql); return $this; } public function display($type,$cssid){ switch($type){ case "table": $r = $this->displayTable($cssid); break; } return $this; } public function displayFields($fields){ $fields = explode(",",$fields); foreach($fields as $field){ if(strpos($field,"|")){ $field_and_modifiers = explode("|", $field); $this->_display_fields[] = $field_and_modifiers[0]; for($i = 1; $i < count($field_and_modifiers);$i++) $this->_modifiers[$field_and_modifiers[0]][] = $field_and_modifiers[$i]; } else{ $this->_display_fields[] = $field; } } return $this; } public function displayTable($cssid){ echo "<table id=" . $cssid . "><tr>\n"; foreach($this->_display_fields as $field) echo "<th>" . strtoupper($field) . "</th>"; echo "</tr>"; switch($this->_current_method){ case ITERATION_OBJECTS: foreach($this->rows as $row){ echo "<tr id=" . $cssid . "_row_" . $row['ID'] . ">"; $obj = new $this->_current_class((int)$row['ID']); foreach($this->_display_fields as $field){ $value = $obj->$field(); if($this->_modifiers[$field]) $value = $this->modify($field, $value); echo "<td id=" . $cssid . "_" . $field . "_" . $row['ID'] . ">" . $value . "</td>"; } echo "</tr>\n"; unset($obj); } break; case ITERATION_ROWS: foreach($this->rows as $row){ echo "<tr id=" . $cssid . "_row_" . $row['ID'] . ">"; foreach($this->_display_fields as $field){ $value = $row[$field]; if($this->_modifiers[$field]) $value = $this->modify($value); echo "<td id=" . $cssid . "_" . $field . "_" . $row['ID'] . ">" . $value . "</td>"; } echo "</tr>\n"; unset($obj); } } echo "</table>\n"; return $this; } function modify($field, $value){ if(!$value) return $value; foreach($this->_modifiers[$field] as $modifier){ switch($modifier){ case "date": $value = date("m-d-Y",(int)$value); break; } } return $value; } } ?>
Syntax Highlighting
[
Open in new window
]
Author Comments
none
Rating
4.56 / 8
55 Votes