RChilton Posted April 21, 2014 Share Posted April 21, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/ Share on other sites More sharing options...
dalecosp Posted April 21, 2014 Share Posted April 21, 2014 You'll need a flag in the GET string. In product_listings.php, check for the existence of the flag and the proper value; if it's set properly, use $totalresult in the while clause instead of $result..... Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476868 Share on other sites More sharing options...
mogosselin Posted April 21, 2014 Share Posted April 21, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476869 Share on other sites More sharing options...
RChilton Posted April 22, 2014 Author Share Posted April 22, 2014 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'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476967 Share on other sites More sharing options...
adam_bray Posted April 22, 2014 Share Posted April 22, 2014 (edited) 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 - Change the whole query 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 April 22, 2014 by adam_bray Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476968 Share on other sites More sharing options...
bsmither Posted April 22, 2014 Share Posted April 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476979 Share on other sites More sharing options...
RChilton Posted April 22, 2014 Author Share Posted April 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476986 Share on other sites More sharing options...
bsmither Posted April 22, 2014 Share Posted April 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476987 Share on other sites More sharing options...
RChilton Posted April 22, 2014 Author Share Posted April 22, 2014 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> "); Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1476988 Share on other sites More sharing options...
RChilton Posted April 23, 2014 Author Share Posted April 23, 2014 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>"); } } Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1477085 Share on other sites More sharing options...
bsmither Posted April 23, 2014 Share Posted April 23, 2014 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>"); Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1477096 Share on other sites More sharing options...
RChilton Posted April 23, 2014 Author Share Posted April 23, 2014 I didn't write the code so I having to dig through it. I do not see anywhere where he actually uses $p = $_GET['p']? The above code does not work. It links but displays nothing.... Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1477108 Share on other sites More sharing options...
bsmither Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287918-hyperlink-to-view-all/#findComment-1477111 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.