Created
August 8, 2013 18:13
-
-
Save leekiernan/6187145 to your computer and use it in GitHub Desktop.
simple retreive news events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| define( 'DB_HOST', '' ); | |
| define( 'DB_NAME', '' ); | |
| define( 'DB_USER', '' ); | |
| define( 'DB_PASS', '' ); | |
| define( 'TBL_CORP_NEWS', 'corporate_news' ); | |
| define( 'TBL_CORP_EVNT', 'corporate_events' ); | |
| define( 'TBL_GOLF_NEWS', 'golf_news' ); | |
| define( 'TBL_GOLF_EVNT', 'golf_events' ); | |
| define( 'TBL_HLTH_NEWS', 'health_news' ); | |
| define( 'TBL_HLTH_EVNT', 'health_events' ); | |
| define( 'TBL_SPA_NEWS', 'spa_news' ); | |
| define( 'TBL_SPA_EVNT', 'spa_events' ); | |
| define( 'TBL_REST_EVNT', 'restaurant_events' ); | |
| define( 'TBL_REST_NEWS', 'restaurant_news' ); | |
| define( 'TBL_WED_NEWS', 'wedding_news' ); | |
| define( 'TBL_WED_EVNT', 'wedding_events' ); | |
| define( 'TBL_ALL_NEWS', serialize( array(TBL_CORP_NEWS, TBL_GOLF_NEWS, TBL_HLTH_NEWS, TBL_SPA_NEWS, TBL_REST_NEWS, TBL_WED_NEWS) )); | |
| define( 'TBL_ALL_EVENTS', serialize( array(TBL_CORP_EVNT, TBL_GOLF_EVNT, TBL_HLTH_EVNT, TBL_SPA_EVNT, TBL_REST_EVNT, TBL_WED_EVNT) )); | |
| define( 'N_ID', 'id' ); | |
| define( 'N_TITLE', 'title' ); | |
| define( 'N_ADDED', 'addeddate' ); | |
| define( 'N_DATE', 'eventdate' ); | |
| define( 'N_CONTENT', 'content' ); | |
| define( 'N_BUSINESS', 'from' ); | |
| class News | |
| { | |
| protected $connection, $database; | |
| public function __construct() | |
| { | |
| // $connection = new DB_Connection(); | |
| $this->connection = mysql_connect( DB_HOST, DB_USER, DB_PASS ) or die( mysql_error() ); | |
| $this->database = mysql_select_db( DB_NAME, $this->connection ) or die( mysql_error() ); | |
| } | |
| protected function clean_params( $params ) | |
| { | |
| if( !$params || !is_array($params) ) | |
| return false; | |
| foreach( $params as $e ) | |
| { | |
| $e = mysql_real_escape_string($e); | |
| } | |
| return $params; | |
| } | |
| protected function query_database( $query ) | |
| { | |
| $arr_params = func_get_args(); | |
| array_shift( $arr_params ); | |
| $arr_params = $this->clean_params( $arr_params ); | |
| $query = vsprintf( $query, $arr_params ); | |
| return mysql_query( $query, $this->connection ); | |
| } | |
| protected function get_from_any( $tables ) | |
| { | |
| if( !$tables ) | |
| return false; | |
| $arr_items = array(); | |
| foreach( $tables as $t ) | |
| { | |
| $result = $this->query_database( | |
| "SELECT * FROM %s", | |
| $t ); | |
| // where DATE(%s) BETWEEN DATE_ADD(NOW(), INTERVAL -6 MONTH) AND DATE(NOW()), N_ADDED ); | |
| while( $row = mysql_fetch_assoc($result) ) | |
| { | |
| $row['from'] = $t; | |
| array_push($arr_items, $row); | |
| } | |
| } | |
| usort( $arr_items, function($a, $b) { return strtotime($b["addeddate"]) - strtotime($a["addeddate"]); } ); | |
| // print_r ( $arr_items ); | |
| return $arr_items; | |
| } | |
| public function get_all_news() | |
| { | |
| $all_news = unserialize( TBL_ALL_NEWS ); | |
| return $this->get_from_any( $all_news ); | |
| } | |
| protected function get_all_events() | |
| { | |
| $all_events = unserialize( TBL_ALL_EVENTS ); | |
| return $this->get_from_any( $all_events ); | |
| } | |
| protected function get_by_type( $type ) | |
| { | |
| return $this->get_from_any( array($type) ); | |
| } | |
| protected function get_events_by_type($type = null) | |
| { | |
| if( !$type || is_null($type) ) | |
| return $this->get_all_events(); | |
| return $this->get_by_type( $type ); | |
| } | |
| protected function get_news_by_type($type = null) | |
| { | |
| if( !$type || is_null($type) ) | |
| { | |
| return $this->get_all_news(); | |
| } | |
| return $this->get_by_type( $type ); | |
| } | |
| protected function strip_utf( $string ) | |
| { | |
| for ($i = 0; $i < strlen ($string); $i++) | |
| { | |
| if( ord($string[$i]) > 127 ) $string[$i] = " "; | |
| } | |
| return $string; | |
| } | |
| protected function get_event_attr($event, $attribute) | |
| { | |
| return $this->strip_utf( $event[$attribute] ); | |
| } | |
| protected function date_as_html($item) | |
| { | |
| if( $this->get_event_attr($item, N_ADDED) ) | |
| $timestamp = strtotime( $this->get_event_attr($item, N_ADDED) ); | |
| else if( $this->get_event_attr($item, N_DATE) ) | |
| $timestamp = strtotime( $this->get_event_attr($item, N_DATE) ); | |
| return sprintf( "<p class='calendar'>%s<em class='%s'>%s</em></p>", | |
| // date('D', $timestamp), | |
| date('j', $timestamp), | |
| $this->get_event_attr($item, N_BUSINESS), | |
| date('M', $timestamp) | |
| ); | |
| } | |
| protected function display_feed_item( $item ) | |
| { | |
| echo "<div class='wrapper'>"; | |
| echo sprintf( "<div class='date'>%s</div>", $this->date_as_html( $item ) ); | |
| echo sprintf( "<div class='header'><h2>%s</h2></div>", $this->get_event_attr($item, N_TITLE ) ); | |
| echo sprintf( "<div class='content'><p>%s</p></div>", nl2br($this->get_event_attr($item, N_CONTENT)) ); | |
| echo "</div>"; | |
| } | |
| public function display_feed_by_type( $type=null ) | |
| { | |
| switch( $type ) | |
| { | |
| case "events" : | |
| $data = $this->get_all_events(); | |
| break; | |
| case "news" : | |
| $data = $this->get_all_news(); | |
| break; | |
| default : | |
| $data = array_merge( $this->get_all_events(), $this->get_all_news() ); | |
| break; | |
| } | |
| echo "<ul id='$type' class='listitems newsevents'>"; | |
| foreach( $data as $r ) | |
| { | |
| echo sprintf( "<li class='%s %s Everything' data-id='%s'>", | |
| $type, | |
| ucfirst( str_replace( array("_events","_news"), "", $this->get_event_attr($r, N_BUSINESS )) ), | |
| $this->get_event_attr($r, N_ID ) | |
| ); | |
| $this->display_feed_item( $r ); | |
| echo "</li>"; | |
| } | |
| echo "</ul>"; | |
| } | |
| public function display_feed_by_business( $business ) | |
| { | |
| $data = $this->get_events_by_type($business); | |
| if( sizeof($data) <= 0 ) | |
| { | |
| if( stripos($business, '_news') ) | |
| { | |
| echo '<h2>News coming soon</h2>'; | |
| } | |
| return false; | |
| } | |
| echo "<ul id='$business' class='listitems newsevents'>"; | |
| foreach( $data as $r ) | |
| { | |
| echo sprintf( "<li class='%s' data-id='%s'>", | |
| $type, | |
| ucfirst( str_replace( array("_events","_news"), "", $this->get_event_attr($r, N_BUSINESS )) ), | |
| $this->get_event_attr($r, N_ID ) | |
| ); | |
| $this->display_feed_item( $r ); | |
| echo "</li>"; | |
| } | |
| echo "</ul>"; | |
| } | |
| public function display_navigation() | |
| { | |
| echo "<ul class='offernav'>"; | |
| echo sprintf( "<li class='news-group-item'><a class='news' href='#'> %s </a></li>", 'Everything') ; | |
| foreach( unserialize(TBL_ALL_NEWS) as $n ) | |
| { | |
| $n = str_replace( array("_events","_news"), "", $n ); | |
| echo sprintf( "<li class='news-group-item'><a href='#'> %s </a></li>", ucfirst($n) ) ; | |
| } | |
| echo "</ul>"; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment