943,844 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 868
  • PHP RSS
Sep 27th, 2008
0

Use mysql data to form links in PHP

Expand Post »
Hey everyone, I want to use mysql to list all of the makes of vehicles and I want the user to be able to click a make and the years that make were made pop up. Is there a way to do this using PHP? I've got the makes to list out but I can't figure out how to make them into a link that takes you to a page of the all the years it was offered.

Thanks for any help!
Arthur
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Sep 27th, 2008
0

Re: Use mysql data to form links in PHP

Hey everyone, I want to use mysql to list all of the makes of vehicles and I want the user to be able to click a make and the years that make were made pop up. Is there a way to do this using PHP? I've got the makes to list out but I can't figure out how to make them into a link that takes you to a page of the all the years it was offered.

Thanks for any help!
Arthur
I would start with something like this, it is untested so may need a little debugging, but not much
php Syntax (Toggle Plain Text)
  1. <? include("connection.php"); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. <title>Untitled Document</title>
  7. </head>
  8.  
  9. <body>
  10. <?
  11. $query = "";
  12. if(isset($_GET['make']))
  13. {
  14. if(trim($query) == "")
  15. {
  16. $query .= " where make = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
  17. }
  18. else
  19. {
  20. $query .= " and make = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
  21. }
  22. }
  23.  
  24. if(isset($_GET['year']) && $_GET['year'] != "--select--")
  25. {
  26. if(trim($query) == "")
  27. {
  28. $query .= " where year = '" . mysql_real_escape_string(stripslashes($_GET['year'])) . "'";
  29. }
  30. else
  31. {
  32. $query .= " and year = '" . mysql_real_escape_string(stripslashes($_GET['year'])) . "'";
  33. }
  34. }
  35.  
  36. $query = "select vehiclepk, make, model, year from vehicles" . $query . " order by make, model";
  37. $searchresult = mysql_query($query);
  38.  
  39. $query = "select distinct make, year from vehicles order by make, year";
  40. $yearresult = mysql_query($query);
  41.  
  42. $yeararray = array();
  43. while($row = mysql_fetch_assoc($yearresult))
  44. {
  45. $yeararray[$row["make"]][] = $row["year"];
  46. }
  47. ?>
  48. <table cellpadding="0" cellspacing="0">
  49. <?
  50. if(mysql_num_rows($searchresult) == 0 || !is_numeric(mysql_num_rows($searchresult)))
  51. {
  52. ?>
  53. <tr>
  54. <td colspan="3">No vehicles found under that search criteria.</td>
  55. </tr>
  56. <?
  57. }
  58. else
  59. {
  60. for($i = 0; $i < mysql_num_rows($searchresult); $i++)
  61. {
  62. ?>
  63. <tr>
  64. <td><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?make=<? echo urlencode(mysql_result($searchresult, $i, "make")); ?>"><? echo mysql_result($searchresult, $i, "make"); ?></a></td>
  65. <td><? echo mysql_result($searchresult, $i, "model"); ?></td>
  66. <td>
  67. <form action="<? echo basename($_SERVER['PHP_SELF']); ?>" method="get">
  68. <input type="hidden" name="make" value="<? echo mysql_result($searchresult, $i, "make"); ?>" />
  69. <select name="year" id="year<? echo mysql_result($searchresult, $i, "vehiclepk"); ?>">
  70. <option value="--select--">--select--</option>
  71. <?
  72. foreach($yeararray[mysql_result($searchresult, $i, "make")] as $value)
  73. {
  74. ?>
  75. <option value="<? echo $value; ?>"><? echo $value; ?></option>
  76. <?
  77. }
  78. ?>
  79. </select>&nbsp;&nbsp;
  80. <input type="submit" value="Search" />
  81. </form>
  82. </td>
  83. </tr>
  84. <?
  85. }
  86. }
  87. ?>
  88. </table>
  89. </body>
  90. </html>
  91. <?
  92. mysql_close();
  93. ?>
just edited the second loop
Last edited by R0bb0b; Sep 27th, 2008 at 5:44 pm.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Sep 28th, 2008
0

Re: Use mysql data to form links in PHP

Awesome! Thanks for posting but I still have a slight problem. I got it to echo the Makes out and when I click them, it takes me back to the page. However, I want the page to be where you click on the make from the list and it takes you to another page where the years for that make are listed and you can click on those. How would I go about doing that? I'm sure your code is easily modified to accomplish this but I can't figure it out. Btw, I have a database called productionfigures with tables called Make and Years. They have a common id linking them. For instance, Chevrolet has the id of 1 in the Makes table and 1930-2008 have the id of 1 as well in the Years table and so on.

I hope this isn't too confusing.

Thanks for your time,
Arthur
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008
Sep 28th, 2008
0

Re: Use mysql data to form links in PHP

Ok, I've been working on this code and have gotten it figured out except for one thing. I cannot populate the drop down boxes with the years that are specific to the makes. Here's the modified code:

PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4. require "includes/dbconnect.php";
  5.  
  6.  
  7. $query = "";
  8. if(isset($_GET['make']))
  9. {
  10. if(trim($query2) == "")
  11. {
  12. $query2 .= " where name = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
  13. }
  14. else
  15. {
  16. $query2 .= " and name = '" . mysql_real_escape_string(stripslashes($_GET['make'])) . "'";
  17. }
  18. }
  19.  
  20. if(isset($_GET['year']) && $_GET['year'] != "--select--")
  21. {
  22. if(trim($query3) == "")
  23. {
  24. $query3 .= " where yearname = '" . mysql_real_escape_string(stripslashes($_GET['year'])) . "'";
  25. }
  26. else
  27. {
  28. $query3 .= " and yearname = '" . mysql_real_escape_string(stripslashes($_GET['year'])) . "'";
  29. }
  30. }
  31.  
  32. $query2 = "select id, name from makes" . $query2 . " order by name";
  33. $searchresult = mysql_query($query2) or die(mysql_error());;
  34.  
  35. $query3 = "select distinct yearname from years order by yearname";
  36. $yearresult = mysql_query($query3) or die(mysql_error());;
  37.  
  38.  
  39. $yeararray = array();
  40. while($row = mysql_fetch_assoc($yearresult))
  41. {
  42. $yeararray[$row["make"]][] = $row["year"];
  43. }
  44. ?>
  45. <table cellpadding="0" cellspacing="0">
  46. <?
  47. if(mysql_num_rows($searchresult) == 0 || !is_numeric(mysql_num_rows($searchresult)))
  48. {
  49. ?>
  50. <tr>
  51. <td colspan="3">No vehicles found under that search criteria.</td>
  52. </tr>
  53. <?
  54. }
  55. else
  56. {
  57. for($i = 0; $i < mysql_num_rows($searchresult); $i++)
  58. {
  59. ?>
  60. <tr>
  61. <td><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?make=<? echo urlencode(mysql_result($searchresult, $i, "name")); ?>"><? echo mysql_result($searchresult, $i, "name"); ?></a></td>
  62. <td><? echo mysql_result($yearresult, $i, "yearname"); ?></td>
  63. <td>
  64. <form action="<? echo basename($_SERVER['PHP_SELF']); ?>" method="get">
  65. <input type="hidden" name="make" value="<? echo mysql_result($searchresult, $i, "name"); ?>" />
  66. <select name="year" id="year<? echo mysql_result($yearresult, $i, "yearname"); ?>">
  67. <option value="--select--">--select--</option>
  68. <?
  69. foreach($yeararray[mysql_result($yearresult, $i, "yearname")] as $value)
  70. {
  71. ?>
  72. <option value="<? echo $value; ?>"><? echo $value; ?></option>
  73. <?
  74. }
  75. ?>
  76. </select>&nbsp;&nbsp;
  77. <input type="submit" value="Search" />
  78. </form>
  79. </td>
  80. </tr>
  81. <?
  82. }
  83. }
  84.  
  85. ?>
  86. </table>
  87. </body>
  88. </html>
  89. <?
  90. mysql_close();
  91. ?>

As it is now, one year is listed beside the make and then there is the drop down box with --select-- in it and the submit button. Can anyone tell me what I've missed here?

Thanks,
Arthur
Reputation Points: 10
Solved Threads: 0
Newbie Poster
forwardlookguy is offline Offline
23 posts
since Jun 2008

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:
Previous Thread in PHP Forum Timeline: Displaying date from mysql
Next Thread in PHP Forum Timeline: regarding image





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


Follow us on Twitter


© 2011 DaniWeb® LLC