Populate dropdown using mysql date range

Reply

Join Date: Jun 2008
Posts: 23
Reputation: forwardlookguy is an unknown quantity at this point 
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

Populate dropdown using mysql date range

 
0
  #1
Oct 15th, 2008
Hello everyone, I'm having trouble populating a dropdown box with mysql data. I have a table with columns makeid, yearstart, yearend and makename. Sample data would be 1, 1936, 1941, American Bantam. I want the dropdown to list the years a certain brand of car was offered. For instance, a user selects American Bantam and the years 1936, 1937... 1940, 1941 are listed. I've tried a bunch of different things but I can't seem to get the dropdown to populate with anything more than just the start or end dates. I'm relatively new to PHP so I'm sure there is a simple solution.

Any help would be appreciated!

Thanks,
Arthur
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 40
Reputation: enim213 is an unknown quantity at this point 
Solved Threads: 2
enim213 enim213 is offline Offline
Light Poster

Re: Populate dropdown using mysql date range

 
0
  #2
Oct 15th, 2008
can you post your code for us to know what you want it would be?

im a newbie too .. perhaps i could help too

enim
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: Populate dropdown using mysql date range

 
0
  #3
Oct 15th, 2008
Originally Posted by forwardlookguy View Post
For instance, a user selects American Bantam and the years 1936, 1937... 1940, 1941 are listed.
If user selects name, then based on that id you can get two dates from your database table, then make a dropdown list by incrementing the first date, put if condition greater than end date...
try it...
or post appropriate code...then we can see it for validate....
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 23
Reputation: forwardlookguy is an unknown quantity at this point 
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

Re: Populate dropdown using mysql date range

 
0
  #4
Oct 16th, 2008
Well, I've actually got two pieces of code. The first one I'll post was given to me by someone else but I've managed to get it to work for my application. Here it is:

  1. <?php
  2.  
  3. require "includes/dbconnect.php";
  4.  
  5.  
  6. $query = "";
  7. if(isset($_GET['make']))
  8. {
  9. if(trim($query2) == "")
  10. {
  11. $query2 .= " where makename = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
  12. }
  13. else
  14. {
  15. $query2 .= " and makename = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
  16. }
  17. }
  18.  
  19. $query2 = "select * from makes" . $query2 . " order by makename";
  20. $searchresult = mysql_query($query2) or die(mysql_error());;
  21.  
  22. $query3 = "SELECT yearstart, yearend FROM makes";
  23. $yearresult = mysql_query($query3) or die(mysql_error());;
  24.  
  25.  
  26. $yeararray = array();
  27. while($row = mysql_fetch_assoc($yearresult))
  28. {
  29. $yeararray[$row["yearstart"]][] = $row["yearend"];
  30. }
  31. ?>
  32.  
  33. <table cellpadding="0" cellspacing="0">
  34. <?
  35. if(mysql_num_rows($searchresult) == 0 || !is_numeric(mysql_num_rows($searchresult)))
  36. {
  37. ?>
  38. <tr>
  39. <td colspan="3">No vehicles found under that search criteria.</td>
  40. </tr>
  41. <?
  42. }
  43. else
  44. {
  45. for($i = 0; $i < mysql_num_rows($searchresult); $i++)
  46. {
  47. ?>
  48. <tr>
  49. <td><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?make=<? echo urlencode(mysql_result($searchresult, $i, "makename")); ?>"><? echo mysql_result($searchresult, $i, "makename"); ?></a></td>
  50. <td>
  51. <form action="<? echo basename($_SERVER['PHP_SELF']); ?>" method="get">
  52. <input type="hidden" name="make" value="<? echo mysql_result($searchresult, $i, "makename"); ?>" />
  53. <select name="year" id="year<? echo mysql_result($yearresult, $i, "yearstart"); ?>">
  54. <option value="Choose">Choose</option>
  55. <?
  56. foreach($yeararray[mysql_result($yearresult, $i, "yearstart")] as $value)
  57. {
  58. ?>
  59. <option value="<? echo $value; ?>"><? echo $value; ?></option>
  60. <?
  61. }
  62. ?>
  63. </select>

The next code is a simple drop down I've been working on. I've tried a lot of different things with it but to no avail.

  1.  
  2. <?php
  3.  
  4. $query3 = "SELECT yearstart, yearend FROM makes";
  5.  
  6. $result = mysql_query($query3) or die(mysql_error());;
  7.  
  8. $options="";
  9.  
  10. while ($row=mysql_fetch_array($result)) {
  11.  
  12. $yearstart=$row["yearstart"];
  13. $yearend=$row["yearend"];
  14. $options.="<OPTION VALUE=\"$yearstart\">".$yearstart.$yearend; /// concatenates your option tags with the value and option at that returned row
  15. }
  16. ?>

Thanks,
Arthur
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 23
Reputation: forwardlookguy is an unknown quantity at this point 
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

Re: Populate dropdown using mysql date range

 
0
  #5
Oct 17th, 2008
Any help?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 6
Reputation: Quadar is an unknown quantity at this point 
Solved Threads: 0
Quadar Quadar is offline Offline
Newbie Poster

Re: Populate dropdown using mysql date range

 
0
  #6
Oct 17th, 2008
Possible modification for Choice Box

<?php

$query3 = "SELECT yearstart, yearend FROM makes";

$result = mysql_query($query3) or die(mysql_error());;

$options="";

while ($row=mysql_fetch_array($result)) {
$yearstart=$row["yearstart"];
$yearend=$row["yearend"];
for ( $i=$yearstart;$i<=$yearend;$i++) {
$options.="<OPTION VALUE=\"$i\">". $i . "</OPTION>";
}
}
?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 23
Reputation: forwardlookguy is an unknown quantity at this point 
Solved Threads: 0
forwardlookguy forwardlookguy is offline Offline
Newbie Poster

Re: Populate dropdown using mysql date range

 
0
  #7
Oct 18th, 2008
Well, that's kinda working. The dropdown now populates with all the years for all the Makes. I only want it to display the relevent years for each car the user clicks on. Any ideas?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1
Reputation: petrov50 is an unknown quantity at this point 
Solved Threads: 0
petrov50 petrov50 is offline Offline
Newbie Poster

Re: Populate dropdown using mysql date range

 
0
  #8
Oct 18th, 2008
It seems to me that the reason you are getting all the makes for the year range you choossis because you are not explicitly choosing a make in that query. You need to have WHERE makename = 'something' in your SQL query.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC