czukoman20 Posted December 12, 2007 Share Posted December 12, 2007 I have this pagination code that i've been working with. and i only have one issue with it. Im trying to make it so the user is going through the pages it is in a random order. I want to make it so that another user won't have that same order for fairness issues. More explaination would be the ("SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand()") Function makes a random order out of the users... user3 user4 user1 user2 user5 When another person from a different computer comes into play .... it will be the exact same random order. How would i make it so it repicks and order for every person. So the first person would have 3 4 1 2 5 Second person would have 5 2 3 1 4 just examples. If i am not clear... then please tell me . dont leave me hanging please <?php // Connects to your Database @mysql_connect("db1093.perfora.net", "dbo215169417", "TkA.Btc7") or die(mysql_error()); @mysql_select_db("db215169417") or die(mysql_error()); //This checks to see if there is a page number. If not, it will set it to page 1 if (!(isset($pagenum))) { $pagenum = 1; } //Here we count the number of results //Edit $data to be your query $data = mysql_query("SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand()") or die(mysql_error()); $rows = mysql_num_rows($data); //This is the number of results displayed per page $page_rows = 1; //This tells us the page number of our last page $last = ceil($rows/$page_rows); //this makes sure the page number isn't below one, or more than our maximum pages if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } //This sets the range to display in our query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query("SELECT * FROM users WHERE userid_2 > 0 $max") or die(mysql_error()); //This is where you display your query results while($values = mysql_fetch_array( $data_p )) { //Print $values'Name']; echo "<br>";; $username = $values['username']; echo " <img src=\"http://www.adworld-online.com/images/banners/". $username ."_banner.jpg\" width=\"500\" height=\"95\"> <br>"; echo ".::. Company: " . $values['companyname'] . "<br> "; echo "Short info: " . $values['info'] . "<br> "; if($session->logged_in){ echo "<a href=\"http://www.adworld-online.com/submit.php?user=$username\">.::. Contact Us </a> <br />"; } else{ echo "<a href=\"http://www.adworld-online.com/submit2.php?user=$username\">.::. Contact Us </a> <br />"; } } echo "<p>"; // This shows the user what page they are on, and the total number of pages echo " --Page $pagenum of $last-- <p>"; // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. if ($pagenum == 1) { } else { echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; } //just a spacer echo " ---- "; //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links if ($pagenum == $last) { } else { $next = $pagenum+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; } ?> ?> Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 12, 2007 Share Posted December 12, 2007 are you wanting the usernames to be random or are you wanting the actually pagination numbers to be random? Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 12, 2007 Author Share Posted December 12, 2007 the usernames. Like i said tho they are random.. but only once. i want them to switch order for every person who views the site. but not as they are hitting the next button Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 Try seeding it with the time, so that it is always different. SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand(NOW()) Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted December 12, 2007 Share Posted December 12, 2007 you could also add a cache control header to your page. Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 12, 2007 Author Share Posted December 12, 2007 Lemmins process didn't work. but thanks. you could also add a cache control header to your page. How would u do that? Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 12, 2007 Author Share Posted December 12, 2007 How bout a different process anyone else Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 Try making a getting a set of values from the db that are in range and randomly organizing them in php. Then query for that set of values. Something like this: $uids = array(); $qry = mysql_query("SELECT userid_2 FROM users WHERE userid_2 > 0"); while ($row = mysql_fetch_assoc($qry)) $uids[] = $row['userid_2']; shuffle($uids); $qry = mysql_query("SELECT * FROM users WHERE userid_2 IN (" . implode(",", $uids) . ")"); Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 12, 2007 Author Share Posted December 12, 2007 Still doesn't work.. just to be clear tho .... u put the code like this ?? ? <?php $pagenum = 1; } //Here we count the number of results //Edit $data to be your query //$data = mysql_query("SELECT * FROM users WHERE userid_2 > '0' ORDER BY rand(NOW())") or die(mysql_error()); $uids = array(); $data = mysql_query("SELECT userid_2 FROM users WHERE userid_2 > 0"); while ($row = mysql_fetch_assoc($data)) $uids[] = $row['userid_2']; shuffle($uids); $data = mysql_query("SELECT * FROM users WHERE userid_2 IN (" . implode(",", $uids) . ")"); $rows = mysql_num_rows($data); //This is the number of results displayed per page $page_rows = 4; //ect..... ?> Quote Link to comment Share on other sites More sharing options...
lemmin Posted December 12, 2007 Share Posted December 12, 2007 Well, I don't really know how your database is set up or what column you would want to randomize, so you may need to mess aorund with it. In theory it should work. What do you mean by "doesn't work"? Does it return anything different than your previous query? Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 13, 2007 Author Share Posted December 13, 2007 it doesnt have any error... and here is the link to what i have http://www.adworld-online.com/browse/ad3.php it always starts with user 4 and banner 4.. there is no change within its randomness. my datatable is setup with the table of users and then the subtable i am dealing with is userid_2 and the values of this if greater than 0 are the ones that i want to display info Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 13, 2007 Share Posted December 13, 2007 lol you cant use order by rand with paging thats useless better or refresh the page to get another set of data than having a paging but you can oder by your query by their id trough desc or asc Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 13, 2007 Share Posted December 13, 2007 maybe you can try this put the field name of your table in an array and randomly select then so you can order your records differently so you can have diff combination fields and desc/asc Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 13, 2007 Author Share Posted December 13, 2007 It is not useless. you dont quite fully understand.. oy. the site im building advertises banners. what i want to do is to be able to not make one banner have a better chance than the others. So i nailed it down to random order or pagination so thatway no DOUBLES appear. Thats my reasoning. I dont like ur rudeness, but i will see if what u said works ok. Thanks Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 13, 2007 Share Posted December 13, 2007 So the first person would have 3 4 1 2 5 Second person would have 5 2 3 1 4 to obtain this maybe you can do this random those number and store that in session and each time the page loads you get the specific number to use from your select as condition Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted December 13, 2007 Share Posted December 13, 2007 if you hav a user ID..... simply take all teh user ID's and place the m in an array and place that array in a cookie or session for tha tuser. then all you do is do pagination with those ID's.... shuffle($IDs); and then work from that. only selecting rows in a given range in teh shuffles ID array/ I know itsmessay with allthe mysql queries.... but its one method. gdlk Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 13, 2007 Author Share Posted December 13, 2007 I dont follow.. could u give me an example Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 13, 2007 Author Share Posted December 13, 2007 I dont clearly understand.. could u possible show me an example of what u r saying Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 13, 2007 Share Posted December 13, 2007 if you hav a user ID..... simply take all teh user ID's and place the m in an array and place that array in a cookie or session for tha tuser. then all you do is do pagination with those ID's.... shuffle($IDs); and then work from that. only selecting rows in a given range in teh shuffles ID array/ I know itsmessay with allthe mysql queries.... but its one method. gdlk if your talking about unknown user and not admins this not applicable Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 13, 2007 Author Share Posted December 13, 2007 Im just being thrown in circles... i have no idea whats going on right now due to people contradicting each others statements.. Can i have examples please.... because i have a hard time understanding without visuals Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 13, 2007 Share Posted December 13, 2007 ok .. maybe this random a number that represents your BANNER ID --- assuming we get this trough randomizing $x=array(1=>3,2=>1,3=>5); $_SESSION[count] =$_SESSION[count] +1; if ($_SESSION[count] == count($x)){ $_SESSION[count] = 1; } selecct * from table where bannerid = $x[$_SESSION[count]]; just an idea hope it helps Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 13, 2007 Author Share Posted December 13, 2007 [code]$x=array(1=>3,2=>1,3=>5); $_SESSION[count] =$_SESSION[count] +1; if ($_SESSION[count] == count($x)){ $_SESSION[count] = 1; } $data = mysql_query("SELECT * FROM users WHERE userid_2 = $x[$_SESSION[count]]") or die(mysql_error()); I put this down and i have a parse error this line $data = mysql_query("SELECT * FROM users WHERE userid_2 = $x[$_SESSION[count]]") or die(mysql_error());[/code] error = Parse error: parse error, unexpected '[', expecting ']' in /homepages/19/d214339524/htdocs/browse/ad3.php on line 129 Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 13, 2007 Share Posted December 13, 2007 this error is always reported here sorry any ways you $x=array(1=>3,2=>1,3=>5); $_SESSION[count] =$_SESSION['count'] +1; if ($_SESSION['count'] == count($x)){ $_SESSION['count'] = 1; } $data = mysql_query("SELECT * FROM users WHERE userid_2 = {$x[$_SESSION['count']]}") or die(mysql_error()); try Quote Link to comment Share on other sites More sharing options...
czukoman20 Posted December 13, 2007 Author Share Posted December 13, 2007 well it works... but the randomization is still the same.. oy would it have to do with anything in the second SELECT function $x=array(1=>3,2=>1,3=>5); $_SESSION[count] =$_SESSION['count'] +1; if ($_SESSION['count'] == count($x)){ $_SESSION['count'] = 1; } $data = mysql_query("SELECT * FROM users WHERE userid_2 = {$x[$_SESSION['count']]}") or die(mysql_error()); $rows = mysql_num_rows($data); //This is the number of results displayed per page $page_rows = 4; //This tells us the page number of our last page $last = ceil($rows/$page_rows); //this makes sure the page number isn't below one, or more than our maximum pages if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } //This sets the range to display in our query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query("SELECT * FROM users WHERE userid_2 $max") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
play_ Posted December 13, 2007 Share Posted December 13, 2007 once the results are pulled from the database, store them in an array. say. $banners. shuffle($banners) use for loop to output results. Quote Link to comment 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.