Jump to content

Recommended Posts

Currently on my site it is built to show 9 products at one time on the page.  I want to build a view all link that would like to all the products in that particular category. 

 

My link is:

 

echo("<a class=\"active\" href=\"$url" . "\">" . "View All</a>");

 

 

I can not figure out how to reconfigure the current code to make this happen.  Have tried all different avenues have come close but can not get it.  Baffled - it seems easy but can not reach the solution.  Have attached my current function in my function.php file and my current code that is on the page right now.

 

 

hyperlink.txt

Link to comment
https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/
Share on other sites

If you want to display all the records, you could simply remove the "LIMIT" part of your SQL query in displayProducts.

function displayProducts($link, $cat, $prod, $p, $displayAll) {

...
$query = "SELECT * FROM prodcat WHERE codes LIKE '$cat' OR codes LIKE '%,$cat' OR codes LIKE '$cat,%' OR codes LIKE '%,$cat,%' ORDER BY sku";

if (!$displayAll) {
 $query .= " LIMIT $minP, $rangeP";
}

...

}

A warning note: If you're going to put that live, please read a little bit on SQL injection as your code is not secure.  There's this stackoverflow article. I also wrote an article on Injections (I'd like your comments if you read it).

Also, you could use a "SELECT COUNT()" instead of retrieving all the rows from the database, it will be more efficient.

Thanks for the help still learning here.  I understand how to get it to show all the products -  the part I am hung up on is how to get it to show all the products when the "view all" link is clicked. 

 

 

Here is my products_listing.php

<?php 
$url = selfURL();
if (isset($_GET['error'])) { $error = $_GET['error']; }
if (isset($_GET['search'])) { $search = $_GET['search']; }
if (isset($_GET['related'])) { $related = $_GET['related']; }

if (isset($_SESSION['myusername'])){$userName = $_SESSION['myusername'];} else {$userName = NULL;}

//************** Product Detail ****************
if (!empty($prod)) {
 if (isProduct($link, $prod)) {
	require_once('products_listing_detail.php');
	} else {
		echo ("<h1>Sorry! This product ($prod) is not available.</h1>
		<p>It's either an invalid SKU or the product has been discontinued and is no longer listed.</p>
		");
	}
	
	echo ("</div>");
	
	


//************** Product Listing ****************
} elseif (!empty($cat) && $numResults != 0 ) {
	require_once('products_listing_listing.php');

//************** Empty Category Listing ****************
} else {
	require_once('products_listing_category.php');
}


?>

All you need to do is add another $_GET variable, maybe call it limit? Then do an if statement to change the query depending on the variables value.

 

To change the query you can do 1 of 2 things -

  1. Change the whole query
  2. Add a $limit variable to the query

Something like this - 

$limit = (isset($_GET['limit']))? $_GET['limit'] : 9;

if( $limit == 9 )
{
 $limit = 'LIMIT 0, 9';
}
else
{
 $limit = '';
}

$query = mysql_query('SELECT * FROM table ' . $limit . ';');
Edited by adam_bray

Can you show us the pagination() function? Regarding the statement as seen in hyperlink.txt that you provided, is it correct?

pagination($numResults,$p,$rangeP);

 

I ask because I have to assume the function echoes its results instead of returning them.

The pagination is at the top - It appears to work properly.
 it displays like this:    

 

Category Name       1-9 of 250       

 

The pagination would obviously have to change if it was showing "view all" too.   I need to keep the pages displaying 9 products per page UNLESS someone chooses  "view all" link.  Trying to figure out how to link "view all" to all the products is the part that I am really stuck on.

How does one select page 2 (second group of nine)?

 

My experience gives me pagination that looks like:

Page 1 of 6150 [1] 2 [3] Next All

each being a link with ?page=X as the querystring where X is a page number, the current page number plus 1, or the word All.

 

The PHP tests for the value of $_GET['page'] and sets the query parameters appropriately.

That is basically how it is written (I did not write the code I'm editing it so I'm a little unfamiliar with how it was all initially setup.  Here is the code  - "all" feature was not built into it.

<?php 
echo ("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin-bottom:10px; margin-top:2px;\">
	  <tr>
	  <td width=\"33%\" height=\"30\" style=\"vertical-align:middle;\">
	  ");

breadcrumbs('products',$cat,$p); //Breadcrumbs

echo ("</td>
	  <td style=\"text-align:center; vertical-align:middle;\" width=\"33%\">" . (($p*($rangeP))-($rangeP-1)) . " – "); //Page and listing count
	  if (($p*($rangeP)) > ($numResults)) { 
			echo ($numResults); 
	  } else { 
			echo ($p*($rangeP)); 
	  } 
	  echo (" of " . ($numResults) . "</td><td style=\"vertical-align:middle;\" width=\"33%\" >
	  ");
	     


																						   
	  pagination($numResults,$p,$rangeP); //Pagination
	  
	  echo ("</td></tr></table>
	  ");


Here is the pagination function:

// Pagination

function pagination ($numResults, $activePage, $rangeP) {
	
	//Variables
	
	$url = selfURL(); //Get Current URL
	$url = preg_replace('/\&p\=\\d+/', '', $url);
	$i = 1; //Start loop at 1
	$p = $activePage; //P changes in loop
	$numPages = ceil(($numResults)/($rangeP)); //Total number of pages (result rounded up)
	if (is_decimal(($numResults/($rangeP))) == false) {$numPages = $numPages;} //Subtracts 1 from numPages if whole number. Fixes extra page error.
	if ($numPages < 5) {$pagCount = $numPages;} else {$pagCount = 5;} //Pagenation button count. Equal to $numPages if less than $numPages.
	if ($pagCount == 4) { $offset = $p-1; } else { // Offset account for four pages to correct last active page position
	if ($p <= 3) { $offset = $p-1; } elseif (($p > 3) && ($p <= $numPages -2)) { $offset = 2; } else { $offset = ($p - $numPages) + 4; }} //Sets offset depending on link position.
	
	if ($numPages > 1) {
	
	echo("<div class=\"paginationWrap\"><ul class=\"pagination\">"); //Start Display
	
	if ($activePage > 2) { echo("<li style=\"border:none; font-size:18px; margin-top:-6px;\"><a href=\"$url&p=1\" class=\"paginationArrows\">«</a></li>"); }
	if ($activePage > 1) { echo("<li style=\"border:none; font-size:18px; margin-top:-6px;\"><a href=\"$url&p=" . ($activePage-1) . "\" class=\"paginationArrows\">‹</a></li>"); }
    while ($i <= $pagCount) { echo("<li>");
		if ($p-$offset != $activePage) {	echo("<a href=\"$url&p=" . ($p-$offset) . "\">" . ($p-$offset) . "</a>");} else { echo ("<b class=\"active\">" . ($p-$offset) . "</b>");}
		
		echo("</li>");
		$p++;
		$i++;  
		}

	  if ($activePage < $numPages) { echo("<li style=\"border:none; font-size:18px; margin-top:-6px;\"><a href=\"$url&p=" . ($activePage+1) . "\" class=\"paginationArrows\">›</a></li>"); }
	  if ($activePage < ($numPages-1)) {echo("<li style=\"border:none; font-size:18px; margin-top:-6px;\"><a href=\"$url&p=" . $numPages . "\" class=\"paginationArrows\">»</a></li>"); }
	  echo("</ul></div>");
	  
	  
	}
	}

Two things:

Where is $p = $_GET['p']?

 

I would try:

Was:
if ($activePage < ($numPages-1)) {echo("<li style=\"border:none; font-size:18px; margin-top:-6px;\"><a href=\"$url&p=" . $numPages . "\" class=\"paginationArrows\">»</a></li>"); }
echo("</ul></div>");
 
Now:
if ($activePage < ($numPages-1)) {echo("<li style=\"border:none; font-size:18px; margin-top:-6px;\"><a href=\"$url&p=" . $numPages . "\" class=\"paginationArrows\">»</a></li>"); }
echo("<li style=\"border:none; font-size:18px; margin-top:-6px;\"><a href=\"$url&p=all\" class=\"paginationArrows\">All</a></li>");
echo("</ul></div>");

I do not expect any results -- not until all the code that uses $p (or it's matched parameter in called functions, such as $activePage) is tested against the phrase 'all' and dealt with appropriately, instead of assuming the value will only be like an integer.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.