Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filtering records using menu 1

Status
Not open for further replies.

cyberspace

Technical User
Aug 19, 2005
968
GB
I am currently in the process of refining an IT helpdesk system and I am trying to implement filtering.

At the minute, all calls for a user show up when they log in.

What I would like is for them to select an option from a drop down menu, and then only records matching the criteria will be shown.

I've coded this in PHP with the correct SQL statements, whereby it takes the value currently set in a list/menu and selects the certain records based on the if statements.

My question is...firstly...how do i force a page refresh when you choose a new option from the menu and secondly...and indeed more importantly...how do i get the page to remember that the option has been changed and only display the corresponding records?

At the minute, it works if i manually alter the default value for the list/menu.


Input appreciated, TIA.

'When all else fails.......read the manual'
 
take this select box:
Code:
<form method="post" action="somepage..php" name="selectForm">
<select name="selectbox">
<option value="a">a</option
<option value="b">b</option
<option value="c">c</option
</select>
</form>

to make it autosubmit you need a bit of js

Code:
<select name="selectbox" onchange="document.selectForm.submit();">

to make the selection "sticky" you need to use some php parsing to check the incoming value of the selectbox and output "selected" at the right time.

Code:
<?php
if (isset($_POST['selectbox'])){	//check that there is a submission
	$val = trim($_POST['selectbox']); //this assumes that magic_quotes is switched off
} else {
	$val = "a"; //the default value
}	
//create an assoc array of values for the select box
$selectvalues = array("a"=>"a", "b"=>"b", "c"=>"c");

//start the select box output
echo'<select name="selectbox" onchange="document.selectForm.submit();">\r\n';

//iterate through the values
foreach ($selectvalues as $value=>$text){
	
	//use ternary operator to set a value for $selected
	$selected =  ($value === $postval) ? 'selected="selected"' : '';
	echo "<option value=\"$value\" $selected>$text</option>\r\n";
}
echo '</select>';
?>
 
thanks jpadie!

This looks a little more complicated than I was expecting!

Just tried to get the form to auto-submit but its coming up with an error. All i did was paste in the line onchange="document.selectForm.submit();"

When i tried it, i got the error "document.selectForm is null or not an object"

Is there somewhere i need to say something like script=javascript? Or is there more code needed?

Sorry if that's an obvious question.

'When all else fails.......read the manual'
 
have you named the form selectForm as per the sample?

it's not complex - most of the steps you have to go through anyway, if you are loading the options from an array or a database. for example, if from a mysql db

Code:
$result = mysql_query("Select value, text from table");
$option = ''; //set up the variable
while ($row=mysql_fetch_assoc($result)){
  $selected = ($row['value'] === $_POST['selectbox']) ? 'selected="selected" : '';
  $option .= "<option value=\"$row['value']\" $selected>$row['text']</option"; 
}
echo "<select name=\"selectbox\" onchange=\"document.selectForm.submit();\">$option</select>";
 
yeah the form is named selectForm.

I must be doing somethng wrong...as a seperate menu appears with the values "a", "b" and "c"....

The page reloads now though..... but it's not holding the value :S

'When all else fails.......read the manual'
 
Well i solved it, this is the end result:

Code:
<select name="filter" id="filter" onchange="location.href=filter.options[selectedIndex].value">
            <option selected="selected">Filter...</option>
			<option value="home.php?filter_id=1" >All Issues</option>
			<option value="home.php?filter_id=2" >Outstanding Issues</option>
			<option value="home.php?filter_id=3" >Resolved Issues</option>
			
          </select>

thanks for your help

'When all else fails.......read the manual'
 
i doubt whether your solution provides a sticky selection as you have hard coded the selected="selected" argument.

try this code which mirrors yours in output (and includes some debugging)
Code:
<?
function getOptions(){
	//presets a variable to return the options
	$return = '';
	
	//this grabs the filterd from the query string
	if(isset($_GET['filter_id'])) {
		$filter=trim($_GET['filter_id']);
	}else{
		$filter="default";
	}
	
	//this is the array of options.  makes it easy to add new items later
	$optionsArray = array(
						array(	"value"=>"1",
								"text"=>"All Issues"),
						array(	"value"=>"2",
								"text"=>"Outstanding Issues"),
						array(	"value"=>"3",
								"text"=>"Resolved Issues")
						);
	
	//this moves through the array of options and 
	//first tests to see whether the option value is the same as the filter_id
	//if it is, then it sets the selected flag.  if not then it does not!
	//it also builds sequentially the <options> string itself
	foreach ($optionsArray as $option){
		$selected = $filter==$option['value'] ? ' selected="selected" ' : '';
		$return .= "\r\n\t\t<option value=\"".$option['value']."\"$selected>".$option['text']."</option>";
	}
	//return the options string to the calling code
	return $return;
}
function getText($val){
	$optionsArray = array(
						array(	"value"=>"1",
								"text"=>"All Issues"),
						array(	"value"=>"2",
								"text"=>"Outstanding Issues"),
						array(	"value"=>"3",
								"text"=>"Resolved Issues")
						);
	foreach ($optionsArray as $option){
		if ($option['value'] == $val) { return $option['text'];}
	}
}
?>
<? 
//debug only
if (isset($_GET['filter_id'])) {
echo '
<hr />
For debugging, you selected the option '.$_GET['filter_id'].' which corresponds to the option '. getText($_GET['filter_id']) . '.  This should be selected below.<hr/>';
}
//end debug
?>
<!-- 
this code uses more traditional html constructs such as a form.  
rather than forcing a redirect on a per-item basis
strictly speaking, for valid xhtml, form controls must be within
a fieldset or another block level element from a defined class
-->
<form action="home.php" method="get" id="FormFilter" name="FormFilter">
	<!--xhtml validity -->
	<fieldset style="border:none; padding:0px; margin:0px;">
	<!-- the javascript is very simple. it just says submit the form called FormFilter-->
	<select name="filter_id" id="filter" onchange="document.FormFilter.submit(); return true;"> 
	<!--
	this code calls the getOptions function from the php. the < ?= is a short hand notation that 
	includes implicitly the echo.  it could also be < ? echo getOptions(); ? >  
	the spacing between < and ? is put in for use in html only
	--> 
	<?=getOptions()?>    
	</select>
	</fieldset>
</form>
 
That's awesome jpadie!!

Really appreciate your help :)

'When all else fails.......read the manual'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top