fluxem Posted August 14, 2006 Share Posted August 14, 2006 I am very new to PHP as well as MySQL so I think what I am trying to do is probably rather simple.. but after reviewing several tutorials and such I am unable to do it.What I am trying to do is this:We have a listing of all 50 states in the US... when one state is click, California for example, on that same page (casinos.php) I would like to have all the California Casinos that I have stored in my mysql database dynamically listed below. I have setup the database table already and have inputted some data to test it with. Is it possible to do all of this with just one php page? I can make it work if I made 50 seperate php pages for each state each one drawing from the table where for example state=CA, but this seems like a lot more work that I probably could avoid. Hopefully I dont sound too much like a moron for asking this but I would really like to learn how to do it.Thanks for your help in advance.. if you need any more info, let me know.Sam Quote Link to comment Share on other sites More sharing options...
mATOK Posted August 14, 2006 Share Posted August 14, 2006 I believe that what you have is an image map, where different states are links. If so, you should be able to link back to the main php page with a variable say, state="??" and then set the page up so that if a state is returned to action a function that displays your casinos and could even change the state on the image map to a different color...Does this help you at all? Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 14, 2006 Share Posted August 14, 2006 [code]<?php// casinos.php - the one and only page you need$thestate = $_GET['state'];if ($thestate="") { $thestate = "CA"; // default for bozos}// make database connection however you do it$query = "SELECT * from your_tablename WHERE state='$thestate'";$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);.. handle the results as you see fit?>[/code]Link to this page with:[code]<a href="casinos.php?state=CA">California</a>[/code] Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 14, 2006 Share Posted August 14, 2006 Adding on to [b]AndyB[/b]'s post, you could also print your states from a database.Here's a basic code layout and basic result printing:Let's assume that your state and casino tables in the database look something like this:[b]states[/b]state_id => unique two letter name for state (i.e. California = CA)state_name => full name for the state ('California')[b]casinos[/b]casino_id => unique ID number for the casinocasino_name => the name of the casinostate_id => the two letter name for the state this casino is associated withYou could layout your basic code something like this:[b]casinos.php[/b][code][code=php:0]<?php// Query for a list of states$state_sql = "SELECT state_name, state_id FROM states ORDER BY state_name ASC";// Run the query$state_result = mysql_query($state_sql) or die("Error Fetching States: ".mysql_error());// Print out the stateswhile($state_row = mysql_fetch_array($state_result)){ echo '<a href="'.$_SERVER['PHP_SELF'].'?state='.$state_row['state_id'].'">'.$state_row['state_name'].'</a><br />';}echo '<p>';// Check if a state link was clicked onif($_GET['state']){ // Format the state ID from the URL for use in a SQL query $state = mysql_real_escape_string(trim($_GET['state'])); // Query for the casinos in the current state $casino_sql = "SELECT * FROM casinos WHERE state_id = '$state' ORDER BY casino_name ASC"; // Run the query $casino_result = mysql_query($casino_sql) or die("Error Retrieving Casinos: ".mysql_error()); // Check if any casinos exist for the current state $casinos_num = mysql_num_rows($casino_result); echo 'Casinos for selected state:<br />'; // Print out the casinos if any exist for the current state if($casinos_num > 0){ while($casino_row = mysql_fetch_array($state_result)){ echo $casino_row['casino_name'].'<br />'; } } else { echo '<strong>No casinos entered for current state!</strong>'; }} else { echo '<strong>Select a state above to see it\'s casinos';}?>[/code] Quote Link to comment Share on other sites More sharing options...
fluxem Posted August 14, 2006 Author Share Posted August 14, 2006 hey thanks guys, really appreciate your help!So it is working now... when I select the state it pulls in the casino list that corresponds to that state Iselected, which is exactly what I was wanting.thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.