Page 1 of 1

Stumped with Pagination

Posted: Fri Jul 21, 2006 12:57 pm
by LiveFree
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:

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++;
	}
Thanks!

Posted: Fri Jul 21, 2006 1:26 pm
by RobertGonzalez
Warning in advance, this is a lot of code...

This is a paging routine I forked from phpBB for my own needs...

Code: Select all

<?php
/**
 * @return str
 * @param $base_url
 * @param $num_items
 * @param $per_page
 * @param $start_item
 * @param $add_prenext_text = 1
 * @desc Returns the pagination string
*/
function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = 1)
{
	global $lang;

	$get_connector = ( strstr($base_url, "?") ) ? '&' : '?';
	$total_pages = ceil($num_items/$per_page);

	if ( $total_pages == 1 )
	{
		return '';
	}

	$on_page = floor($start_item / $per_page) + 1;

	$page_string = '';
	if ( $total_pages > 10 )
	{
		$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;

		for($i = 1; $i < $init_page_max + 1; $i++)
		{
			//$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
			$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . $get_connector . "start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
			if ( $i <  $init_page_max )
			{
				$page_string .= ", ";
			}
		}

		if ( $total_pages > 3 )
		{
			if ( $on_page > 1  && $on_page < $total_pages )
			{
				$page_string .= ( $on_page > 5 ) ? ' ... ' : ', ';

				$init_page_min = ( $on_page > 4 ) ? $on_page : 5;
				$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;

				for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
				{
					//$page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
					$page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . $get_connector ."start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
					if ( $i <  $init_page_max + 1 )
					{
						$page_string .= ', ';
					}
				}

				$page_string .= ( $on_page < $total_pages - 4 ) ? ' ... ' : ', ';
			}
			else
			{
				$page_string .= ' ... ';
			}

			for($i = $total_pages - 2; $i < $total_pages + 1; $i++)
			{
				//$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>'  : '<a href="' . $base_url . "start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
				$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>'  : '<a href="' . append_sid($base_url . $get_connector . "start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
				if( $i <  $total_pages )
				{
					$page_string .= ", ";
				}
			}
		}
	}
	else
	{
		for($i = 1; $i < $total_pages + 1; $i++)
		{
			//$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
			$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . append_sid($base_url . $get_connector . "start=" . ( ( $i - 1 ) * $per_page ) ) . '">' . $i . '</a>';
			if ( $i <  $total_pages )
			{
				$page_string .= ', ';
			}
		}
	}

	if ( $add_prevnext_text )
	{
		if ( $on_page > 1 )
		{
			//$page_string = ' <a href="' . $base_url . "start=" . ( ( $on_page - 2 ) * $per_page ) . '">' . $lang['previous_page'] . '</a>&nbsp;&nbsp;' . $page_string;
			$page_string = ' <a href="' . append_sid($base_url . $get_connector . "start=" . ( ( $on_page - 2 ) * $per_page ) ) . '">' . $lang['previous_page'] . '</a>&nbsp;&nbsp;' . $page_string;
		}

		if ( $on_page < $total_pages )
		{
			//$page_string .= '&nbsp;&nbsp;<a href="' . $base_url . "start=" . ( $on_page * $per_page ) . '">' . $lang['next_page'] . '</a>';
			$page_string .= '&nbsp;&nbsp;<a href="' . append_sid($base_url . $get_connector ."start=" . ( $on_page * $per_page ) ) . '">' . $lang['next_page'] . '</a>';
		}

	}

	$page_string = $lang['goto_page'] . ' ' . $page_string;

	return $page_string;
}
?>
How I loop it...

Code: Select all

<?php
/**************************************************************************************************
*
*	Set paging information
*
**************************************************************************************************/					

$total_items = count($array_to_loop); // example: 47
$per_page = 40;
$start = 0;

if (!isset($_GET['show_all']))
{
    $start = ( isset($_GET['start']) ) ? intval($_GET['start']) : $start;

    if ($start <> 0)
    {
        // Start was set and is now at least 10
        $page_limit = ( ($total_items - $start) < $per_page) ? $total_items : ($start + $per_page);
    }
    else 
    {
        $page_limit = $per_page;
    }
}
else
{
    $start = 0;
    $page_limit = $total_items;
}

// Loop it
for ($i = $start; $i < $page_limit; $i++)
{
    // Do what you want with the display here
    // in the form $array_to_loop[$i]['field_name']
}
?>

Posted: Fri Jul 21, 2006 1:30 pm
by LiveFree
Damn thats sweet-o!

Looking over it; I fail to see the place where I would include my hidden form data sicne I need to keep the POST info.

Thanks!

Posted: Fri Jul 21, 2006 7:35 pm
by RobertGonzalez
In the paging function, where you see the 'start=' stuff, add a line for 'data=' and add your past data there. Make an entry into the function parameter list that houses post data and use it in the function.

This is only a suggestion. There are proabably a dozen different, better ways to do it but that is the first thing that came to mind.

Posted: Fri Jul 21, 2006 10:54 pm
by LiveFree
Now that I think about it more, does anyone have a more scaled-down pagination system?

Posted: Sat Jul 22, 2006 7:29 am
by timvw
Could at least bother to search the 'code snippets' forum etc...