php - Using Pagination with Mysql Queries -


i unable maintain variable pagination script. hoping help.

i using function user can choose categories displayed. i'm using pagination script found online , confronted issue, when choose "page 2" $dyncat no longer parsed in url because pagination script creates new url , uses also.

could me out using pagination variable query? thank you.

error notice: undefined index: dyncat ----/dyncat.php on line 32 understand why undefined, don't know how integrate , maintain user's query (i.e. variable url) pagination script. again.

page1 index.php (user selects option)

<?php //generate , list categories include "storescripts/connect_to_mysql.php"; $dyncat = ""; $data = mysql_query("select category, id products group category") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { $listcategory = $info["category"]; $dyncat .= ' <li> <a href="dyncat.php?dyncat='.$listcategory.'" > '.$listcategory.' </a> </li> '; } mysql_close(); ?> <?php echo $dyncat; ?> 

page2 dyncat.php (user views selection pagination)

<?php error_reporting(e_all); ini_set('display_errors', '1'); ?> <?php //generate , list categories include "storescripts/connect_to_mysql.php"; $dyngallery = ""; $dyncat = ""; $data = mysql_query("select category, id products group category") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { $listcategory = $info["category"]; $dyncat .= ' <li> <a href="dyncat.php?dyncat='.$listcategory.'" > '.$listcategory.' </a> </li> '; } mysql_close(); ?> <?php //query user selection & pagination include('storescripts/connect_to_mysql.php'); $cat = mysql_escape_string($_get['dyncat']); $tablename="products"; $targetpage = "dyncat.php"; $limit = 3; $query = "select count(*) num $tablename category = '$cat'"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages['num']; $stages = 3; $page = mysql_escape_string(isset($_get['page'])) ? mysql_escape_string($_get['page']) : 0; if($page){ $start = ($page - 1) * $limit; }else{ $start = 0; } // category data , images $query1 = "select * $tablename category = '$cat' order views desc limit $start, $limit"; $result = mysql_query($query1); // initial page num setup if ($page == 0){$page = 1;} $prev = $page - 1; $next = $page + 1; $lastpage = ceil($total_pages/$limit); $lastpagem1 = $lastpage - 1; $paginate = ''; if($lastpage > 1) { $paginate .= "<div class='paginate'>"; // previous if ($page > 1){ $paginate.= "<a href='$targetpage?page=$prev#gallery'>previous</a>"; }else{ $paginate.= "<span class='disabled'>previous</span>"; } // pages if ($lastpage < 7 + ($stages * 2)) // not enough pages breaking { ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} } } elseif($lastpage > 5 + ($stages * 2)) // enough pages hide few? { // beginning hide later pages if($page < 1 + ($stages * 2)) { ($counter = 1; $counter < 4 + ($stages * 2); $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} } $paginate.= "..."; $paginate.= "<a href='$targetpage?page=$lastpagem1#gallery'>$lastpagem1</a>"; $paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>"; } // middle hide front , elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) { $paginate.= "<a href='$targetpage?page=1#gallery'>1</a>"; $paginate.= "<a href='$targetpage?page=2#gallery'>2</a>"; $paginate.= "..."; ($counter = $page - $stages; $counter <= $page + $stages; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} } $paginate.= "..."; $paginate.= "<a href='$targetpage?page=$lastpagem1#gallery'>$lastpagem1</a>"; $paginate.= "<a href='$targetpage?page=$lastpage#gallery'>$lastpage</a>"; } // end hide pages else { $paginate.= "<a href='$targetpage?page=1#gallery'>1</a>"; $paginate.= "<a href='$targetpage?page=2#gallery'>2</a>"; $paginate.= "..."; ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "<span class='current'>$counter</span>"; }else{ $paginate.= "<a href='$targetpage?page=$counter#gallery'>$counter</a>";} } } } // next if ($page < $counter - 1){ $paginate.= "<a href='$targetpage?page=$next#gallery'>next</a>"; }else{ $paginate.= "<span class='disabled'>next</span>"; } $paginate.= "</div>"; } mysql_close(); ?> <?php $productcount = mysql_num_rows($result); // count output amount while($row = mysql_fetch_array($result)) { $id = $row["id"]; $product_name = $row["product_name"]; $price = $row["price"]; $category = $row["category"]; $subcategory = $row["subcategory"]; $date_added = strftime("%b %d, %y", strtotime($row["date_added"])); $dyngallery .= ' <div> <li> <a href="product.php?id=' . $id . '#gallery"> <img style="border:#fff 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="180" height="255" border="1" style="opacity: 0;/></a> <h4> <a name= ' . $id . ' id= ' . $id . ' value= ' . $id . ' href="product.php?id= ' . $id . '#gallery"> ' . $product_name . ' </a> </h4> <p>'. $category .'</p> <p>'. $subcategory .'</p> <span> $' . $price . '</span> </li> </div> '; } ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <?php include ("../header.php");?> <?php include ("../menu.php");?> <a id="shop"></a> <div class="body"> <div class="sidebar"> <div class="first"> <h2><a href="index.php">dora designs</a></h2> <!-- dynamic categories --> <ul> <?php echo $dyncat; ?> </ul> </div>  </div> <div class="content"> <div class="figure"> <img src="/images/galleryholder.png" alt=""/> </div> <div class="products"> <div class="paging" align = "center"> <a id="gallery"></a> <? echo $paginate;?> <? echo '</br>'. $cat;?> <? echo '</br>'. $total_pages.' results'; ?> </div> <ul> <?php echo $dyngallery; ?> </ul> 

i hate dump code in post @ loss , have never used pagination before. appreciated.

change ever instance of

$targetpage?page= 

to

$targetpage?dyncat=$cat&page=  

you want 2 copies of $cat 1 db requires escaping mysql_escape_string() other url urlencode()

replace line

$cat = mysql_escape_string($_get['dyncat']); 

with

$cat=urldecode($_get['dyncat']); //from url, raw value display $cat_mysql = mysql_escape_string($cat); //use in mysql queries $cat_url=urlencode($cat); //use in url 

then

$targetpage?dyncat=$cat&page=

will be

$targetpage?dyncat=$cat_url&page=


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -