donovan78 Posted May 21, 2010 Share Posted May 21, 2010 Hi, I am trying to process this search in a MySQL database of gps tracks by category , MySQL table is that : CREATE TABLE `geo_routes` ( `id` int(11) NOT NULL auto_increment, `name` varchar(200) NOT NULL default '', `author` varchar(80) NOT NULL default '', `description` text NOT NULL, `category` varchar(100) NOT NULL default '', `property` int(11) NOT NULL default '0', `time` int(11) NOT NULL default '0', `downloads` int( NOT NULL default '0', `visits` int( NOT NULL default '0', PRIMARY KEY (`id`), FULLTEXT KEY `search` (`name`,`description`) ) ENGINE=MyISAM AUTO_INCREMENT=56 DEFAULT CHARSET=latin1 AUTO_INCREMENT=56 ; The multiple select form that is : <form action="<?php echo GEO_URL_ROOT.'/index.php?s=category'; ?>" method="POST"> <select multiple name="category[]" style="width:150px;height:260px;"> <option value="motor bike">Motor Bike</option> <option value="biking">Biking</option> <option value="mountain bike">Mountain Bike</option> <option value="trekking">Trekking</option> <option value="runner">Runner</option> <option value="off road">Off Road</option> </select> <br /> <br /> <input type="submit" name="submit" value="Submit"> </form> And the php script makes the MySQL query is that : function getRouteByCat($cat) { if(!is_array($cat)) return false; $query = "SELECT * FROM geo_routes"; $filters = array(); foreach ($cat as $c) { $filters[] ="category LIKE '%".$this->escape($c)."%'"; } $query.= (empty($filters)?'':' WHERE '.implode(' AND ',$filters)).' ORDER BY id ASC'; return $this -> query($query); } I need to select the routes in database by category from this form : motor bike,biking,mountain bike,trekking, runner,off road however the front-end displays the categories ALL together, grouped but not selected by genre. For example , if I select "trekking" in the form, this variable go to this script , who process the post : index.php <? elseif(!empty($_GET['s'])) { $cat = $_POST['category']; $cat =$database->getRouteByCat($cat); $category=$cat; switch($_GET['s']) { case 'name': $order = 'name ASC'; $title = 'Tours and Hotels'; break; case 'category': $order = '$category'; $order = 'category DESC'; $title = 'Category'; break; case 'top': $order = '(downloads*10 + visits) DESC'; $title = ' Rating'; break; default: $order = 'id DESC'; $title = 'Last Routes'; } ?> I don't know if i am using the wrong variable because the php script make this : http://trekhotel.com/bina/index.php?s=category Thanks for some help or suggestion ! Quote Link to comment Share on other sites More sharing options...
leehanken Posted May 21, 2010 Share Posted May 21, 2010 <select multiple name="category[]" style="width:150px;height:260px;"> What are the brackets for after the word 'category'? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 They turn the returned values into an array so that the multiple selections will be sent to the processing script. Ken Quote Link to comment Share on other sites More sharing options...
donovan78 Posted May 21, 2010 Author Share Posted May 21, 2010 Hi leehanken ! the brackets after "category" means array , that is array of categories. thanks. Quote Link to comment Share on other sites More sharing options...
leehanken Posted May 21, 2010 Share Posted May 21, 2010 Okay, I was not familiar with that, sorry for the question. $order = '$category'; $order = 'category DESC'; I noticed you set the same variable twice. Is this what you meant to do? Quote Link to comment Share on other sites More sharing options...
donovan78 Posted May 22, 2010 Author Share Posted May 22, 2010 Really i guess this variable defined twice is wrong however i change now : elseif(!empty($_GET['s'])) { $cat = $_POST['category']; $cat =$database->getRouteByCat($cat); $order=$cat; switch($_GET['s']) { case 'name': $order = 'name ASC'; $title = 'Tours and Hotels'; break; case 'category': $order = 'category DESC'; $title = 'Category'; break; And continues listing full categories NOT filtering by each kind , if you understand me ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.