class pagintation
{
/*
Access: public
*/
var $start; //row to start on.
var $pages; //total pages.
/*
Access: priavte
*/
var $_db; //dBal reference
var $_page_var; //request var
var $_page; //current page
var $_row_count; //totalrows in complete query.
var $_query = false; //sql query to full resultset
var $_per_page; //records per page.
var $_link_format; //the format for pag links
var $_res; //db result
/*Class constructor
args
dbconnection: A valid database connection resource constructed by mysql_connect.
page_var: the request var hence 'page' woul;d be $_GET['page'];
link_format: the format to make the links %p is where the page number goes hence ?page=%p or /page-%p.html
path: the path relative or absolute to this script. Default current directory. Include follaing slash
page: The page to display, 0 based hence page =0 is page 1
per_page: number of records to show per page
*/
function pagintation($db_connection, $per_page=15, $path='./includes/', $page_var='page', $link_format='?page=%p')
{
if (!is_resource($db_connection))
{
echo 'Pagintation Error: Could not make connection to database';
exit; //No need to do anything more kill the script no database connection made.
}
if (!file_exists($path.'dbal/mysql/dBal.php'))
{
echo 'Pagintation Error: dBal.php not found';
exit; //No need to do anything more kill the script no database connection made.
}
require_once $path.'dbal/mysql/dBal.php';
$page = (isset($_GET[$page_var])) ? $_GET[$page_var] : $page;
$this->_db = new pag_dBal($db_connection);
$this->_page = $page;
$this->_per_page = $per_page;
$this->_page_var = $page_var;
$test = str_replace('%p', $_GET[$page_var], $link_format);
$test2 = 'http://test.com'.$_SERVER['REQUEST_URI'];
$parts = parse_url($test2);
$tmp = explode('&', $parts['query']);
foreach($tmp as $tmp2)
{
$tmp3 = explode('=', $tmp2);
$query[$tmp3[0]] = $tmp3[1];
}
unset($query[$page_var]);
if (count($query))
{
unset($tmp);
foreach ($query as $key => $value)
{
$tmp[] = "$key=$value";
}
$str = implode('&', $tmp);
if (strpos($link_format, '?') !== false)
{
$link_format = str_replace('?', '&', $link_format);
$link_format = '?'.$str.$link_format;
}
}
$this->_link_format = $link_format;
}
/*
args
sql: the sql statement to return all the rows for the entire query
Returns a mysql result.
Usage:
$res = $obj->query('Select * From my_table Where this_col = 1');
while ($row = mysql_fetch_assoc($res))
{
//write out your record display
}
*/
function query($sql)
{
$sql = str_replace(';', '', $sql);
$res = $this->_db->query($sql);
$row_count = $this->_db->num_rows($res);
$this->pages = ceil($row_count / $this->_per_page) - 1;
$this->_row_count = $row_count;
//reset the page to max page if page number is greater then the number of pages
$this->_page = ($this->_page > $this->pages) ? $this->pages : $this->_page;
//reset the page number to 0 if the pagenumber is negative
$this->_page = ($this->_page < 0) ? 0 : $this->_page;
//multiply page times per page and subtract 1 because start is 0 based
$this->start = ($this->_per_page * $this->_page);
//if page was 0 then start will be -1 so we change this to 0
$this->start = ($this->start < 0) ? 0 : $this->start;
//add the limits
$sql .= ' Limit '.$this->start.', '.$this->_per_page;
//store the query
$this->_query = $sql;
//store the result
$this->_res = $this->_db->query($sql);
//return the result
return $this->_res;
}
//return one row from the result
function get_row()
{
return $this->_db->fetch_assoc($this->_res);
}
function get_records($text_class='')
{
$output = " Showing records: ". ((int)$this->start + 1);
$last = ($this->_page * $this->_per_page) + $this->_per_page;
$last = ($last > $this->_row_count) ? $this->_row_count : $last;
$output .= ' Through '. $last . ' of '. $this->_row_count .'';
return $output;
}
/*
args
hide: hide first prev if on first page next last on last page
fl: show first and last
prev text: Previous text link
next_text: Next Text Link hence Next >
link class: the css class for links
text class: the css class for text
Returns (string)
First Previous 1 2 3 4 5 Next Last
*/
function get_fp_pn_nl($hide=false, $fl=true, $prev_text='Previous',$next_text='Next', $link_class='', $text_class='')
{
$output = $this->get_first_prev($hide, $fl, $prev_text, $link_class, $text_class).
$this->get_page_numbers($link_class, $text_class).
$this->get_next_last($hide, $fl, $next_text, $link_class, $text_class);
return $output;
}
/*
args
link class: the css class for links
text class: the css class for text
fl: show first and last
prev text: Previous text link
next_text: Next Text Link hence Next >
Returns (string)
First Previous
*/
function get_first_prev($hide=false, $fl=true, $prev_text='Previous', $link_class='', $text_class='')
{
$this->_check_valid();
if ($this->_page != 0)
{
if ($fl)
{
$output .= "_get_number(0)."\" class=\"$link_class\">First ";
}
$output .= "_get_number($this->_page - 1) ."\" class=\"$link_class\">$prev_text ";
}
elseif (!$hide)
{
if ($fl)
{
$output .= "First ";
}
$output .= "$prev_text ";
}
return $output;
}
/*
args
base_uri: the url base to use for pagintation links
link class: the css class for links
text class: the css class for text
Returns (string)
Next Last
*/
function get_next_last($hide=false, $fl=true, $next_text='Next', $link_class='', $text_class='')
{
$this->_check_valid($base_uri);
if ($this->_page != $this->pages)
{
//return links
$output .= "_get_number($this->_page + 1) ."\" class=\"$link_class\">$next_text ";
if ($fl)
{
$output .= "_get_number($this->pages) . "\" class=\"$link_class\">Last";
}
}
elseif (!$hide)
{
//return text
$output .= "$next_text ";
if ($fl)
{
$output .= "";
}
}
return $output;
}
/*
args
base_uri: the url base to use for pagintation links
link class: the css class for links
text class: the css class for text
Returns (string)
1 2 3 4 5 6 7 8 9 10
*/
function get_page_numbers($link_class='', $text_class='')
{
$this->_check_valid($base_uri);
$output = '';
for ($x = 0; $x <= $this->pages; $x++)
{
if (($this->_page)!= $x)
{
//return links
$output .= "_get_number($x) ."\" class=\"$link_class\">" . ($x+1) ." ";
}
else
{
//return text
$output .= "". ($x + 1) ." ";
}
}
return $output;
}
/*
Returns (string)
Prev | 2 | Next
*/
function short_with_num()
{
$this->_check_valid($base_uri);
$output =
$this->get_first_prev(true, false, 'Prev') . '| ' . ($this->_page + 1) .' | '.
$this->get_next_last(true, false, 'Next').'
';
return $output;
}
/*
Returns (string)
Combobox with javascript form submittal
*/
function get_combo($combo_class='')
{
?>
}
/*
args
base_uri: the url base to use for pagintation links
before: number of page numbers to show before current page
after: number of pages to display after the current page
link class: the css class for links
text class: the css class for text
Returns (string)
1 2 3 4 5 6 7 8 9...100
This method was never completed, I may complete it at a later day.
*/
/*
function get_pages_mid_trun($base_uri='', $before=2, $after=6, $link_class='', $text_class='')
{
if (($before + $after + 2) < $pages)
{
return get_page_numbers($base_uri, $link_class, $text_class);
}
$this->_check_valid($base_uri);
$s1 = $this->_page - $before;
$s1 = ($s < 1) ? 1 : $s1;
$e1 = $this->_page + $after;
$e1 = ($e1 > $this->pages) ? $this->pages : $e1;
$l1 = ($e1 != $this->pages) ? $this->pages : 0;
$tmp[] = '';
for ($x = 0; $x < $this->pages; $x++)
{
if ($this->_page != $x)
{
//return links
$tmp[] = "".$x+1 ." ";
}
else
{
//return text
$tmp[] = "".$x+1 ." ";
}
}
return $output;
}
*/
/*
Returns (string)
Returns a css stylable table with all the results in your query.
*/
function get_table($table_class='', $class_prefix='')
{
//lets not booger the orginal result returned by $obj->query
$res = $this->_db->query($this->_query);
$row = $this->_db->fetch_assoc($res);
?>
foreach ($row as $key => $value) {
?>
|
}
?>
$res = $this->_db->query($this->_query);
while ($row = $this->_db->fetch_assoc($res))
{
?>
foreach ($row as $key => $value) {
?>
|
}
?>
}
?>
}
/*
Private Methods
*/
function _check_valid()
{
if (!is_resource($this->_res))
{
echo 'Pagintation Error: No query pulled pagintation not possible.';
exit; //No need to do anything more kill the script no query was pulled
}
return $base_uri;
}
function _get_number($page)
{
$output = str_replace('%p', $page, $this->_link_format);
return $output;
}
}
?>
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag ''
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line ''
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag ''
$current = &$parent[$level-1];
}
}
return($xml_array);
}
?>
class S {
private static function _proc($regex, $s)
{
return addslashes(preg_replace("~{$regex}~is", '', $s));
return $s;
}
public static function toString (&$s)
{
$s = (string) addslashes($s);
return $s;
}
public static function toMd5 (&$s)
{
$s = (string) md5($s);
return $s;
}
public static function toInt (&$s)
{
$s = (int) self::_proc('[^0-9]+', $s);
return $s;
}
public static function toDouble (&$s)
{
$s = (double) self::_proc('[^0-9\.]+', $s);
return $s;
}
public static function toBool (&$s)
{
$s = (bool) ($s == 'true' || $s == true || $s == 1) ? true : false;
return $s;
}
public static function toPhone(&$s, $strip_1=true, $make_arrays=false)
{
$s = self::_proc('[^0-9]+', $s);
if ($strip_1 || $make_arrays)
{
$s = self::_proc('^1', $s);
}
if ($make_arrays)
{
preg_match('~^(\d{3})(\d{3})(\d{4})$~is', $s, $match);
$s = array(
$match[1],
$match[2],
$match[3]
);
}
return $s;
}
public function validEmail($s)
{
return (bool) (preg_match('~[a-z0-9\-\.]+@[a-z0-9\-\.]+\.[a-z0-9]+~is', $s));
}
}
?>