Forum Moderators: coopster

Message Too Old, No Replies

PAGNATION and Random Result help

         

phil2564

3:06 am on Jul 17, 2004 (gmt 0)

10+ Year Member



I have a DB of models.
I want when you do a search for say a male in the state of florida the results come back in random order each time. The problem I am having is in my pagnation when you go to page 2 you get some of page 1 results. Is there anyway to alter my script below to give back random results in a set and hold them so the pagnation will work correctly, So basically it will not scramble the results until the person does another search. Can anyone help me get through this from start to finish. Below is my script if you could alter it to do this please post.
____________________________________________________
<?php

$conn=mysql_connect("localhost","","");
$rs=mysql_select_db("mydb",$conn);

/* The str_replace function replaces the string accordingly, for example 10,11,12,13 to 10','11','12','13. Later in query its formated as '10','11','12','13' Which is the standard format to use in WHERE IN Query. */

$mysex = str_replace(",", "','", $_POST['sex']);
$myage = str_replace(",", "','", $_POST['age']);
$myweight = str_replace(",", "','", $_POST['weight']);
$myheight = str_replace(",", "','", $_POST['height']);
$myhaircolor = str_replace(",", "','", $_POST['haircolor']);
$myeyecolor = str_replace(",", "','", $_POST['eyecolor']);
$mystate = str_replace(",", "','", $_POST['state']);
// get the pager input values
$mypage = "";
$mypage = $_POST['page'];
$limit = 5; // 5 records per page...you can change it according to your wish.

$result = mysql_query("SELECT count(*) FROM models WHERE sex IN ('$mysex') AND age IN ('$myage') AND weight IN ('$myweight') AND height IN ('$myheight') AND haircolor IN ('$myhaircolor') AND eyecolor IN ('$myeyecolor') AND state IN ('$mystate')", $conn);

$total = mysql_result($result, 0, 0);

// work out the pager values
$pager = Pager::getPagerData($total, $limit, $mypage);
$offset = $pager->offset;
$limit = $pager->limit;
$mypage = $pager->page;

$answer = "SELECT * FROM models WHERE sex IN ('$mysex') AND age IN ('$myage') AND weight IN ('$myweight') AND height IN ('$myheight') AND haircolor IN ('$myhaircolor') AND eyecolor IN ('$myeyecolor') AND state IN ('$mystate') ORDER BY RAND(0.9233482386203
) LIMIT $offset, $limit ";

$rs=mysql_query($answer,$conn) or die("Error occured in SQL -> ".mysql_error());

if (mysql_num_rows($rs) == 0)
{

echo("No models with these Parameters where found!...please try again! ");
}
echo("example.com SEARCH RESULTS ");

while ($row =mysql_fetch_array($rs) )
{ print"<table width = 551 border=0>";

echo ("<tr><td><b> ID:</b> " .$row ["id"]."&nbsp; &nbsp;");
echo ("<b> Name:</b> " .$row ["name"]."&nbsp; &nbsp;");
echo ("<b> Age:</b> ".$row["age"]."&nbsp; &nbsp;");
echo ("<b> Weight:</b> ".$row["weight"]."&nbsp; lbs &nbsp;");
echo ("<b> Height:</b> ".$row["height"]."<br>");
echo ("<b> Hair Color:</b> ".$row["haircolor"]."&nbsp; &nbsp;");
echo ("<b> Eye Color:</b> ".$row["eyecolor"]."&nbsp; &nbsp;");
$mylink= $row["link"];
$go ="More from this model";
echo "<a href=\"$mylink\" target='_blank'>$go</a></tr></td><br>";
$filepath = $row["filepath"];
echo "<td><img src='$filepath'></td><hr></tr>";

print"</table>";
}

?>
<?php

class Pager
{
function getPagerData($numHits, $limit, $page)
{
$numHits = (int) $numHits;
$limit = max((int) $limit, 1);
$page = (int) $page;
$numPages = ceil($numHits / $limit);

$page = max($page, 1);
$page = min($page, $numPages);

$offset = ($page - 1) * $limit;

$ret = new stdClass;

$ret->offset = $offset;
$ret->limit = $limit;
$ret->numPages = $numPages;
$ret->page = $page;

return $ret;
}
}

?>
<form action="mmf_result.php" method="POST" target="_self">
<input name="state" type="hidden" id="state" value="<?php echo $_POST['state'];?>">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'];?>">
<input type="hidden" name="age" value="<?php echo $_POST['age'];?>">
<input type="hidden" name="weight" value="<?php echo $_POST['weight'];?>">
<input type="hidden" name="height" value="<?php echo $_POST['height'];?>">
<input type="hidden" name="haircolor" value="<?php echo $_POST['haircolor'];?>">
<input type="hidden" name="eyecolor" value="<?php echo $_POST['eyecolor'];?>">
<select name="page">
<?php

// output paging system
for ($i = 1; $i <= $pager->numPages; $i++)
{
if ($i == $mypage)
echo "<option value='$i' SELECTED> $i </option>";
else
echo "<option value='$i'> $i </option>";
}

?>
</select>
<input type="submit" name="submit" value="GO">
</form>

[edited by: jatar_k at 5:45 pm (utc) on July 17, 2004]
[edit reason] generalized url [/edit]

jatar_k

5:30 pm on Jul 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



trouble is with random results, if you need to keep showing more that are also randomized with out repeating, the next query will need to know which ones were already displayed.

you will need to store or pass the results displayed to the next page. You could use a session var or cookie possibly. You could also restructure your queries so there is some wethod to the random results, which wouldn't actually make them random anymore.

Maybe give them search options or only show random results on the first page, not sure.

coopster

6:08 pm on Jul 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<edit>I was just about to post when I noticed jatar_k had responded. I echo his thoughts, but I'll include something here anyway.<edit>

As with any other stateless connection, you'll have to devise a way to control your found data set until the user requests another search. Since you are using a random result set, I guess I would turn to sessions. If the result set was so large, maybe you could just store the primary keys and develop a different query to grab the dataset on any given return...just some ideas.