By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

Some help with some code.

Chrisjc
100+
P: 375
I am hoping someone can help me out here. I am trying to make one drop down box pull from a database the DB name is “dealerlocater” the column it needs to pull in this drop down is called “state” Once it loads all the states in the drop down box it then needs to be able to do the following.

Once a user selects a state it will load all information listed in the ROWS of the state column I.E.

++++++++++++++++
+ Select Your State + Pulls from column “state” say they pick California
++++++++++++++++

Anything in the database with California in that column will then begin to display the rows.


Company Name:
Address:
City:
State abbreviation:
Zip:
Phone:
Website:

The database is set up like this

State company address city stateabb zip etc...
California Test Company 123 St. Test CA 99999
California Test Company 123 St. Test CA 99999
California Test Company 123 St. Test CA 99999
================================================== ======
So that is what the data file looks like and once they clicked on a state it will find all that match what they picked and display what ever rows I would list.

Displaying the rows is easy I just need help with the quey and the click on the state and it will search it and load whatever columns I tell it do that match with the state.

Any help would be great!!!

Here is something I tryed and its not working.

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3.  
  4. var div7 = '<p>&nbsp;</p>';
  5.  
  6. var ajax = new sack();
  7. var selstate;
  8.  
  9.  
  10. function buildtop() {
  11.     ajax.requestFile  = 'getCarInfo.php?buildtop=1';
  12.     ajax.onCompletion = makeTop;
  13.     ajax.runAJAX();
  14. }
  15.  
  16. function getPart(sel) {
  17.     selstate = sel.options[sel.selectedIndex].value;
  18.     if(selYear.length>0){
  19.         ajax.requestFile = 'getCarInfo.php?getpart=1&state='+selstae+';   
  20.         ajax.onCompletion = createPart;     
  21.         ajax.runAJAX();         
  22.     }
  23. }
  24. </script>
  25. </head>
  26.  
  27.  
  28. <?php
  29. // =========================================================
  30. //  Populate the State selection list from the database
  31. // =========================================================
  32. echo '<form action="" method="post">';
  33. echo '<select id="state" name="state" style="margin-bottom:4px;font-family: Tahoma; font-size: 10pt; height:28px;width:184px;" onchange="getList(this)">';
  34. echo '<option value="">Select Your State</option>';
  35. // =========================================================
  36. // Connection to the Database
  37. // =========================================================
  38. include ('db functions/db_connect.php');
  39. // =========================================================
  40. // SELECT year to make drop down list
  41. // =========================================================
  42. $res = mysql_query("SELECT state FROM dealerlocater WHERE 'state' GROUP BY state ORDER BY state")
  43.    or die("Invalid query: " . mysql_query());
  44. while ($row = mysql_fetch_assoc($res)) {
  45.    $st = $row['state'];
  46.    echo "<option value='$st'>$st</option>";
  47. }
  48. echo '</select>';
  49. ?>
  50.  
  51.  
  52.  
  53. <DIV ID="divmsg7">
  54.     </DIV>
  55.  
I belive using the AJAX is a waste... I think this can all be done on one page with out passing info am I correct?

If not I will need the help with the ajax...

Thank you

Chris
Jan 9 '08 #1
Share this Question
Share on Google+
1 Reply


Chrisjc
100+
P: 375
And here is the other page.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /*************************************************************************************
  3. 1. drop down STATE
  4. **************************************************************************************/
  5. // ===============================================================================
  6. // Load Config
  7. // ===============================================================================
  8. include ('config.php');
  9. // ===============================================================================
  10. // Connection to the Database
  11. // ===============================================================================
  12. include ('db functions/db_connect.php');
  13. // =====================================================================================================
  14. // Construct list overview after user selectes state.
  15. // =====================================================================================================
  16. if( (isset($_GET['getList']) AND isset($_GET['state'])){
  17.   if(isset($_GET['getList'])) {  
  18.      $st = $_GET['state']; 
  19.  
  20.  
  21.  
  22.      $query="SELECT dealerlocater.state, ".
  23.             " FROM dealerlocater ".
  24.             " WHERE state='$st' ".
  25.             " GROUP BY dealerlocater.state ".
  26.             " ORDER BY dealerlocater.state ";
  27.   }
  28. $res = mysql_query($query)
  29.   or die("Invalid query: " . mysql_query());
  30. // ===============================================================================
  31. // read the Categories table and build the colno array in max 4 entries
  32. // ===============================================================================
  33. $cat = mysql_query("SELECT * from dealerlocater GROUP BY state ORDER BY state")
  34.   or die("SELECT Categories failed: ".mysql_error());
  35. $catArray = array();
  36. $i=0;
  37. while ($rowcat = mysql_fetch_assoc($cat)) {
  38.   $colArray[$i] = $rowcat['colno'];
  39.   $i++;
  40. } // End WHILE
  41.  
  42. // This ends the table for the TOP part of the screen
  43. // ===============================================================================
  44. if (mysql_num_rows($res) > 0) {
  45.   // build the output array sorted by category
  46.   $outArray=array();
  47.   $k=0;
  48.   while ($row = mysql_fetch_assoc($res)) {
  49.     $st = $row['state'];    
  50.     $cp = $row['company'];
  51.     $ad = $row['address'];
  52.     $ci = $row['city'];    //if (is_null($ca)) $ca ='1';
  53.     $sb = $row['stateabb']; 
  54.     $zi = $row['zip'];     
  55.     $we = $row['web'];  
  56.  
  57.     $outArray[$ca][]=array('state'             => $st, 
  58.                            'company'         => $cp,
  59.                            'address'         => $ad,
  60.                            'city'              => $ci,    
  61.                            'stateabb'        => $sb,
  62.                            'zip'             => $zi,    
  63.                            'web'                => $we);
  64.   }
  65. }
  66. else {                          // no rows selected (mysql_num_rows == 0)
  67.   echo 'No results found for your search!';
  68. }
  69.  
Jan 9 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.