davidknag Posted January 3, 2011 Share Posted January 3, 2011 Okay so i have a flash game website. I want it so that i can list all the games in a category onto one page. So, php selects the items with the category with mysql(for example 'a for arcade' http://url.com/select.php?cat=a) and then lists every game's name and a link to the url (by grabbing the id of the game and providing a link (http://url.com/play.php?id=1) This is the code i have so far.. <?php $con = mysqli_connect('localhost', 'root', 'root', 'games')or die(mysqli_error($con)); if(isset($_GET['cat'])){ $query_var = $_GET['type']; // force it to be an integer $query=" SELECT * FROM games WHERE category = '$query_var'"; $result = mysqli_query($con, $query)or die(mysqli_error($con)); } } ?> <html> <head> <title> Test </title> </head> <body> ? </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/ Share on other sites More sharing options...
jcbones Posted January 3, 2011 Share Posted January 3, 2011 What is the problem? Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154037 Share on other sites More sharing options...
davidknag Posted January 3, 2011 Author Share Posted January 3, 2011 What is the problem? i dont know how to take every item in the category, put it in a list, and add a link according to the id listed in the sql. Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154039 Share on other sites More sharing options...
jcbones Posted January 3, 2011 Share Posted January 3, 2011 <?php $con = mysqli_connect('localhost', 'root', 'root', 'games')or die(mysqli_error($con)); if(isset($_GET['cat'])){ $query_var = $_GET['type']; // force it to be an integer $query=" SELECT * FROM games WHERE category = '$query_var' ORDER BY title"; $result = mysqli_query($con, $query)or die(mysqli_error($con)); } } ?> <html> <head> <title> Test </title> </head> <body> <?php if(mysql_num_rows($result) >0) { while($r = mysql_fetch_assoc($result)) { echo '<a href="http://url.com/play.php?cat=true&type=' . $query_var . '&id=' . $r['id'] . '" title="' . $r['description'] . '">' . $r['title'] . '</a><br />'; } } else { echo 'No games exist in this category!'; } ?> </body> </html> Try this, it may need some tweaking. Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154045 Share on other sites More sharing options...
davidknag Posted January 3, 2011 Author Share Posted January 3, 2011 <?php $con = mysqli_connect('localhost', 'root', 'root', 'games')or die(mysqli_error($con)); if(isset($_GET['cat'])){ $query_var = $_GET['type']; // force it to be an integer $query=" SELECT * FROM games WHERE category = '$query_var' ORDER BY title"; $result = mysqli_query($con, $query)or die(mysqli_error($con)); } } ?> <html> <head> <title> Test </title> </head> <body> <?php if(mysql_num_rows($result) >0) { while($r = mysql_fetch_assoc($result)) { echo '<a href="http://url.com/play.php?cat=true&type=' . $query_var . '&id=' . $r['id'] . '" title="' . $r['description'] . '">' . $r['title'] . '</a><br />'; } } else { echo 'No games exist in this category!'; } ?> </body> </html> Try this, it may need some tweaking. oh, sorry i didn't tell you this before. The urls for the game are all like "url.com/play.php?id=x" x is the id from mysql Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154046 Share on other sites More sharing options...
fxuser Posted January 3, 2011 Share Posted January 3, 2011 So the thing is: -Get the cat id -query with sql to check if the category exists , if it does then echo all the results , if not go to errorpage -when you click on the game you want to play then check if the id of the game exist , if it does then show the flash game , if not go to errorpage so this is what i came up with: The file that you will get all the results of each category game (select.php) $dbhost = 'localhost'; $dbname = 'root'; $dbuser = 'root'; $dbpass = 'games'; $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); if (isset($_GET['cat'])){ $cat = $_GET['cat']; $get_games = mysql_query("SELECT * FROM games WHERE category = '$cat'"); $get_games_rows = mysql_num_rows($get_games); if ($get_games_rows>=1){ while ($fetch = mysql_fetch_assoc($get_games)){ $id = $fetch['id']; $title = $fetch['title']; echo "<a href='play.php?id=".id."'>Play ".$title."</a><br>"; } } else echo "Category does not exist"; } else echo "Category is not set"; this one to show the game on play.php $dbhost = 'localhost'; $dbname = 'root'; $dbuser = 'root'; $dbpass = 'games'; $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); if (isset($_GET['id'])){ $game_id = $_GET['id']; $query = mysql_query("SELECT * FROM games WHERE id = '$game_id'"); $rows = mysql_num_rows($query); if ($rows>=1){ while($fetch = mysql_fetch_assoc($query)){ $swfname = $fetch['swfname']; } //show the flash game with that name } else{ echo "game does not exist"; } } else echo "Game is not set."; Correct me for any fault. Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154047 Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2011 Share Posted January 3, 2011 Since the OP is already using mysqli, it would probably be a good idea to use mysqli functions for the remainder of the code. Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154048 Share on other sites More sharing options...
davidknag Posted January 3, 2011 Author Share Posted January 3, 2011 Since the OP is already using mysqli, it would probably be a good idea to use mysqli functions for the remainder of the code. Honestly, idc.. whats the difference? So the thing is: -Get the cat id -query with sql to check if the category exists , if it does then echo all the results , if not go to errorpage -when you click on the game you want to play then check if the id of the game exist , if it does then show the flash game , if not go to errorpage so this is what i came up with: The file that you will get all the results of each category game (select.php) $dbhost = 'localhost'; $dbname = 'root'; $dbuser = 'root'; $dbpass = 'games'; $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); if (isset($_GET['cat'])){ $cat = $_GET['cat']; $get_games = mysql_query("SELECT * FROM games WHERE category = '$cat'"); $get_games_rows = mysql_num_rows($get_games); if ($get_games_rows>=1){ while ($fetch = mysql_fetch_assoc($get_games)){ $id = $fetch['id']; $title = $fetch['title']; echo "<a href='play.php?id=".id."'>Play ".$title."</a><br>"; } } else echo "Category does not exist"; } else echo "Category is not set"; this one to show the game on play.php $dbhost = 'localhost'; $dbname = 'root'; $dbuser = 'root'; $dbpass = 'games'; $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); if (isset($_GET['id'])){ $game_id = $_GET['id']; $query = mysql_query("SELECT * FROM games WHERE id = '$game_id'"); $rows = mysql_num_rows($query); if ($rows>=1){ while($fetch = mysql_fetch_assoc($query)){ $swfname = $fetch['swfname']; } //show the flash game with that name } else{ echo "game does not exist"; } } else echo "Game is not set."; Correct me for any fault. might be errors here: if ($get_games_rows>=1){ while ($fetch = mysql_fetch_assoc($get_games)){ $id = $fetch['id']; $title = $fetch['title']; echo "<a href='play.php?id=".$id."'>Play ".$title."</a><br>"; brings me to: www.url/play.php?id=id literally... id=id hmmph Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154050 Share on other sites More sharing options...
fxuser Posted January 3, 2011 Share Posted January 3, 2011 you are right... change : echo "<a href='play.php?id=".id."'>Play ".$title."</a><br>"; to echo "<a href='play.php?id=".$id."'>Play ".$title."</a><br>"; Quote Link to comment https://forums.phpfreaks.com/topic/223223-mysql-integration/#findComment-1154052 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.