connection = mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_password) or die('Unable to connect to database'); mysql_select_db($this->mysql_db) or die('Unable to select database'); } function quoteSmart($value){ if (get_magic_quotes_gpc()) { $value = stripslashes($value); } if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string(htmlspecialchars($value)) . "'"; } return $value; } function validateArray($info_array){ $correctKeys = array('headline', 'start', 'end', 'shown', 'text'); if(count($info_array) != count($correctKeys)){ return false; }else{ foreach($correctKeys as $key => $value){ if(!(array_key_exists($value, $info_array))){ return false; } } } return true; } function addEntry($info_array){ $is_valid = validateArray($info_array); if(!$is_valid){ return 'Array is not valid'; }else{ $query = 'INSERT INTO `' . $this->mysql_table . '` VALUES (NULL, ' . $this->quoteSmart($info_array['headline']) . ', ' . $this->quoteSmart($info_array['start']) . ', ' . $this->quoteSmart($info_array['end']) . ', ' . $this->quoteSmart($info_array['shown']) . ', ' . $this->quoteSmart($info_array['text']) . ')'; $result = mysql_query($query); if($result){ return 'Query ran successfully'; }else{ return 'Query failed'; } } } function updateEntry($id, $info_array){ $is_valid = validateArray($info_array); if(!$is_valid){ return 'Array is not valid'; }else{ $query = 'UPDATE `' . $this->mysql_table . '` SET headline=' . $this->quoteSmart($info_array['headline']) . ', start=' . $this->quoteSmart($info_array['start']) . ', end=' . $this->quoteSmart($info_array['end']) . ', shown=' . $this->quoteSmart($info_array['shown']) . ', text=' . $this->quoteSmart($info_array['text']) . " WHERE id=$id"; $result = mysql_query($query); if($result){ return 'Query ran successfully'; }else{ return 'Query failed'; } } } function removeEntry($id){ $query = 'DELETE FROM `' . $this->mysql_table . "` WHERE id=$id"; $result = mysql_query($query); if($result){ return 'Query ran successfully'; }else{ return 'Query failed'; } } function getAll($sort){ $query = 'SELECT * FROM `' . $this->mysql_table . '` ORDER BY ' . $this->quoteSmart($sort); $result = mysql_query($query); while($tempArray = mysql_fetch_array($result)){ $array[$tempArray['id']] = $tempArray; } return $array; } function getCurrent($sort){ $query = 'SELECT * FROM `' . $this->mysql_table . '` ORDER BY ' . $this->quoteSmart($sort); $result = mysql_query($query); while($tempArray = mysql_fetch_array($result)){ if($tempArray['end'] >= date("Y-m-d")){ if($tempArray['start'] <= date("Y-m-d")){ $array[$tempArray['id']] = $tempArray; } } } return $array; } function close(){ mysql_close($this->connection); } } ?>