Jump to content

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


fluxem

Recommended Posts

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
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?
[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]
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 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:

[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 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';
}

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.