hoponhiggo Posted July 18, 2011 Share Posted July 18, 2011 Hi Guys I have the following code which display 'game' data from my database in the relevent html tags. <?php //get results from db $sql = "SELECT * FROM Games ORDER BY gametitle"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("No records found"); // loop through the results returned by your query while($data = mysql_fetch_assoc($res)) { $title=$data['gametitle']; $cover=$data['cover']; $gameid=$data['gameid']; // directory for images $dir="coverart"; ?> <div id="cardcontainer"> <div id="coverart"> <?php echo "<img src='$dir/{$cover}' width='100' height='140'><br>"; ?> </div> <div id="gametitle"> <a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a> </div> <div id="friendrating"></div> <div id="globalrating"></div> </div> <?php } ?> This works fine, apart from the <a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a> bit. What i actually want this to do is to open more details of the individual game that the link is for. How can i do this? Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/ Share on other sites More sharing options...
conker87 Posted July 18, 2011 Share Posted July 18, 2011 <?php //get results from db if (isset($_GET['gameid']) && is_numeric($_GET['gameid'])) { // check to see is the gameis is both set and a number (Assuming gameid is an int in your DB $gameid = mysql_real_escape_string($_GET['gameid'])); // added security $sql = "SELECT * FROM Games WHERE gameid = $gameid"; // making the query, setting the where clause to only fetch the row with gameid of the selected int $res = mysql_query($sql); $data = mysql_fetch_assoc($res); // However you'd like to format the html to output // I'm assuming that the file you have put here is called review.php } else { $sql = "SELECT * FROM Games ORDER BY gametitle"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0) die("No records found"); // loop through the results returned by your query while($data = mysql_fetch_assoc($res)) { $title=$data['gametitle']; $cover=$data['cover']; $gameid=$data['gameid']; // directory for images $dir="coverart"; ?> <div id="cardcontainer"> <div id="coverart"> <?php echo "<img src='$dir/{$cover}' width='100' height='140'><br>"; ?> </div> <div id="gametitle"> <a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a> </div> <div id="friendrating"></div> <div id="globalrating"></div> </div> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244213 Share on other sites More sharing options...
hoponhiggo Posted July 18, 2011 Author Share Posted July 18, 2011 Hi Thanks for the reply. I know this is a big ask, but any chance you could give me a brief explanation of what you have coded? Just so i know for the future and can make the right changes if neccessary Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244216 Share on other sites More sharing options...
conker87 Posted July 18, 2011 Share Posted July 18, 2011 The If statement checks to see if gameid variable exists in the url (the review?gameid= part) AND that the value is a number if (isset($_GET['gameid']) && is_numeric($_GET['gameid'])) { Then it's added into another variable, mainly for ease of use and then escaped for added security, mysql_real_escape_string escapes all bad and potentially malicious characters or code. $gameid = mysql_real_escape_string($_GET['gameid'])); We then build the query, pretty much the same as your query before, but instead of getting all data, we just pick the row in the DB that has the gameid of the value that we choose. $sql = "SELECT * FROM Games WHERE gameid = $gameid"; We execute the query. $res = mysql_query($sql); Put the data gathered into an array. $data = mysql_fetch_assoc($res); // However you'd like to format the html to output // I'm assuming that the file you have put here is called review.php } else { Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244217 Share on other sites More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 the code that conker posted will always return false, since he uses the is_int function on a $_GET value, query-strings that are passed to the $_GET array are strings, not integers.. <?php //get results from db if (!empty($_GET['gameid'])) { //if an id is allowed to be 0, replace with isset() $gameid = $_GET['gameid']); // mysql_real_escape_string is only needed for db insertion $sql = "SELECT * FROM Games WHERE gameid = $gameid"; $res = mysql_query($sql); $data = mysql_fetch_array($res, MYSQL_ASSOC); //just the way I like to do it // However you'd like to format the html to output }else { $sql = "SELECT * FROM Games ORDER BY gametitle"; $res = mysql_query($sql) or die(mysql_error()); //use mysql_error() for debugging only if(mysql_num_rows($res) == 0){ die("Mysql Error:".mysql_error()); }else{ // loop through the results returned by your query while($data = mysql_fetch_assoc($res)) { $title=$data['gametitle']; $cover=$data['cover']; $gameid=$data['gameid']; // directory for images $dir="coverart"; ?> <div id="cardcontainer"> <div id="coverart"> <?php echo "<img src='{$dir}/$cover' width='100' height='140'><br>"; ?> </div> <div id="gametitle"> <a href="Reviews.php?gameid=<?php echo $gameid ?>"><?php echo $title ?></a> </div> <div id="friendrating"></div> <div id="globalrating"></div> </div> <?php } } ?> Edit: he changed it to is_numeric, which will work in this case Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244218 Share on other sites More sharing options...
conker87 Posted July 18, 2011 Share Posted July 18, 2011 Edit: he changed it to is_numeric, which will work in this case Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244219 Share on other sites More sharing options...
hoponhiggo Posted July 18, 2011 Author Share Posted July 18, 2011 Ah Thank you botth for your help. I think i get the jist of this now. I was looking at using and ' }else{' myself but couldnt get it to work Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244222 Share on other sites More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 Edit: he changed it to is_numeric, which will work in this case Ah Thank you botth for your help. I think i get the jist of this now. I was looking at using and ' }else{' myself but couldnt get it to work no problem Quote Link to comment https://forums.phpfreaks.com/topic/242267-help-with/#findComment-1244229 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.