Stumped with Pagination
Posted: Fri Jul 21, 2006 12:57 pm
Well so I thought my hidden form would work.
My Situation: I have a music search DB that uses a searching 'hit' system to determine what gets displayed. This uses POST data and this cannot be lost. Only 10 records can be displayed at a time.
My Problem: I have tried a number of pagination technices including the hidden form technie, sessions, the whole shabang but I can never (a get the right # of #s to displau or when they click on the link; the post data doesnt 'take' and it goes back to search form
My code:
Thanks!
My Situation: I have a music search DB that uses a searching 'hit' system to determine what gets displayed. This uses POST data and this cannot be lost. Only 10 records can be displayed at a time.
My Problem: I have tried a number of pagination technices including the hidden form technie, sessions, the whole shabang but I can never (a get the right # of #s to displau or when they click on the link; the post data doesnt 'take' and it goes back to search form
My code:
Code: Select all
$genre = ParseString($_POST['genre']);
$secGenre = ParseString($_POST['secGenre']);
$artist = ParseString($_POST['artist']);
$length = $_POST['length'];
$royalty = $_POST['royalty'];
$format = $_POST['format'];
$parts = explode('-', $length);
$length = $parts[1] - 1;
if ($royalty != 0.00){
$parts = explode('-', $royalty);
$royalty = $parts[1] - 1;
}
$output .= "<form method='POST' action='music.php' name='form'>";
$output .= "<input type='hidden' name='genre' id='genre' value='".$_POST['genre']."' />";
$output .= "<input type='hidden' name='secGenre' id='secGenre' value='".$_POST['secGenre']."' />";
$output .= "<input type='hidden' name='artist' id='artist' value='".$_POST['artist']."' />";
$output .= "<input type='hidden' name='length' id='length' value='".$_POST['length']."' />";
$output .= "<input type='hidden' name='royalty' id='royalty' value='".$_POST['royalty']."' />";
$output .= "<input type='hidden' name='format' id='format' value='".$_POST['format']."' />";
$output .= "</form>"; # All that was for pagination, hidden form galore!
$offset = (isset($_POST['offset'])) ? intval($_POST['offset']) : 0;
$total = $offset + 10;
$output .= "<b><i>Pages: </i></b><br />";
for ($i = 0; $i < mysql_num_rows(mysql_query("SELECT * FROM music")); $i++){
$output .= "<a href='music.php?offset=".$i + 10 . "' onclick='form.submit()'>$i</a>";
}
if ($total == 10) $sql = mysql_query("SELECT * FROM `music` LIMIT 10"); elseif ($total > 10) $sql = mysql_query("SELECT * FROM `music` LIMIT $offset, $total");
$points = 0; // Init. the 'SEARCH POINTS' counter
$hits = array(); // The array of the records who have a high enough point value
$hit_points = 3; // The numer of points needed for the record to be considered a 'hit'
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
$range1 = $royalty + 1;
$range2 = $royalty - 1;
if ($genre){
$genre = str_replace("#", "\#", $genre);
if (preg_match("#(.*?)$genre(.*?)#i", $row['genre'])) $points += 1;
}
if ($secGenre){
$secGenre = str_replace("#", "\#", $secGenre);
if (preg_match("#(.*?)$secGenre(.*?)#i", $row['secGenre'])) $points += 1;
}
if ($artist){
if (preg_match("#(.*?)$artist(.*?)#i", $row['artistName'])) $points += 1;
}
if (strtoupper($format) == strtoupper($row['format'])) $points += 1;
$range1 = $length + 1;
$range2 = $length - 1;
$length = round($length);
$row['length'] = round($row['length']);
if (preg_match("#($range1|$range2|$range)#i", $row['length'])) $points += 1;
if ($format == 'any') $points += 1;
if ($points >= $hit_points) $hits[] = $row['id'];
unset($points);
}
$num = (isset($_POST['offset'])) ? $_POST['offset'] : 0;
$offset = (isset($_POST['offset'])) ? intval($_POST['offset']) : 0;
$limit = $offset + 10;
foreach ($hits as $id){
$sql = mysql_query("SELECT id, title, genre, fee, length, format FROM `music` WHERE id=$id") OR DIE (mysql_error());
$row = mysql_fetch_array($sql) OR DIE (mysql_error());
$output .= "<b><a href='music.php?op=info&id=".$id."'>".$row['title']."</a></b><br />";
$output .= "<i>".$row['genre']."</i><br />";
$output .= "\$".$row['fee'].", ".$row['length']." Minutes<br />";
$output .= "Format: ".$row['format']."<p>";
$num++;
}