• Hi

    I am writing a plugin which very simply loads data from a table in the database and displays it in a table on the frontend.

    At the top of my table I have several dropdown boxes which are used to filter the data.

    How do I go about reloading and updating the SQL query each time the dropdown boxes are changed?

    I’m very new to developing in PHP so if any of my code is incorrect then please feel free to point it out to me.

    Thanks

    function renderswaplist() {
    
    	$output = '<table>
    	  <tr>
        <td><select name="baseSearch" id="baseSearch"><option value="" selected="selected">All</option></select></td>
        <td><select name="roleSearch" id="roleSearch"><option value="" selected="selected">All</option></select></td>
    	<td><select name="dateSearch" id="dateSearch"><option value="" selected="selected">Any</option></select></td>
        <td><select name="lengthSearch" id="lengthSearch"><option value="" selected="selected">Any</option></select></td>
    	<td><select name="timeSearch" id="timeSearch"><option value="" selected="selected">Any</option></select></td>
    	<td></td>
    	<td></td>
    	<td></td>
      </tr>
      <tr>
      	<th>Base</th>
    	<th>Role</th>
        	<th>Date</th>
        	<th>Days</th>
        	<th>Check In</th>
        	<th>Route</th>
        	<th>Staff Number</th>
        	<th>Comments</th>
      </tr>';
    
      global $wpdb;
    
      $tablename = $wpdb->prefix . "swaplist";
    
      foreach( $wpdb->get_results("SELECT * FROM $tablename;") as $key => $row) {
    		$crewnumber = $row->crewnumber;
    		$role = $row->role;
    		$base = $row->base;
    		$date = $row->startdate;
    		$length = $row->length;
    		$checkin  = $row->checkin;
    		$route =  $row->route;
    		$comments = $row->comments;
    		$formatteddate = date( 'd M', strtotime($date));
    		$formattedcheckin = date( 'H:i', strtotime($checkin));		
    
    		$output .= "<tr>
    	<td>$base</td>
    	<td>$role</td>
    	<td>$formatteddate</td>
    	<td>$length</td>
    	<td>$formattedcheckin</td>
    	<td>$route</td>
    	<td>$crewnumber</td>
    	<td>$comments</td>
      </tr>";
    
    }
    
    $output .= '</table>';
    
    	return $output;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Two different solutions:

    A) Use javascript to manage the controls within the browser, and have it use AJAX to relay the request to the server and display the refreshed query results.

    B) Enclose your table in a form, have a button labelled “REFRESH” that does a submit with your field values. The URL accesses a custom page template. The custom page template has default values for the various fields so that its initial load is sane.

    I have used both approaches, you will be able to find examples in the plugins library online here.

    Thread Starter guy.joseph

    (@guyjoseph)

    Hi Ross

    Thanks for your reply. Are you able to point me in the direction of any examples, I’ve been struggling to find one?

    Thanks

    I can’t find right away the example I used as a template, but this one does all you asked for and more. Pity that it might be more complex than you need. Suggest that you install it so that you can see how it works in your browser inspector.

    https://ww.wp.xz.cn/plugins/abase/

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Reload shortcode on dropdown box changed’ is closed to new replies.