Jump to content


Photo

Need some help..pulling data from a mysql database.


  • Please log in to reply
4 replies to this topic

#1 fluxem

fluxem
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 14 August 2006 - 05:35 PM

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


#2 mATOK

mATOK
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts
  • LocationToronto, Ontario

Posted 14 August 2006 - 06:33 PM

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?
"Not everything that counts can be counted, and not everything that can be counted counts."

W3 Web Tutorials | Check Your html | CSS layout techniques | ALA | Zen Garden | CSS Links

Navs with flavor | CSS Playground | CSS boxes

#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 14 August 2006 - 07:04 PM

<?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

?>

Link to this page with:

<a href="casinos.php?state=CA">California</a>

Legend has it that reading the manual never killed anyone.
My site

#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 14 August 2006 - 07:10 PM

Adding on to AndyB'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:

states
state_id => unique two letter name for state (i.e. California = CA)
state_name => full name for the state ('California')

casinos
casino_id => unique ID number for the casino
casino_name => the name of the casino
state_id => the two letter name for the state this casino is associated with

You could layout your basic code something like this:

casinos.php
[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 states
while($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 on
if($_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';
}

?>


#5 fluxem

fluxem
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 14 August 2006 - 09:59 PM

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 I
selected, which is exactly what I was wanting.

thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users