Jump to content

Need Assistance Adding A "View All" Link To My Paging Script!


designer76

Recommended Posts

My paging script below works perfectly, all I need to know is how would I add a "view all" link that would allow users to view all of the results on one page. I have tried playing with the script myself, but I can't come up with the right code to get the link to work properly. When the "view all" link is clicked I would like for the paging (and all of the current paging links) to remain on the page and be functional still, just like the paging on the middle of this page here: http://www.rocawear.com/nshop/product.php?view=listing&groupName=mALLtops&dept=men&both=yes How do I go about doing that? Thanks in advance for any help given.

 

//////////////PAGING///////////////////

// How many items to show per page
$limit = 9;

$page = $_GET['page'];

if($page) 
$start = ($page - 1) * $limit;

// If no page var is given, set start to 0
else
$start = 0;

// How many adjacent pages should be shown on each side?
$adjacents = 3;
       
// First get total number of rows in data table
// If you have a WHERE clause in your query, make sure you mirror it here

$pagingconstruct = "SELECT COUNT(*) as num FROM search WHERE $construct";

$total_pages = mysql_fetch_assoc(mysql_query($pagingconstruct));

$total_pages = $total_pages['num'];
       
// Setup vars for query
// Your file name  (the name of this file)
$targetpage = "search.php";
       
// Setup page vars for display

// If no page var is given, default to 1
if ($page == 0) $page = 1;     

// Previous page is page - 1
$prev = $page - 1;

// Next page is page + 1
$next = $page + 1;

// Lastpage is = total pages / items per page, rounded up
$lastpage = ceil($total_pages/$limit);

// Last page minus 1
$lpm1 = $lastpage - 1;
       
// Now we apply our rules and draw the pagination object.
// We're actually saving the code to a variable in case we want to draw it more than once.
$pagination = "";

if($lastpage > 1)
{      
$pagination .= "<div id='search_container'><div id='paging'>";

// Previous button
if ($page > 1)
$pagination .= "<a class='pages' href='$targetpage?page=$prev&search=$search'>previous</a>";

else
$pagination .= "<span class='disabled'>previous</span>";       
               
// Pages
// Not enough pages to bother breaking it up
if ($lastpage < 7 + ($adjacents * 2))
{      
for ($counter = 1; $counter <= $lastpage; $counter++)
{

if ($counter == $page)
$pagination .= "<span class='current'>$counter</span>";

else
$pagination .= "<a class='pages' href='$targetpage?page=$counter&search=$search'>$counter</a>";                                
}
}

// Enough pages to hide some
elseif($lastpage > 5 + ($adjacents * 2))
{

// Close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))               
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{

if ($counter == $page)
$pagination .= "<span class='current'>$counter</span>";

else
$pagination .= "<a class='pages' href='$targetpage?page=$counter&search=$search'>$counter</a>";                                
}

$pagination .= "<a class='pages' href='$targetpage?page=$lpm1&search=$search'>$lpm1</a>";
$pagination .= "<a class='pages' href='$targetpage?page=$lastpage&search=$search'>$lastpage</a>";              
}

// In middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination .= "<a class='pages' href='$targetpage?page=1&search=$search'>1</a>";
$pagination .= "<a class='pages' href='$targetpage?page=2&search=$search'>2</a>";

for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination .= "<span class='current'>$counter</span>";

else
$pagination .= "<a class='pages' href='$targetpage?page=$counter&search=$search'>$counter</a>";                                
}
$pagination .= "<a class='pages' href='$targetpage?page=$lpm1&search=$search'>$lpm1</a>";
$pagination .= "<a class='pages' href='$targetpage?page=$lastpage&search=$search'>$lastpage</a>";              
}

// Close to end; only hide early pages
else
{
$pagination .= "<a class='pages' href='$targetpage?page=1&search=$search'>1</a>";
$pagination .= "<a class='pages' href='$targetpage?page=2&search=$search'>2</a>";

for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{

if ($counter == $page)
$pagination .= "<span class='current'>$counter</span>";

else
$pagination .= "<a class='pages' href='$targetpage?page=$counter&search=$search'>$counter</a>";                                
}
}
}
               
// Next button
if ($page < $counter - 1)
$pagination .= "<a class='pages' href='$targetpage?page=$next&search=$search'>next</a>";

else
$pagination .= "<span class='disabled'>next</span>";
$pagination .= "</div></div>\n";               
}
}

///////////////END PAGING///////////////

The link you provided actually shows how it's done, just point at the 'view all' link and you will see that it sets the page attribute in the url to 'viewall'. So you need a check in your script to see if page = viewall. If it is then you need to use the same script you currently have to calculate the links and then use a DIFFERENT query to actually fetch the results for that page. I can't be too specific as you don't include the code for fetching the items. Needless to say you probably don't want to change any of what you have posted.

 

 

Thanks for the reply Cags, is this the code that you need for getting the results?

 

This Code Is Before The Paging Script

	//connect to database
	$con = mysql_connect("localhost", $name, $word);
	if (!$con)
	{
	header('Location: http://localhost/database.php');
	}
	mysql_select_db($databasename);

		//explode search term
		$search_exploded = explode(" ",$search);

		foreach($search_exploded as $search_each)
		{

		//construct query
		$x++;
		if ($x==1)
			$construct .= "keywords LIKE '%|$search_each|%'";
		else 
			$construct .= "AND keywords LIKE '%|$search_each|%'";		
		}

 

This Code Is After The Paging Script

$start_index = (($page - 1) * $limit) + 1;

	$end_index = $start_index + $limit -1;

	if ($end_index > $total_pages) $end_index = $total_pages;

	$result_range = "$start_index - $end_index"; 

	//echo out construct		
	$construct = "SELECT * FROM search WHERE $construct LIMIT $start, $limit";
	$run = mysql_query($construct);
	{

$final_page = $lastpage;
if ($final_page < $page) $final_page = $page;

$result = $run;

// find how many results there are
$total_results = mysql_num_rows($result);

// user specifies column count
$num_cols = 3;

// calculate row count
$num_rows = ceil($total_results / $num_cols);

// loop through rows
for($row = 0; $row < $num_rows; $row++)
{
// output table (or just <tr>'s if you prefer)
    $output_html .= "deleted to save space";
}
}

The code after the script is what needs checking.

 

$construct = "SELECT * FROM search WHERE $construct LIMIT $start, $limit";

 

You need to check if $_GET['page'] is showall, if it is then you need to run a different query to that, one without a limit. Something like

 

if( $_GET['page'] == 'showall' ) {
   $construct = "SELECT * FROM search WHERE $construct";
} else {
   $construct = "SELECT * FROM search WHERE $construct LIMIT $start, $limit";
}

You may or may not have to make a small change to your original script just to stop $start = ($page - 1) * $limit; from breaking (as it will be a string not an integer).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.