Login   Register  
PHP Classes
elePHPant
Icontem

File: ez_select.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Pineapple Technologies  >  EZ Select  >  ez_select.php  >  Download  
File: ez_select.php
Role: ???
Content type: text/plain
Description: The class itself.
Class: EZ Select
Author: By
Last change:
Date: 12 years ago
Size: 3,957 bytes
 

Contents

Class file image Download
<?php

class ez_select
{
	var $fields	= array();	// fields to select
	var $tables	= array();	// tables to select from
	var $where	= array();	// WHERE clauses
	var $order_by	= array();	// fields for ORDER BY clause
	var $group_by	= array();	// fields for GROUP BY clause
	var $query;			// the actual, constructed query
	var $order_type;		// ASC or DESC
	var $have_limit;		// whether or not to put a limit on this query
	var $limit_offset;		// what record to start on for a limit
	
	// how many records to get
	var $limit_num_records;
	
	function select_query()
	{
		// nothing yet
	}

	function add_field($fields)
	{
		// adds fields from which we'll be selecting stuff
		$this->fields[] = $fields;
	}
	
	function add_table($table)
	{
		// adds a table from which to select results
		$this->tables[] = $table;
	}
	
	function add_where_clause($clause)
	{
		// adds a where clause to the query
		$this->where[] = $clause;
	}
	
	function add_order_by($field)
	{
		// adds a field to order by
		$this->order_by[] = $field;
	}
	
	function add_group_by($field)
	{
		// adds a field to group by
		$this->group_by[] = $field;
	}
	
	function set_order_type($type)
	{
		// sets order type (ASC or DESC)
		$this->order_type = $type;
	}
	
	function set_limit($offset=0, $num_records)
	{
		// adds a limit to the query
		$this->have_limit = true;
		$this->limit_offset = $offset;


		$this->limit_num_records = $num_records;
	}

	function show()
	{
		// shows the query so far, color coded
		
		// a select query...
		$query = '<b>SELECT</b> ';
		
		// fields to select, comma separated
		$query .= '<font color=blue>';
		$query .= implode(', ', $this->fields);
		$query .= '</font>';
		
		// tables to select from
		$query .= ' <b>FROM</b> ';
		$query .= '<font color=green>';
		$query .= implode(', ', $this->tables);
		$query .= '</font>';

		// if there are any where clauses
		if( count($this->where) )
		{
			$query .= ' <b>WHERE</b> <font color=red>';
			$query .= implode(' </font><b>AND</b><font color=red> ', $this->where);
			$query .= '</font>';
		}
		
		// if there are any fields to group by
		if( count($this->group_by) )
		{
			$query .= ' <b>GROUP BY</b> <font color=orange>';
			$query .= implode(', ', $this->group_by);
			$query .= '</font>';
		}
		
		// any fields to order by
		if( count($this->order_by) )
		{
			$query .= ' <b>ORDER BY</b> <font color=purple>';
			$query .= implode(', ', $this->order_by);
			$query .= '</font>';
			
			$query .= ' <b>' . $this->order_type . '</b>';
		}

		// a limit
		if($this->have_limit)
		{
			$query .= " <b>LIMIT</b> <font color=magenta>$this->limit_offset, ";
			$query .= $this->limit_num_records;
			$query .= '</font>';
		}
	
		echo "<br>" . $query;

	} // end function show()
	
	function make()
	{
		// creates and returns the query
		
		// a select query...
		$this->query = 'SELECT ';
		
		// fields to select, comma separated
		$this->query .= implode(',', $this->fields);
		
		// tables to select from
		$this->query .= ' FROM ';
		$this->query .= implode(',', $this->tables);
		
		// if there are any where clauses
		if( count($this->where) )
		{
			$this->query .= ' WHERE ';
			$this->query .= implode(' AND ', $this->where);
		}
		
		// if there are any fields to group by
		if( count($this->group_by) )
		{
			$this->query .= ' GROUP BY ';
			$this->query .= implode(',', $this->group_by);
		}
		
		// any fields to order by
		if( count($this->order_by) )
		{
			$this->query .= ' ORDER BY ';
			$this->query .= implode(',', $this->order_by);
			
			$this->query .= ' ' . $this->order_type;
		}

		// a limit
		if($this->have_limit)
		{
			$this->query .= " LIMIT $this->limit_offset,";
			$this->query .= $this->limit_num_records;
		}
	
		return $this->query;

	} // end function make()

} // end class ez_select

?>