Jump to content

Custom Search Based upon User selection with multiple number of Select boxes.


bhanu507

Recommended Posts

Hello,

 

I had 8 select boxes(dropdowns) in a form. Its like a search kind of implementation in the website. I don't know what the user selects, he may choose different select boxes, any number of select boxes.

 

So, based upon the user selection, I need to generate the data. The data fields to be generated would be almost same.

 

So, How would I know what the user selection is and how many select boxes has been selected and how could I generate different data based upon selected boxes.

 

I had to make a small note abt this, that the data generation fields may change for some of the user select combinations, but most of the result fields would be same.

 

so, can you please help me out, how to do, how to make different combination data generations, because, i had 8 fields, i had to make 8! combinations, that would result in a big code.

Hello,

 

I had 8 select boxes(dropdowns) in a form. Its like a search kind of implementation in the website. I don't know what the user selects, he may choose different select boxes, any number of select boxes.

 

So, based upon the user selection, I need to generate the data. The data fields to be generated would be almost same.

 

So, How would I know what the user selection is and how many select boxes has been selected and how could I generate different data based upon selected boxes.

 

I had to make a small note abt this, that the data generation fields may change for some of the user select combinations, but most of the result fields would be same.

 

so, can you please help me out, how to do, how to make different combination data generations, because, i had 8 fields, i had to make 8! combinations, that would result in a big code.

i had 8 fields, i had to make 8! combinations, that would result in a big code.

 

use loops friend.

 

 

 

but can you help me out, how to use loops, because, if we use loops, then we need to write code to 8!combinations

set a default option value for the first option of every box then check for that option, eg if the value of the select box equals first option do nothing and move on, if value of select box does not equal first option then do something.

 

I usually set the first option to something like --Select--

$select=$_POST['select'];
foreach ($select AS $selected => $value) {
if ($value!='--Select--') { do something }
}

 

$value will be the value of the selected box, if you name all your boxes select[somevar], this will create an array to loop through.

 

$selected with be the somevar you put in the square brackets to uniquely identify each box

in the do something you can create an array of the results you get and use that

eg

 

$result[$selected]=$data

 

each time it loops through for the selected items it will add to the array and it will use $selected as the key for each result and $data will be the value of data you want to output

hey, but, actually, i want to know the user selection and based upon usser selection, i need to generate table with some number of fields, so the table may change upon user's different kind of selections.

 

thats was the main issue actually.

$selected is the name of the select box the user selected and $value is the option the user selected.

I am not sure what data you want to generate I am assuming it will come from a database, so in the foreach loop you will have to get your data from the database based on the $value result. If you want to put it in a table you will need to have the <table> foreach ($select AS $selected => $value) { do mysql query based on $selected and $value,  then echo "<tr><td>some data</td><td>some more data</td></tr>"; } </table> like that

each loop will then generate a table row with your data based on the users selections

 

without seeing code of what you are trying to do I cant be more specific

hey, " dragon_sa" , sorry, as I don't know your name, I am using your id to address you...

 

I will try to explain you with an example kind of thing...like for suppose in a form , I had some select dropdown boxes. lets say...

 

1) cars

 

2) Bykes

 

3) Trucks

 

4)Engine capacity

 

and if the user selects cars, it will show up different cars from wide range of car manufacturing companies, and then the user will select the car, later when user submits the forms, it will be redirected to another form where, actually, it will show up the car price, engine capacity, mileage, type of car..etc..so, if the user selects cars and engine capacity at a same time, then it will return another table with different fields in it, hope you got me point

 

In this way I want to generate data with different combinations like, cars and engine capacity, cars & bykes&enginecapacity, trucks and enginecapacity, and trucks and cars only,likewise, we can form 4! combination, and for this 4! combinations, we need to generate data with different fields

 

and in my case, it was 8 fields....so think how the code would be and what would be the number of combinations

 

 

 

\

 

 

 

 

 

 

 

 

 

 

 

Now you got me... its exactly what i am looking for, I am able to update the next box, but look into the website, where if we select only car type as some type , lets say"audi", then just click submit, then in another form, you will be generating data showing different audi cars ,, with different types and price list... the same kind of way, I want to generate data in another form , where using the selection of the user, i need to present data to the end user related to the user selection.

This is done with AJAX

I have a demo here for selecting from a list of countries that will then update a list of states in the next select box.

If you look at it carefully you should be able to adapt it.

 

you need to set your doc type at the top of your page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

 

the ajax script you will need to modify to suit is like this

<script language="javaScript" type="text/javascript">
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}

	return xmlhttp;
    }

function getState(codeId)
{
   var strURL="statecall.php?countrycode="+codeId;
   var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
      if (req.readyState == 4)
      {
 // only if "OK"
 if (req.status == 200)
         {
    document.getElementById('statediv').innerHTML=req.responseText;
 } else {
   	   alert("There was a problem while using XMLHTTP:\n" + req.statusText);
 }
       }
      }
   req.open("GET", strURL, true);
   req.send(null);
   }
}
</script>

 

The country select box or first select box looks like this

Country:  <select name="country" id="country" onchange="getState(this.value);">
    		<option value="--Please Select Country--">--Please Select Country--</option>
	<?php  // print category combo box
		$sql = "SELECT name,ccode FROM countries ORDER BY name ASC";
		$result = mysql_query($sql);
		while ($result_row = mysql_fetch_array($result)) {
		$cname = $result_row["name"];
		$cid = $result_row["ccode"];
		echo "<option value='$cid'";
		if (($_POST['country']) == ($cid)) { echo "selected";
		}
		echo ">$cname</option>\n";
		}
		?>
  </select>

This will change the contents of a Div tag I have with id='statediv'

<div id="statediv"><?php include('statecall.php'); ?></div>

 

and the statecall.php page I have looks like this

<select name="state" id="state">
<option value="--Please Select State/Province--">--Please Select State/Province--</option>
<?php  
include('data_connect.php');
// print state select box
// convert country code to country name
$countrycode=$_GET['countrycode'];
$usercountry=$_GET['usercountry'];
  if (!$countrycode) { $countrycode = $usercountry; }
  if ($contactCode) { $countrycode = $contactCode; }
  $getcnty = "SELECT * FROM countries WHERE ccode='".$countrycode."'";
  $resultcnty = mysql_query($getcnty);
  $selectcnty = mysql_fetch_array($resultcnty);
  $cntyname = $selectcnty["name"];
		$querystate = "SELECT * FROM states WHERE code='".$countrycode."' ORDER BY nameorder";
		$results = mysql_query($querystate);
		while($row_result = mysql_fetch_array($results)) {
		$statename = $row_result["name"];
		if ($statename) {
		if ($statename!=$userstate) { echo "<option value='".$statename."'>".$statename."</option>\n";
		}
		}
		}
		if (!empty($userstate)) { echo "<option value='".$userstate."' selected='selected'>".$userstate."</option>\n";
		}
		if (!$statename) {
		if (!empty($cntyname)) {
		echo "<option value='".$cntyname."' selected='selected'>".$cntyname."</option>\n";
		}
		}
?>
</select>

you will basically need to do that for each select box

 

when your form is submitted you use the same conditions as I posted first up to process it

 

 

hmmmmm... again we came to start, i think u didn't got my point, as i said in the previous one like, I am able to generate or populate data in the select boxes, like as you have shown me, but i want to generate data, based upon the user selection. like, if he select only 3 select options, and then I need to generate data based upon the user selected fields, here my main concern is abt generating data in another form, depending upon the user selection.

 

see, if he select 2 boxes, then one type of report will be generated, and if he select 5 boxes at a time, then another type of report with different fields woud be generated. thats what i want, hope you got me, how to nullify 8! combinations,( max probability)

The statecall.php for example would be one of your scripts used to populate the box, you would have a similar script for each box, the user selects one, a few or all of the options, but the options in the sequential select boxes dont become available until the user selects the previous 1.

Each time the user selects a box AJAX queries a database based on the selection and populates the next select box with options.

When the user eventually hits the submit button selecting 1 or more select boxes this is when you process based on the selections and display the data based on those selections.

 

thats how carsales.com.au site works

hey, i want to make a note with you, only 3 out of 8 fields are dependent on each other, and the user may select one field mandatorily from those 3 fields, and he may or may not select a field from remaning 5 fields. if he selects, both selections , we need to generate the data, otherwise, generate another data......

 

 

 

by having the option --select-- at the start of each box that is your trigger to see if a box has been selected, you can use the ajax script on the 3 boxes in question, and only include the ajax script on any other boxes that may require a select input otherwise just supply your default data from the database for those 5 boxes, they could even interact with each other the same as the first 3 boxes do, but not rely on what is selected in the first 3, it depends on what data you want to achieve.

hey , thanks for suggesting, now you got me, can i have a code kind of thing of your explanation, so that i would try to understand and rather it would be easy to work on it...plz can you plz write a code, with 6or 7 fields,

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.