Skip to content

Instantly share code, notes, and snippets.

@leekiernan
Created August 8, 2013 18:13
Show Gist options
  • Save leekiernan/6187147 to your computer and use it in GitHub Desktop.
Save leekiernan/6187147 to your computer and use it in GitHub Desktop.
simple retreive offers
<?php
define( 'DB_HOST', '' );
define( 'DB_NAME', '' );
define( 'DB_TABL', '' );
define( 'DB_USER', '' );
define( 'DB_PASS', '' );
define ( 'E_DATE', 'eventdate' );
class DB_Connection
{
}
class Offers
{
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( !is_array($params) )
return false;
foreach( $params as $a )
{
$a = mysql_real_escape_string($a);
}
return $params;
}
public function query_offers( $query, $arr_params=null )
{
$arr_params = $this->clean_params( $arr_params );
$sql = vsprintf( $query, $arr_params );
$result = mysql_query( $sql, $this->connection );
return $result;
}
protected function move_to_past_offer( $offer )
{
// $move = $this->query_offers( 'INSERT INTO %s SELECT * FROM %s where id = %d', array('past_resort_offers', 'resort_offers', $offer['id'] ) );
$move = sprintf( 'INSERT INTO %s SELECT * FROM %s where id = %d',
'past_resort_offers', 'resort_offers', $offer['id'] );
$remove = sprintf( 'DELETE FROM %s WHERE id = %d', 'resort_offers', $offer['id'] );
$this->query_offers( $move );
$this->query_offers( $remove );
return true;
}
public function get_offers_of_type( $type=null )
{
if( !$type )
$query = sprintf( "SELECT * FROM %s", DB_TABL );
else
$query = sprintf( "SELECT * FROM %s WHERE type = '%s'", DB_TABL, $type );
return $this->query_offers( $query );
}
public function get_navigation( )
{
$result = $this->query_offers( sprintf("SELECT %s FROM %s GROUP BY %s", 'AppliesTo', DB_TABL, 'AppliesTo') );
$types = array();
while( $r = mysql_fetch_array($result) )
array_push($types, $r['AppliesTo'] );
return $types;
}
public function get_offer_id( $offer )
{
if( !$offer['id'] )
return false;
return $offer['id'];
}
public function get_offer_title( $offer )
{
if( !$offer['Title'] )
return false;
return $offer['Title'];
}
public function get_offer_subheader( $offer )
{
if( !$offer['Subheader'] )
return false;
return $offer['Subheader'];
}
public function get_offer_description( $offer )
{
if( !$offer['Description'] )
return false;
return $offer['Description'];
}
public function get_offer_terms( $offer )
{
if( !$offer['Terms'] )
return false;
return $offer['Terms'];
}
public function get_offer_image( $offer )
{
if( !$offer['Image'] )
return false;
return $offer['Image'];
}
public function get_offer_type( $offer )
{
if( !$offer['AppliesTo'] )
return false;
return $offer['AppliesTo'];
}
public function get_offer_times( $offer )
{
if( !$offer['TimeAllowed'] )
return false;
return $offer['TimeAllowed'];
}
public function get_offer_link_html( $offer )
{
if( !$offer['Link'] )
return false;
return sprintf( "<a href='%s'>%s</a>", $offer['Link'], $offer['LinkTitle'] );
}
public function get_time_to_deadline( $offer )
{
if( !$offer['Deadline'] )
return false;
return strtotime( $offer['Deadline'] ) - time();
}
protected function strip_utf( $string )
{
for ($i = 0; $i < strlen ($string); $i++)
{
if( ord($string[$i]) > 127 ) $string[$i] = " ";
}
return $string;
}
private function wrap_offer_html( $content )
{
return "<div class='offer wrapper'>".$content."</div>";
}
public function display_navigation()
{
echo "<ul class='offernav'>";
echo sprintf( "<li class='offer-group-item'><a class='suboffer' href='#'> %s </a></li>", 'Everything') ;
foreach( $this->get_navigation() as $n )
{
echo sprintf( "<li class='offer-group-item'><a href='#'> %s </a></li>", $n) ;
}
echo "</ul>";
}
protected function display_offer_html( $offer )
{
$title = $this->get_offer_title( $offer );
echo "<div class='wrapper'>";
// if( ($image = $this->get_offer_image($offer)) ) {
echo sprintf( "<div class='offerimg'> <img src='%s' alt='%s' class='offer-img' /> </div>",
$this->get_offer_image($offer), // $image,
$title
);
// }
echo sprintf( "<div class='header'><h2>%s</h2></div>", $title );
echo ( ($sh = $this->get_offer_subheader($offer)) ?
sprintf( "<div class='subheader'><h3>%s</h3></div>", $sh ) : "" );
echo sprintf( "<div class='moreinfo'><a href='#'>%s</a></div>", 'More info' );
echo ( ($d = $this->get_offer_description($offer)) ?
sprintf( "<div class='descript'><p>%s</p></div>", nl2br($d) ) : "" );
echo ( ($ta = $this->get_offer_times($offer)) ?
sprintf( "<div class='timerange'><p>%s</p></div>", $ta ) : "" );
echo ( ($t = $this->get_offer_terms($offer)) ?
sprintf( "<div class='terms'><em>%s</em></div>", $t ) : "" );
echo ( ($dl = $this->get_time_to_deadline($offer)) ?
sprintf( "<div class='remaining'><strong>%s</strong></div>", $dl ) : "" );
echo ( ($l = $this->get_offer_link_html($offer)) ?
sprintf( "%s", $l ) : "" );
echo "</div>";
}
public function display_offers()
{
return $this->display_offers_of_type( );
}
public function display_offers_of_type( $type=null )
{
$resource = $this->get_offers_of_type( $type );
// usort( $resource, function($a, $b) { return strtotime($b["addeddate"]) - strtotime($a["addeddate"]); } );
echo "<ul id='$type' class='offer-items listitems'>";
while( $r = mysql_fetch_assoc($resource) )
{
if( $this->get_time_to_deadline($r) < 0 )
{
$this->move_to_past_offer($r);
break;
}
echo sprintf( "<li class='offer %s Everything' data-id='%s'>", $this->get_offer_type($r), $this->get_offer_id($r) );
$this->display_offer_html($r);
echo "</li>";
}
echo "</ul>";
}
public function display_offers_sidebar()
{
$resource = $this->get_offers_of_type( );
// usort( $resource, function($a, $b) { return strtotime($b["addeddate"]) - strtotime($a["addeddate"]); } );
echo '<div class="offers">';
while( $r = mysql_fetch_array($resource) )
{
$this->display_offer_html_brief($r);
}
echo '</div>';
}
protected function display_offer_html_brief( $offer )
{
$title = $this->get_offer_title( $offer );
echo "<div><a href='offers.php'>";
echo sprintf( "<img src='%s' alt='%s' />",
$this->get_offer_image($offer),
$title
);
echo sprintf( "<p class='offerHeading'>%s</p>", $title );
echo ( ($d = $this->get_offer_subheader($offer)) ?
substr( sprintf( "<p>%s", nl2br($d) ), 0, 40).'...</p>' : ( ($e = $this->get_offer_description($offer)) ?
substr( sprintf( "<p>%s", nl2br($e) ), 0, 40).'...</p>' : "" ) );
echo "</a></div>";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment