Support » Plugins » Developing a plugin – link to pages

  • I am a beginner to WP plugin development and PHP. I am developing (or trying to develop) a plugin. My main page (called merchants- code below) is working as expected. My need is to produce search results in the same or another page when you click the Search button. Details as follows:

    Plugin Name: Merchants
    WP Version : 3.7.1
    Web Server: WAMP 2.2, Localhost

    Database:

    -- Table structure for table <code>directory</code>
    
    CREATE TABLE IF NOT EXISTS <code>directory</code> (
      <code>directory_id</code> int(5) NOT NULL AUTO_INCREMENT,
      <code>merchant</code> varchar(50) NOT NULL,
      <code>category</code> varchar(50) NOT NULL,
      <code>area</code> varchar(50) NOT NULL,
      PRIMARY KEY (<code>directory_id</code>)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
    
    -- Dumping data for table <code>directory</code>
    
    INSERT INTO <code>directory</code> (<code>directory_id</code>, <code>merchant</code>, <code>category</code>, <code>area</code>) VALUES
    (1, 'Acapps', 'IT Services', 'Cape Town CBD'),
    (2, 'Octagon Property', 'Real Estate', 'Green Point'),
    (3, 'JFM Solutions', 'IT Services', 'Bellville'),
    (4, 'Lollas Cleaning', 'Health & Hygiene', 'Belhar'),
    (5, 'Genana B C', 'IT Services', 'Belhar'),
    (6, 'Pick n Pay', 'Supermarket', 'Green Point');

    PHP CODE

    The main page is called merchants.php:

    <?PHP
    
    /*
    Plugin Name: Merchants
    Description: Custom plugin to manage merchants on my future website
    
    */
    
    function list_merchants(){
    	global $wpdb;
    
    	//Get specified fields from DB
    	$merchants	=	$wpdb->get_results("SELECT * FROM directory");
    
    	//Create a search form, start populating variable $show for shortcode implementation later
    	$show	=	"<form method='POST' action='http://localhost/lab/search.php'>";
    
    	$show	.=	"<select name='areas'>
    					<option>Belhar<option>
    					<option>Bellville<option>
    					<option>Cape Town CBD<option>
    					<option>Green Point<option>
    				</select>";
    
    	$show	.=	"<input type='submit' value='Search' /></form>";
    
    	//Create a table to show results
    	$show	.=  "<table cellspacing='5' cellpadding='5' border='2'>";
    
    	$show	.=	"<th>COMPANY</th>	<th>CATEGORY</th>	<th>AREA</th>";
    
    	//walkthrough array $merchant to display all merchants in DB
    	foreach( $merchants AS $merchants){
    
    		$show .=	"<tr><td>" . $merchants->merchant . "</td><td>" . $merchants->category . "</td><td>" . $merchants->area . "</td></tr>";
    
    	}
    
    	$show	.=	"</table>";
    
    	return $show;
    }
    
    add_shortcode( 'merchants', 'list_merchants');
    
    ?>

    ..and finally the code I am attempting to get result from (named search.php)

    <?PHP
    
    function search_by_area(){
    	global $wpdb;
    
    	//Get results matching keyword
    	If (ISSET($_POST['areas'])){
    		$keyword	=	$_POST['areas'];
    		$merchants_result	=	$wpdb->get_results("SELECT * FROM directory WHERE area WHERE area REGEXP '$keyword' ORDER BY merchant");
    	}
    
    	Else {
    		$merchants_result	=	$wpdb->get_results("SELECT * FROM directory");
    		}
    
    	//Create a table to show results
    	$show_result	.=  "<table cellspacing='5' cellpadding='5' border='2'>";
    
    	$show_result	.=	"<th>COMPANY</th>	<th>CATEGORY</th>	<th>AREA</th>";
    
    	//walkthrough array $merchant to display all merchants in DB
    	foreach( $merchants_result AS $merchants_result){
    
    		$show_result .=	"<tr><td>" . $merchants_result->merchant . "</td><td>" . $merchants_result->category . "</td><td>" . $merchants_result->area . "</td></tr>";
    
    	}
    
    	$show_result	.=	"</table>";
    
    	return $show_result;
    }
    
    //add_shortcode( 'search', 'search_by_area');
    
    ?>

    Remember the goal is to show results in the same or another page when you click on my Search button. Your contribution is really appreciated.

  • The topic ‘Developing a plugin – link to pages’ is closed to new replies.