AJAX Search class that produces JSON

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

AJAX Search class that produces JSON

Post by Luke »

I need to build a class(es) that can search my database for locations, and return json objects. Currently I have a controller with a lot of repetative code. I believe a class or a few classes would clean this code up nicely, but I'm having a hard time thinking of how to put the class together. Here's what it needs to do:

Search database by the following columns: type, corridor, description (fulltext search), and keywords (also fulltext search).
It also needs to be able to page through the results, and have the ability to just page through the ENTIRE database as well. Here is my code right now... without a class.

Code: Select all

<?php
class mapController extends appController{
	public function findAction(){

		$output = '';
		$Business = new Business;
		$sortable = array('name', 'address', 'created');

		// Build pagination
		$params = $this->_action->getParams();
		$showpage = isset($params['showpage']) ? $params['showpage'] : 0;
		$orderby  = isset($params['orderby']) && in_array($params['orderby'], $sortable) ? $params['orderby'] : 'name';
		$ascdesc  = isset($params['ascdesc']) && $params['ascdesc'] == 'desc' ? 'desc' : 'asc';

		$perpage = $this->config->view->perpage;
		$gettotal = $this->db->query('SELECT COUNT(*) AS `total` FROM `' . $Business->getTable() .'` WHERE `temporary` = 0 AND `suspended` = 0');
		$total_pages = ceil($gettotal->fields['total'] / $perpage);
		$start = $showpage ? ($showpage * $perpage) - $perpage : 0;
		
		$businesses = $Business->find('`temporary` = 0 AND `suspended` = 0 ORDER BY `' . $orderby . '` ' . $ascdesc . ' LIMIT ' . $start . ',' . $perpage);

		foreach($businesses as $business){
			if($business->loadPoint()){
				$business_data = array(
					'point' => $business->point->_original,
					'business' => $business->toArray()
				);
			}
			$output[] = $business_data;
		}
		Zend::loadClass('Zend_Json');
		$json = Zend_Json::encode($output);
		echo $json;
	}
	public function findtypeAction(){
		$output = '';
		$Business = new Business;

		$params = $this->_action->getParams();
		$type = isset($params['type']) ? $params['type'] : 'business';

		$businesses = $Business->find('`type` = "' . $type . '" AND `temporary` = 0 AND `suspended` = 0');

		foreach($businesses as $business){
			if($business->loadPoint()){
				$business_data = array(
					'point' => $business->point->_original,
					'business' => $business->toArray()
				);
			}
			$output[] = $business_data;
		}
		Zend::loadClass('Zend_Json');
		$json = Zend_Json::encode($output);
		echo $json;
	}
	public function findcorridorAction(){
		$output = '';
		$Business = new Business;

		$params = $this->_action->getParams();
		$corridor = isset($params['corridor']) ? $params['corridor'] : 'hwy70';

		$businesses = $Business->find('`corridor` = "' . $corridor . ' AND `temporary` = 0 AND `suspended` = 0"');

		foreach($businesses as $business){
			if($business->loadPoint()){
				$business_data = array(
					'point' => $business->point->_original,
					'business' => $business->toArray()
				);
			}
			$output[] = $business_data;
		}
		Zend::loadClass('Zend_Json');
		$json = Zend_Json::encode($output);
		echo $json;
	}
	public function searchAction(){

		$output = '';
		$Business = new Business;

		// Build pagination
		$params = $this->_action->getParams();
		$showpage = isset($params['showpage']) ? $params['showpage'] : 0;
		$keywords = mysql_real_escape_string($_GET['keywords']);

		$perpage = $this->config->view->perpage;
		$gettotal = $this->db->query('SELECT COUNT(*) AS `total` FROM `' . $Business->getTable() .'` WHERE MATCH (keywords, description) AGAINST ("' . $keywords . '") AND `temporary` = 0 AND `suspended` = 0;');
		$total_pages = ceil($gettotal->fields['total'] / $perpage);
		$start = $showpage ? ($showpage * $perpage) - $perpage : 0;
		
		$businesses = $Business->find('MATCH (keywords,description) AGAINST ("' . $keywords . '") AND `temporary` = 0 AND `suspended` = 0 LIMIT ' . $start . ',' . $perpage);

		foreach($businesses as $business){
			if($business->loadPoint()){
				$business_data = array(
					'point' => $business->point->_original,
					'business' => $business->toArray()
				);
			}
			$output[] = $business_data;
		}
		Zend::loadClass('Zend_Json');
		$json = Zend_Json::encode($output);
		echo $json;
	}
}
?>
What I'm asking is that somebody just kind of give me an idea of what the class(es) might look like. Any help is appreciated.

Oh yea... and I would also like to lose the models in this class. Since we're only searching with the 'business' model, I'd like to just do all the sql queries manually... since they would be faster, and I do not need every column returned from the database.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Minor aside: Is Zend's JSON encoder any good? I've written my own very basic one, but I could be tempted to switch.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Zend's JSON encoder/decoder work but there are a few outstanding issues AFAIK. The dev wiki should list them.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Read up on the specification pattern (PDF LINK). It's a long read but well worth it.
Post Reply