pappakaka Posted July 20, 2011 Share Posted July 20, 2011 I have a website that uses PHP to display content. That content is ofcourse taken from a database in MySQL, and only 1 row at a time is displayed on the page. The row of content that is displayed is randomly selected every time the page loads. So the page change (almost) everytime you reload it. Here is the problem. Even if the content on the page change, the URL does not. So there is no way to display a specific row. For example: I load the page www.website.com/row.php and a random row with ID 1 is displayed. Then I reload the page and a new row with the ID 2 is displayed, but the URL is still the same. What I need is so that, when it selects a random row, the ID of that row is added to the URL as a query. Like this: Row 1 = www.website.com/row.php?id=1 Row 2 = www.website.com/row.php?id=2 That way, if you only go to www.website.com/row.php. The page reloads and a random row is displayed with the ID of that row added to the URL. And if you go to www.website.com/row.php?id=1 directly, no random row is selected, only the row that matches the ID of the URL. I hope you understand and can help me, I really don't know how to explain it better. Here is the code I use right now to select a random row from the database and also to calculate a few inputs the user can make on the previous page on the site. <?php require("Includes/connection.php"); $connection = @mysql_connect($ControlHost, $ControlUser, $ControlPass) or die("Couldn't connect to server"); $db = @mysql_select_db("$ControlDb", $connection) or die("Couldn't select database"); $genre_string=""; //$genre_string1=""; if(empty($_REQUEST['genreString1'])){ if(!empty($_REQUEST['genre'])){ $genre=$_REQUEST['genre']; $total_genre=count($genre); $genre_string1=""; for($i=0; $i<$total_genre;$i++){ $genre_string .= "Genree LIKE '%" . $genre[$i] . '%' . "'". " AND "; $genre_string1 .= $genre[$i] . " OR "; } $genre_string = substr($genre_string, 0, -4); $genre_string1 = substr($genre_string1, 0, -4); $genre_string = "WHERE" . "(" . $genre_string . ")"; } if( !empty($_REQUEST['YearFrom']) ){ $YearFrom = $_REQUEST['YearFrom']; $YearTo = $_REQUEST['YearTo']; if(!empty($_REQUEST['genre'])) $genre_string .= " AND "; else $genre_string .= "Where "; $genre_string .= "(YearFrom BETWEEN $YearFrom AND $YearTo)"; } }elseif(!empty($_REQUEST['genreString1'])){ $genre_string1 = $_REQUEST['genreString1']; $pieces = explode(" OR ", $genre_string1); for($ii=0; $ii<count($pieces);$ii++){ $genre_string .= "Genree LIKE '%" . trim($pieces[$ii]) . '%' . "'" . " AND "; $genre[]=trim($pieces[$ii]); } $genre_string = substr($genre_string, 0, -4); if( !is_numeric(trim($pieces[0]))) $genre_string = ""; else $genre_string = "WHERE" . "(" . $genre_string . ")"; if( !empty($_REQUEST['YearFrom']) ){echo "111"; $YearFrom = $_REQUEST['YearFrom']; $YearTo = $_REQUEST['YearTo']; if( is_numeric(trim($pieces[0]))) $genre_string .= " AND (YearFrom BETWEEN $YearFrom AND $YearTo)"; if( !is_numeric(trim($pieces[0]))) $genre_string = " Where (YearFrom BETWEEN $YearFrom AND $YearTo)"; } } //echo $genre_string; //echo "SELECT * FROM movie $genre_string ORDER BY RAND() LIMIT 0,1<br />"; $result = mysql_query("SELECT * FROM movie $genre_string ORDER BY RAND() LIMIT 0,1"); //echo $er=mysql_num_rows($result);exit; $row = mysql_fetch_array($result); $MID = $row["MID"]; $Title = $row["Title"]; $YearFrom = $row["YearFrom"]; $RMTRating = $row["RMTRating"]; $IMDBRating = $row["IMDBRating"]; $Description = $row["Description"]; $Genree = $row["Genree"]; $Genree = explode(",", $Genree); $Poster = $row["Poster"]; $TrailerLink = $row["TrailerLink"]; $MoreInfo = $row["MoreInfo"]; $Prequel = $row["Prequel"]; $Sequel = $row["Sequel"]; ?> Then you I imagine it would be something like this to make each URL/row unique: $currentdomain = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; if ($currentdomain == "http://www.website.com/row.php") { header( "Location: row.php?id=" . $MID ) ; } Quote Link to comment https://forums.phpfreaks.com/topic/242429-help-making-each-page-unique/ Share on other sites More sharing options...
dragon_sa Posted July 20, 2011 Share Posted July 20, 2011 If you enter a url like www.website.com/row.php?id=2, then id=2 is a $_GET variable, if you add code to your row.php page to check for this variable then it can display that row from the database, something like if (isset($_GET['id']) && is_int($_GET['id'])) { $id=$_GET['id']; $result = mysql_query("SELECT * FROM movie '$genre_string' WHERE id='$id'"); } else { $result = mysql_query("SELECT * FROM movie '$genre_string' ORDER BY RAND() LIMIT 0,1"); } or something to that affect, if I understand you correctly Quote Link to comment https://forums.phpfreaks.com/topic/242429-help-making-each-page-unique/#findComment-1245112 Share on other sites More sharing options...
pappakaka Posted July 20, 2011 Author Share Posted July 20, 2011 If you enter a url like www.website.com/row.php?id=2, then id=2 is a $_GET variable, if you add code to your row.php page to check for this variable then it can display that row from the database, something like if (isset($_GET['id']) && is_int($_GET['id'])) { $id=$_GET['id']; $result = mysql_query("SELECT * FROM movie '$genre_string' WHERE id='$id'"); } else { $result = mysql_query("SELECT * FROM movie '$genre_string' ORDER BY RAND() LIMIT 0,1"); } or something to that affect, if I understand you correctly Thank you so much, this actually worked! So simple! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/242429-help-making-each-page-unique/#findComment-1245148 Share on other sites More sharing options...
AyKay47 Posted July 20, 2011 Share Posted July 20, 2011 is_int will not work when checking for a $_GET value..everything sent to the $_GET array from the query string is in string format.. with the code that dragon_sa posted...you will always invoke the else condition...i would recommend changing to is_numeric instead...which checks for integers or string numbers Quote Link to comment https://forums.phpfreaks.com/topic/242429-help-making-each-page-unique/#findComment-1245153 Share on other sites More sharing options...
premiso Posted July 20, 2011 Share Posted July 20, 2011 Or better yet, statically cast it to an int. if (!empty($_GET['id'])) { $id=(int)$_GET['id']; $where = "WHERE id='$id'"; } else { $where = "ORDER BY RAND() LIMIT 0,1"; } $result = mysql_query("SELECT * FROM movie '$genre_string' " . $where) or trigger_error('Query Failed: ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/242429-help-making-each-page-unique/#findComment-1245155 Share on other sites More sharing options...
pappakaka Posted July 20, 2011 Author Share Posted July 20, 2011 Yes by using $id=(int)$_GET['id']; it worked much better thank you! Quote Link to comment https://forums.phpfreaks.com/topic/242429-help-making-each-page-unique/#findComment-1245168 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.