PodiumAssault
02-23-2009, 05:53 AM
Hi. I'm terribly novice when it involves MySQL queries so I've tried to figure out this problem but I just can't seem to work around it. I think I learn best when someone else shows me what I've done wrong, or presents a working version and I can then break it down to see where my errors were. So I apologize for the novice code that is about to follow in this post :)
http://beta.podiumassault.com/setupgarage.php
- On that page, the users are prompted to select a vehicle.
- Once they select a vehicle (inserted into URL via GET variable) they are presented with a list of tracks to select.
- Once they select a track (inserted into URL via GET, along with vehicle) they are presented with a list of setups available for that combination.
(use Riley Daytona Prototype and then Daytona International Speedway - Road, to see it working fine)
That works just fine and dandy, but it is a bit tedious and uninformative. I figured the best way was to show how many setups are available for each vehicle from the start.
So I queried the database for the vehicle names, and used a WHILE loop to display the names and then query the database again inside the WHILE loop to count the setups available. This gives me an error when the second mysql_fetch_array comes around, saying it isn't a valid resource. Perhaps it is my novice coding biting me in the bottom.
I've tried and tried to get it to work while it cycles through the vehicles, but I just can't figure it out. Below I've included the database schema as well as the horrible PHP code I've come up with to get the page to work.
CARS table
- carID
- carnameshort
- carnamelong
- carstatus (active/inactive)
TRACKS table
- trackID
- tracknameshort
- tracknamefull
- trackstatus (active/inactive)
SETUPS table
- setupID
- creatorID
- carnameshort
- tracknameshort
- filename
- availability (public/private)
(there are more fields, but I dont think they are relevant)
Here is the PHP coding with the inlaid MySQL queries where I pull the information. I'll love to see any suggestions on protecting the data from injection, and/or just general code tips.
I use a file for functions, and I connect to the database with one (dbconnect) and then select a database with another (selectdb). Here is the code, laugh away. ;)
(coding questions at the bottom)
<?php
// Display the setups if both variables are set.
if (isset($_GET['vehicle']) && isset($_GET['track'])) {
// Assign the GET variables to something simpler
$vehiclesearch = $_GET['vehicle'];
$tracksearch = $_GET['track'];
$viewability = "Public";
// Connect to the database and start pulling information
dbconnect();
selectdb();
$setupQuery = "SELECT * FROM setups WHERE carnameShort='$vehiclesearch' AND tracknameShort='$tracksearch' AND availability='$viewability' ORDER BY setupID";
$setupResult = mysql_query($setupQuery);
// check to see if there are any setups or not
if (mysql_num_rows($setupResult) == 0) {
echo "There are no setups available at this car and track combination, please check back later!";
echo "<meta http-equiv='refresh' content='3;url=setupgarage.php' />";
}
else {
// Display the list of setups
while ($setupData = mysql_fetch_array($setupResult)) {
echo $setupData['setupFilename'] . "<br />";
}
}
}
// Display the tracks if the vehicle variable is the only one set
elseif (isset($_GET['vehicle'])) {
// Connect to the database and select the table
dbconnect();
selectdb();
$getVehicle = $_GET['vehicle'];
// Query the database for the track information
$trackQuery = "SELECT tracknameFull, tracknameShort, trackStatus FROM tracks WHERE trackStatus='active' ORDER BY tracknameFull";
$trackResult = mysql_query($trackQuery) or die(mysql_error());
while($trackData = mysql_fetch_array($trackResult)) {
echo "<a href=\"" . $_SERVER['PHP_SELF'] ."?vehicle=".$getVehicle."&track=".$trackData['tracknameShort']."\">".$trackData['tracknameFull']."</a><br />";
}
}
// Display the vehicles if no variables are set
else {
// Connect to the database and select the table
dbconnect();
selectdb();
// Query the database for the car information
// RETURNS AT LEAST A RESULT ==> $carQuery = "SELECT c.carnameFull, c.carnameShort, COUNT(*) as count FROM cars c, setups s WHERE c.carStatus='active' AND s.availability='Public' GROUP BY c.carID";
$carQuery = "SELECT carnameFull, carnameShort FROM cars WHERE carStatus='active' ORDER BY carID";
$carResult = mysql_query($carQuery) or die(mysql_error());
while($carData = mysql_fetch_array($carResult)) {
echo "<a href=\"" . $_SERVER['PHP_SELF'] ."?vehicle=".$carData['carnameShort']."\">".$carData['carnameFull']."</a> (". $carData['setupCount']." setups available)<br />";
}
}
?>
There are two things I would really LOVE to learn how to do (or at least figure them out)
1) Display the total setups available next to each car upon the initial page view. (tried this and failed)
2) Once a vehicle is selected, show the tracks available that ONLY have at least one setup available for them, rather than a list of 50 tracks. (no idea how to do this :( )
I do apologize for the length of this post, and if the request is too much. I'm just lost with this.
Thanks,
Ryan
http://beta.podiumassault.com/setupgarage.php
- On that page, the users are prompted to select a vehicle.
- Once they select a vehicle (inserted into URL via GET variable) they are presented with a list of tracks to select.
- Once they select a track (inserted into URL via GET, along with vehicle) they are presented with a list of setups available for that combination.
(use Riley Daytona Prototype and then Daytona International Speedway - Road, to see it working fine)
That works just fine and dandy, but it is a bit tedious and uninformative. I figured the best way was to show how many setups are available for each vehicle from the start.
So I queried the database for the vehicle names, and used a WHILE loop to display the names and then query the database again inside the WHILE loop to count the setups available. This gives me an error when the second mysql_fetch_array comes around, saying it isn't a valid resource. Perhaps it is my novice coding biting me in the bottom.
I've tried and tried to get it to work while it cycles through the vehicles, but I just can't figure it out. Below I've included the database schema as well as the horrible PHP code I've come up with to get the page to work.
CARS table
- carID
- carnameshort
- carnamelong
- carstatus (active/inactive)
TRACKS table
- trackID
- tracknameshort
- tracknamefull
- trackstatus (active/inactive)
SETUPS table
- setupID
- creatorID
- carnameshort
- tracknameshort
- filename
- availability (public/private)
(there are more fields, but I dont think they are relevant)
Here is the PHP coding with the inlaid MySQL queries where I pull the information. I'll love to see any suggestions on protecting the data from injection, and/or just general code tips.
I use a file for functions, and I connect to the database with one (dbconnect) and then select a database with another (selectdb). Here is the code, laugh away. ;)
(coding questions at the bottom)
<?php
// Display the setups if both variables are set.
if (isset($_GET['vehicle']) && isset($_GET['track'])) {
// Assign the GET variables to something simpler
$vehiclesearch = $_GET['vehicle'];
$tracksearch = $_GET['track'];
$viewability = "Public";
// Connect to the database and start pulling information
dbconnect();
selectdb();
$setupQuery = "SELECT * FROM setups WHERE carnameShort='$vehiclesearch' AND tracknameShort='$tracksearch' AND availability='$viewability' ORDER BY setupID";
$setupResult = mysql_query($setupQuery);
// check to see if there are any setups or not
if (mysql_num_rows($setupResult) == 0) {
echo "There are no setups available at this car and track combination, please check back later!";
echo "<meta http-equiv='refresh' content='3;url=setupgarage.php' />";
}
else {
// Display the list of setups
while ($setupData = mysql_fetch_array($setupResult)) {
echo $setupData['setupFilename'] . "<br />";
}
}
}
// Display the tracks if the vehicle variable is the only one set
elseif (isset($_GET['vehicle'])) {
// Connect to the database and select the table
dbconnect();
selectdb();
$getVehicle = $_GET['vehicle'];
// Query the database for the track information
$trackQuery = "SELECT tracknameFull, tracknameShort, trackStatus FROM tracks WHERE trackStatus='active' ORDER BY tracknameFull";
$trackResult = mysql_query($trackQuery) or die(mysql_error());
while($trackData = mysql_fetch_array($trackResult)) {
echo "<a href=\"" . $_SERVER['PHP_SELF'] ."?vehicle=".$getVehicle."&track=".$trackData['tracknameShort']."\">".$trackData['tracknameFull']."</a><br />";
}
}
// Display the vehicles if no variables are set
else {
// Connect to the database and select the table
dbconnect();
selectdb();
// Query the database for the car information
// RETURNS AT LEAST A RESULT ==> $carQuery = "SELECT c.carnameFull, c.carnameShort, COUNT(*) as count FROM cars c, setups s WHERE c.carStatus='active' AND s.availability='Public' GROUP BY c.carID";
$carQuery = "SELECT carnameFull, carnameShort FROM cars WHERE carStatus='active' ORDER BY carID";
$carResult = mysql_query($carQuery) or die(mysql_error());
while($carData = mysql_fetch_array($carResult)) {
echo "<a href=\"" . $_SERVER['PHP_SELF'] ."?vehicle=".$carData['carnameShort']."\">".$carData['carnameFull']."</a> (". $carData['setupCount']." setups available)<br />";
}
}
?>
There are two things I would really LOVE to learn how to do (or at least figure them out)
1) Display the total setups available next to each car upon the initial page view. (tried this and failed)
2) Once a vehicle is selected, show the tracks available that ONLY have at least one setup available for them, rather than a list of 50 tracks. (no idea how to do this :( )
I do apologize for the length of this post, and if the request is too much. I'm just lost with this.
Thanks,
Ryan