| 1 | <?php | 
| 2 |  | 
| 3 | /** | 
| 4 | * This example illustrates the use of the | 
| 5 | * DataList object classes.  This object | 
| 6 | * can show a list of data from any data source | 
| 7 | * and have any GUI layout and provide the | 
| 8 | * features of: | 
| 9 | * searching, sorting, paging of the data. | 
| 10 | * | 
| 11 | * This page shows the Data coming from 2 different | 
| 12 | * types of DB objects.  One from a PEAR::DB object, | 
| 13 | * and another from a ADODB object. | 
| 14 | * | 
| 15 | * $Id: widget6.php,v 1.10 2004/04/01 07:14:17 hemna Exp $ | 
| 16 | * | 
| 17 | * @author Walter A. Boring IV <waboring@buildabetterweb.com> | 
| 18 | * @package phpHtmlLib | 
| 19 | * @subpackage widget-examples | 
| 20 | * @version 2.0 | 
| 21 | * | 
| 22 | */ | 
| 23 |  | 
| 24 | /** | 
| 25 | * Include the phphtmllib libraries | 
| 26 | * | 
| 27 | */ | 
| 28 | include_once("includes.inc"); | 
| 29 | include_once("db_defines.inc"); | 
| 30 |  | 
| 31 | include_once($phphtmllib."/widgets/data_list/includes.inc"); | 
| 32 | include_once($phphtmllib."/widgets/data_list/PEARSQLDataListSource.inc"); | 
| 33 |  | 
| 34 | //add the include path for adodb | 
| 35 | //assuming you have the adodb dir in your | 
| 36 | //site's document root. | 
| 37 | ini_set("include_path", | 
| 38 | ini_get("include_path").":".$_SERVER["DOCUMENT_ROOT"]."/adodb"); | 
| 39 | require_once( "adodb.inc.php" ); | 
| 40 |  | 
| 41 | include_once($phphtmllib."/widgets/data_list/ADODBSQLDataListSource.inc"); | 
| 42 |  | 
| 43 | /** | 
| 44 | * This is an example that shows how to use a PEAR db object | 
| 45 | * as the source for the data to show. | 
| 46 | * | 
| 47 | * @author Walter A. Boring IV <waboring@buildabetterweb.com> | 
| 48 | * @package phpHtmlLib | 
| 49 | * @subpackage widget-examples | 
| 50 | * @version 2.0 | 
| 51 | */ | 
| 52 | class pearmysqllist extends DefaultGUIDataList { | 
| 53 | //change the # of rows to display to 20 from 10 | 
| 54 | var $_default_rows_per_page = 20; | 
| 55 |  | 
| 56 | /** | 
| 57 | * This function is called automatically by | 
| 58 | * the DataList constructor.  It must be | 
| 59 | * extended by the child class to actually | 
| 60 | * set the DataListSource object. | 
| 61 | * | 
| 62 | * | 
| 63 | */ | 
| 64 | function get_data_source() { | 
| 65 | //build the PEAR DB object and connect | 
| 66 | //to the database. | 
| 67 | $dsn = "mysql://".DB_USERNAME.":".DB_PASSWORD."@".DB_HOSTNAME."/".DB_NAME; | 
| 68 | $db = DB::connect($dsn, TRUE); | 
| 69 | if (DB::isError($db)) { | 
| 70 | die( $db->getMessage() ); | 
| 71 | } | 
| 72 |  | 
| 73 | //create the DataListSource object | 
| 74 | //and pass in the PEAR DB object | 
| 75 | $source = new PEARSQLDataListSource($db); | 
| 76 |  | 
| 77 | //set the DataListSource for this DataList | 
| 78 | //Every DataList needs a Source for it's data. | 
| 79 | $this->set_data_source( $source ); | 
| 80 |  | 
| 81 | //set the prefix for all the internal query string | 
| 82 | //variables.  You really only need to change this | 
| 83 | //if you have more then 1 DataList object per page. | 
| 84 | $this->set_global_prefix("pear_"); | 
| 85 | } | 
| 86 |  | 
| 87 | /** | 
| 88 | * This method is used to setup the options | 
| 89 | * for the DataList object's display. | 
| 90 | * Which columns to show, their respective | 
| 91 | * source column name, width, etc. etc. | 
| 92 | * | 
| 93 | * The constructor automatically calls | 
| 94 | * this function. | 
| 95 | * | 
| 96 | */ | 
| 97 | function user_setup() { | 
| 98 | //add the columns in the display that you want to view. | 
| 99 | //The API is : | 
| 100 | //Title, width, DB column name, field SORTABLE?, field SEARCHABLE?, align | 
| 101 | $this->add_header_item("IP", "100", "ip_address", SORTABLE, | 
| 102 | SEARCHABLE, "left"); | 
| 103 | $this->add_header_item("Version", "100", "version", SORTABLE, | 
| 104 | SEARCHABLE,"center"); | 
| 105 | $this->add_header_item("Time", "200", "time", SORTABLE, | 
| 106 | NOT_SEARCHABLE,"center"); | 
| 107 |  | 
| 108 |  | 
| 109 |  | 
| 110 | $columns = "*"; | 
| 111 | $tables = "user_downloads u, versions v"; | 
| 112 | $where_clause = "u.version_id=v.version_id"; | 
| 113 | $this->_datasource->setup_db_options($columns, $tables, $where_clause); | 
| 114 | } | 
| 115 |  | 
| 116 | /** | 
| 117 | * This is the basic function for letting us | 
| 118 | * do a mapping between the column name in | 
| 119 | * the header, to the value found in the DataListSource. | 
| 120 | * | 
| 121 | * NOTE: this function is can be overridden | 
| 122 | *       so that you can return whatever you want for | 
| 123 | *       any given column. | 
| 124 | * | 
| 125 | * @param array - $row_data - the entire data for the row | 
| 126 | * @param string - $col_name - the name of the column header | 
| 127 | *                             for this row to render. | 
| 128 | * @return  mixed - either a HTMLTag object, or raw text. | 
| 129 | */ | 
| 130 | function build_column_item($row_data, $col_name) { | 
| 131 | switch ($col_name) { | 
| 132 | case "Time": | 
| 133 | $dt = $row_data["time"]; | 
| 134 | $yr=strval(substr($dt,0,4)); | 
| 135 | $mo=strval(substr($dt,4,2)); | 
| 136 | $da=strval(substr($dt,6,2)); | 
| 137 | $hr=strval(substr($dt,8,2)); | 
| 138 | $mi=strval(substr($dt,10,2)); | 
| 139 | $obj = date("m/d/Y h:i A", mktime ($hr,$mi,0,$mo,$da,$yr)); | 
| 140 | break; | 
| 141 | default: | 
| 142 | $obj = DefaultGUIDataList::build_column_item($row_data, $col_name); | 
| 143 | break; | 
| 144 | } | 
| 145 | return $obj; | 
| 146 | } | 
| 147 | } | 
| 148 |  | 
| 149 | /** | 
| 150 | * This is a subclass of the pear mysql list object. | 
| 151 | * The only difference being is the DataListSource object | 
| 152 | * for an ADODB object, instead of a PEAR DB object. | 
| 153 | * | 
| 154 | */ | 
| 155 | class adodbmysqllist extends pearmysqllist { | 
| 156 |  | 
| 157 | /** | 
| 158 | * This function is called automatically by | 
| 159 | * the DataList constructor.  It must be | 
| 160 | * extended by the child class to actually | 
| 161 | * set the DataListSource object. | 
| 162 | * | 
| 163 | * | 
| 164 | */ | 
| 165 | function get_data_source() { | 
| 166 | //build the PEAR DB object and connect | 
| 167 | //to the database. | 
| 168 | $db = &ADONewConnection('mysql');  # create a connection | 
| 169 | $db->PConnect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_NAME); | 
| 170 |  | 
| 171 | //create the DataListSource object | 
| 172 | //and pass in the PEAR DB object | 
| 173 | $source = new ADODBSQLDataListSource($db); | 
| 174 |  | 
| 175 | //set the DataListSource for this DataList | 
| 176 | //Every DataList needs a Source for it's data. | 
| 177 | $this->set_data_source( $source ); | 
| 178 |  | 
| 179 | //set the prefix for all the internal query string | 
| 180 | //variables.  You really only need to change this | 
| 181 | //if you have more then 1 DataList object per page. | 
| 182 | $this->set_global_prefix("adodb_"); | 
| 183 | } | 
| 184 |  | 
| 185 | } | 
| 186 |  | 
| 187 |  | 
| 188 | //create the page object | 
| 189 | $page = new HTMLPageClass("phpHtmlLib Widgets - DataList Example", | 
| 190 | XHTML_TRANSITIONAL); | 
| 191 |  | 
| 192 | //enable output debugging. | 
| 193 | if (isset($_GET['debug'])) { | 
| 194 | $page->set_text_debug( TRUE ); | 
| 195 | } | 
| 196 |  | 
| 197 | //add the css | 
| 198 | $page->add_head_css( new DefaultGUIDataListCSS ); | 
| 199 |  | 
| 200 | //build the PEAR list and sort by ip_address by default | 
| 201 | $pearlist = new pearmysqllist("PEAR::MySQL List", 600, "ip_address", TRUE); | 
| 202 | $pearlist->set_align("right"); | 
| 203 |  | 
| 204 | //build the ADODB list using the same exact table in the DB | 
| 205 | //and sort by version by default | 
| 206 | $adodblist = new adodbmysqllist("ADODB::MySQL List", 600, "version", TRUE); | 
| 207 | $adodblist->set_align("left"); | 
| 208 |  | 
| 209 | $page->add( $pearlist, html_br(2), $adodblist ); | 
| 210 |  | 
| 211 | print $page->render(); | 
| 212 | ?> |