943,813 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3432
  • PHP RSS
Oct 15th, 2008
0

Populate dropdown using mysql date range

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Oct 15th, 2008
0

Re: Populate dropdown using mysql date range

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
Reputation Points: 11
Solved Threads: 2
Light Poster
enim213 is offline Offline
40 posts
since Jun 2008
Oct 15th, 2008
0

Re: Populate dropdown using mysql date range

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....
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 16th, 2008
0

Re: Populate dropdown using mysql date range

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:

PHP Syntax (Toggle Plain Text)
  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.

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Oct 17th, 2008
0

Re: Populate dropdown using mysql date range

Any help?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Oct 17th, 2008
0

Re: Populate dropdown using mysql date range

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>";
}
}
?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Quadar is offline Offline
6 posts
since May 2008
Oct 18th, 2008
0

Re: Populate dropdown using mysql date range

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Oct 18th, 2008
0

Re: Populate dropdown using mysql date range

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
petrov50 is offline Offline
1 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC