Help on pagination please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gnawz
    New Member
    • Nov 2006
    • 64

    #1

    Help on pagination please

    I have a pagination function I am using in a file called functions.php as below
    Code:
    <? //Pagination functions  function getPagingQuery($sql, $itemPerPage = 10) {    if (isset($_GET['page']) && (int)$_GET['page'] > 0)    {       $page = (int)$_GET['page'];    }    else    {       $page = 1;    }        // start fetching from this row number    $offset = ($page - 1) * $itemPerPage;        return $sql . " LIMIT $offset, $itemPerPage"; }  /*    Get the links to navigate between one result page to another.     */ function getPagingLink($sql, $itemPerPage = 10, $strGet = '') {    $result        = dbQuery($sql);    $pagingLink    = '';    $totalResults  = dbNumRows($result);    $totalPages    = ceil($totalResults / $itemPerPage);        // how many link pages to show    $numLinks      = 10;            // create the paging links only if theres more than one page of results    if ($totalPages > 1) {           $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;               if (isset($_GET['page']) && (int)$_GET['page'] > 0) {          $pageNumber = (int)$_GET['page'];       } else {          $pageNumber = 1;       }              // print 'previous' link only if its not       // on page one       if ($pageNumber > 1) {          $page = $pageNumber - 1;          if ($page > 1) {             $prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";          } else {             $prev = " <a href=\"$self?$strGet\">[Prev]</a> ";          }                          $first = " <a href=\"$self?$strGet\">[First]</a> ";       } else {          $prev  = ''; // on page one, don't show 'previous' link          $first = ''; // nor 'first page' link       }           // print 'next' link only if its not       // on the last page       if ($pageNumber < $totalPages) {          $page = $pageNumber + 1;          $next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> ";          $last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> ";       } else {          $next = ''; // if on the last page, don't show 'next' link          $last = ''; // nor 'last page' link       }        $start = $pageNumber - ($pageNumber % $numLinks) + 1;       $end   = $start + $numLinks - 1;                    $end   = min($totalPages, $end);              $pagingLink = array();       for($page = $start; $page <= $end; $page++)   {          if ($page == $pageNumber) {             $pagingLink[] = " $page ";   // no need to create a link to current page          } else {             if ($page == 1) {                $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";             } else {                   $pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";             }             }           }              $pagingLink = implode(' | ', $pagingLink);              // return the page navigation link       $pagingLink = $first . $prev . $pagingLink . $next . $last;    }        return $pagingLink; } ?>
    I include that functions.php in every page because it contains all the functions I need

    However, when I add the pagination to my DB functions containing SQLs, it doesn't work. Code below:

    Code:
    <? function allsales_Today() {    $rowsPerPage = 5;     //$sql = 'SELECT * FROM cstocksales WHERE SaleDate = current_date';    $sql = 'SELECT * FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate';        //$result = dbQuery($sql);    $result     = dbQuery(getPagingQuery($sql, $rowsPerPage));    $pagingLink = getPagingLink($sql, $rowsPerPage);        if (dbNumRows($result) == 0) //if none       {         echo '<p><font color=#CC0000><h4>No sales for today were found in the records...</h4></font></p>';       echo 'Go back <a href="index.php">Here</a> for another search.';    }    else    {       echo "<h5>Today's sales sales:<p></h5>";       echo '<table width="100%"  border="1" bordercolor="#000000">';      echo'<tr>';       echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';       echo'<th scope="col" bgcolor="#66FFFF">Category</th>';       echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';       echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';       echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';       echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';       echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';     echo'</tr>';      while($row = dbFetchArray($result))     // while($row = mysql_fetch_array($result))      {                 echo'<tr>';          echo'<td align="center">';          $SaleID = $row["SaleID"];                   echo $SaleID. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Category = $row["Category"];                   echo $Category. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Brand = $row["Brand"];                   echo $Brand. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Quantity = $row["Quantity"];                   echo $Quantity. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $PaidBy = $row["PaidBy"];                   echo $PaidBy. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Cost = $row["Cost"];                   echo $Cost. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $SaleDate = $row["SaleDate"];                   echo $SaleDate. "<br>";                   echo'</td align="center">';                   echo '</tr>';              }    echo'</table>';        //paginating link    echo $pagingLink;        echo "<br>"; echo "<strong>Sales figures:</strong>"; echo "<hr>";        $sql_total = 'SELECT PaidBy, SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate GROUP BY PaidBy';        $result = dbQuery($sql_total);             while($row = dbFetchArray($result))         {        echo "Total sales by ". $row['PaidBy']. "<font color=#CC0000> =  Kshs <strong>". $row['SUM(Cost)']; echo "</font></strong>";       echo "&nbsp;&nbsp;"; echo"|"; echo "&nbsp;&nbsp;";       }           $sql_total_final = 'SELECT SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate';        $result = dbQuery($sql_total_final);    while($row = dbFetchArray($result))         {       echo "<strong>All sales = <font color=#CC0000> Kshs " .$row['SUM(Cost)']; echo "</font></strong>" ;       }    }     echo "<br>";    echo '<h4><a href="index.php">Back</a> to sales panel.'; } ?>
    The first page is displayed well. When I click next, the page is blank and the URL is


    The URL for the search should be



    Followed by the pagination as a user clicks next or a value in the pagination link

    viewsales.php is the file containing sales functions

    It only works if I put each function in its own page. My sales functions are many (33 functions) and so they are all in one file called view sales.php in a case statement.

    How do I make it work if I have a file that contains all my sales functions and I want to use the pagination in each function?

    my pagination function is in a file I call at the top of every page (functions.php)
    Code:
    <? //Pagination functions  function getPagingQuery($sql, $itemPerPage = 10) {    if (isset($_GET['page']) && (int)$_GET['page'] > 0)    {       $page = (int)$_GET['page'];    }    else    {       $page = 1;    }        // start fetching from this row number    $offset = ($page - 1) * $itemPerPage;        return $sql . " LIMIT $offset, $itemPerPage"; }  /*    Get the links to navigate between one result page to another.     */   function getPagingLink($sql, $itemPerPage = 10, $strGet = '') {    $result        = dbQuery($sql);    $pagingLink    = '';    $totalResults  = dbNumRows($result);    $totalPages    = ceil($totalResults / $itemPerPage);        // how many link pages to show    $numLinks      = 10;            // create the paging links only if theres more than one page of results    if ($totalPages > 1) {           $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;               if (isset($_GET['page']) && (int)$_GET['page'] > 0) {          $pageNumber = (int)$_GET['page'];       } else {          $pageNumber = 1;       }              // print 'previous' link only if its not       // on page one       if ($pageNumber > 1) {          $page = $pageNumber - 1;          if ($page > 1) {             $prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";             ||             $prev = " <a href=\"$self?action=$action&$strGet/\">[Prev]</a> ";          } else {             $prev = " <a href=\"$self?$strGet\">[Prev]</a> ";          }                          $first = " <a href=\"$self?$strGet\">[First]</a> ";       } else {          $prev  = ''; // on page one, don't show 'previous' link          $first = ''; // nor 'first page' link       }           // print 'next' link only if its not       // on the last page       if ($pageNumber < $totalPages) {          $page = $pageNumber + 1;          $next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> "; || " <a href=\"$self?action=$action&$strGet/\">[Prev]</a> ";          $last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> "; || " <a href=\"$self?action=$action&$strGet/\">[Prev]</a> ";       } else {          $next = ''; // if on the last page, don't show 'next' link          $last = ''; // nor 'last page' link       }        $start = $pageNumber - ($pageNumber % $numLinks) + 1;       $end   = $start + $numLinks - 1;                    $end   = min($totalPages, $end);              $pagingLink = array();       for($page = $start; $page <= $end; $page++)   {          if ($page == $pageNumber) {             $pagingLink[] = " $page ";   // no need to create a link to current page          } else {             if ($page == 1) {                $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";             } else {                   $pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";             }             }           }              $pagingLink = implode(' | ', $pagingLink);              // return the page navigation link       $pagingLink = $first . $prev . $pagingLink . $next . $last;    }        return $pagingLink; } ?>
    Like in this page (list.php)

    Code:
    <?php if (!defined('WEB_ROOT')) {    exit; } $rowsPerPage = 5;  $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI']; checkUser();  $sql = "SELECT * FROM fragrancestock"; $result     = dbQuery(getPagingQuery($sql, $rowsPerPage)); $pagingLink = getPagingLink($sql, $rowsPerPage);  $errorMessage = (isset($_GET['cstock_error']) && $_GET['cstock_error'] != '') ? $_GET['cstock_error'] : '&nbsp;'; ?> <form action="index.php?view=add" method="post"  name="frmList" id="frmList">   <table width="100%" border="0" align="center">     <tr class="title_text">       <td width="100">Category</td>       <td width="400">Brand</td>       <td width="100" align="center">Stock</td>       <td width="100">Date Added</td>       <td width="50">Details</td>       <td width="50">Add</td>       <td width="50">Delete</td>     </tr>     <?php if (dbNumRows($result) > 0) {    $i = 0;        while($row = dbFetchAssoc($result)) {       extract($row);                 if ($i%2) {          $class = 'row1';       } else {          $class = 'row2';       }              $i += 1; ?>     <tr class="<?php echo $class; ?>">       <td width="100"><?php echo $Category; ?></td>       <td width="400"><?php echo $Brand; ?></td>       <td width="100" class="inner_border"><?php echo $Quantity; ?></td>       <td width="100"><?php echo $DateAddedStock; ?></td>       <td width="50"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?view=detail&StockID=<?php echo $StockID; ?>">View</a></td>       <td width="50"><a href="javascript:modifyStock(<?php echo $StockID; ?>);"><strong>Add         </strong> </a></td>       <td width="50"><a href="javascript:deleteStock(<?php echo $StockID; ?>);">Delete</a></td>     </tr>     <?php    } // end while   ?>     <tr>       <td colspan="7" align="center">         <?php     echo $pagingLink;    ?>       </td>     </tr>     <?php    } else { ?>     <tr>       <td colspan="7" align="center">No Stock Items Yet</td>     </tr>     <?php } ?>     <tr>       <td colspan="7">&nbsp;</td>     </tr>     <tr>       <td colspan="7" align="right"> <input name="btnAddStock" type="submit" value="New Entry" class="button_image">     </tr>   </table> </form>  <label class="title_text">View Stock:</label> <hr> <table width="100%" border="0">   <tr>     <td><form action="index.php?view=showbybrand" method="post" name="frmByBrand">         <table width="100%" border="0">           <tr>             <td width="200">By Brand:</td>             <td width="200"><select name="sltBrand">                 <option value="0">Select Brand</option>                 <?          $sql = "SELECT DISTINCT Brand FROM cstockitems ORDER BY Brand ASC";          $result = dbQuery($sql);                          if(dbNumRows($result))          {             while($row = dbFetchAssoc($result))             {             echo "<option>$row[Brand]</option>";             }          }                 else                {             echo "<option>No Brands Present</option>";                }      ?>               </select></td>             <td width="200"><input type="submit" name="Submit" value="Show" class="button_image" onClick="return CheckShowBrandByBrand();">             </td>           </tr>         </table>       </form></td>   </tr>   <tr>     <td><form name="frmByCategory" method="post" action="showbycategory.php">         <table width="100%" border="0">           <tr>             <td width="200">By Category:</td>             <td width="200"><select name="sltCategory">                 <option value="0">Select Category</option>                 <?          $sql = "SELECT DISTINCT Category FROM cstockitems ORDER BY Category ASC";          $result = dbQuery($sql);                          if(dbNumRows($result))          {             while($row = dbFetchAssoc($result))             {             echo "<option>$row[Category]</option>";             }          }                 else                {             echo "<option>No Categories Present</option>";                }      ?>               </select></td>             <td width="200"><input type="submit" name="Submit2" value="Show" class="button_image" onClick="return CheckShowBrandByCategory();"></td>           </tr>         </table>       </form></td>   </tr>   <tr>     <td><form name="frmSearchBrand" method="post" action="showbysearch.php">         <table width="100%" border="0">           <tr>             <td width="200">Search:</td>             <td width="200"><input type="text" name="txtSearchBrand" size="40"></td>             <td width="200"><input type="submit" name="Submit3" value="Search" class="button_image" onClick="return CheckShowBrandBySearch();"></td>           </tr>         </table>       </form></td>   </tr> </table> <label class="title_text">Print product list for re-stocking:</label> <hr> <table width="100%" border="0" cellspacing="0" cellpadding="0">   <tr>     <td><a href="exportoutofstockfile.php">Out of stock:</a></td>     <td><a href="exportlowstockfile.php">Low stock:</a></td>     <td><a href="exportwarningstockfile.php">Stock running low:</a></td>    <td><a href="printstocksheet.php">Print stock sheet</a></td>   </tr> </table>   <script> function CheckShowBrandByBrand() {    form = window.document.frmByBrand;        if (form.sltBrand.selectedIndex == 0)    {       alert('You have not selected a brand to view details!');       return false;       }    else    {    return true;    } }  function CheckShowBrandByCategory() {    form = window.document.frmByCategory;        if (form.sltCategory.selectedIndex == 0)    {       alert('You have not selected a Category to view details!');       return false;       }    else    {    return true;    } }  function CheckShowBrandBySearch() {    form = window.document.frmSearchBrand;        if (form.txtSearchBrand.value == "")    {       alert('You have not entered a brand name to search!');       return false;       }    else    {    return true;    } } </script>
    But you can see that page has one function so the pagination works well using the variable ($page)

    My problem is how to make it work in a page containing multiple functions AND it works in every function
    Where should I change without having to make a pagination function for every POST function

    My page of functions where I want to use it below:

    Code:
    <? require_once '../../functions.php';    $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI']; checkUser();  $action = isset($_GET['action']) ? $_GET['action'] : ''; //$page = $_GET['action'];  switch ($action) {    case 'jan' :       sales_January();    break;           case 'feb' :       sales_February();    break;        case 'mar' :       sales_March();    break;           case 'april' :       sales_April();    break;        case 'may' :       sales_May();    break;           case 'june' :       sales_June();    break;        case 'july' :       sales_July();    break;           case 'aug' :       sales_August();    break;        case 'sept' :       sales_September();    break;           case 'oct' :       sales_October();    break;        case 'nov' :       sales_November();    break;           case 'dec' :       sales_December();    break;           case 'allsalestoday' :       allsales_Today();    break;           case 'allsalessevendays' :       allsales_SevenDays();    break;        case 'salesbycategory' :       salesbyCategory();    break;        case 'salesbybrand' :       salesbyBrand();    break;     default : }  function allsales_Today($action) {    $rowsPerPage = 2;     //$sql = 'SELECT * FROM cstocksales WHERE SaleDate = current_date';    $sql = 'SELECT * FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate';        //$result = dbQuery($sql);    $result     = dbQuery(getPagingQuery($sql, $rowsPerPage));    $pagingLink = getPagingLink($sql, $rowsPerPage);        if (dbNumRows($result) == 0) //if none       {         echo '<p><font color=#CC0000><h4>No sales for today were found in the records...</h4></font></p>';       echo 'Go back <a href="index.php">Here</a> for another search.';    }    else    {       echo "<h5>Today's sales sales:<p></h5>";       echo '<table width="100%"  border="1" bordercolor="#000000">';      echo'<tr>';       echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';       echo'<th scope="col" bgcolor="#66FFFF">Category</th>';       echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';       echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';       echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';       echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';       echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';     echo'</tr>';      while($row = dbFetchArray($result))     // while($row = mysql_fetch_array($result))      {                 echo'<tr>';          echo'<td align="center">';          $SaleID = $row["SaleID"];                   echo $SaleID. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Category = $row["Category"];                   echo $Category. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Brand = $row["Brand"];                   echo $Brand. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Quantity = $row["Quantity"];                   echo $Quantity. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $PaidBy = $row["PaidBy"];                   echo $PaidBy. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Cost = $row["Cost"];                   echo $Cost. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $SaleDate = $row["SaleDate"];                   echo $SaleDate. "<br>";                   echo'</td align="center">';                   echo '</tr>';              }    echo'</table>';        //paginating link    echo $pagingLink;        echo "<br>"; echo "<strong>Sales figures:</strong>"; echo "<hr>";        $sql_total = 'SELECT PaidBy, SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate GROUP BY PaidBy';        $result = dbQuery($sql_total);             while($row = dbFetchArray($result))         {        echo "Total sales by ". $row['PaidBy']. "<font color=#CC0000> =  Kshs <strong>". $row['SUM(Cost)']; echo "</font></strong>";       echo "&nbsp;&nbsp;"; echo"|"; echo "&nbsp;&nbsp;";       }           $sql_total_final = 'SELECT SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate';        $result = dbQuery($sql_total_final);    while($row = dbFetchArray($result))         {       echo "<strong>All sales = <font color=#CC0000> Kshs " .$row['SUM(Cost)']; echo "</font></strong>" ;       }    }     echo "<br>";    echo '<h4><a href="index.php">Back</a> to sales panel.'; }  function allsales_SevenDays() {    $sql = 'SELECT * FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 8 DAY) <= SaleDate';       $result = dbQuery($sql);    ///$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));    //$pagingLink = getPagingLink($sql, $rowsPerPage);        if (dbNumRows($result) == 0) //if none       {         echo '<p><font color=#CC0000><h4>No sales for the past seven days were found in the records...</h4></font></p>';       echo 'Go back <a href="index.php">Here</a> for another search.';    }    else    {       echo '<h5>Sales in the past seven days:<p></h5>';       echo '<table width="100%"  border="1" bordercolor="#000000">';      echo'<tr>';       echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';       echo'<th scope="col" bgcolor="#66FFFF">Category</th>';       echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';       echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';       echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';       echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';       echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';     echo'</tr>';      while($row = dbFetchArray($result))      {        echo'<tr>';          echo'<td align="center">';          $SaleID = $row["SaleID"];                   echo $SaleID. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Category = $row["Category"];                   echo $Category. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Brand = $row["Brand"];                   echo $Brand. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Quantity = $row["Quantity"];                   echo $Quantity. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $PaidBy = $row["PaidBy"];                   echo $PaidBy. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Cost = $row["Cost"];                   echo $Cost. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $SaleDate = $row["SaleDate"];                   echo $SaleDate. "<br>";                   echo'</td align="center">';                   echo '</tr>';      }    echo'</table>';    //    echo 'Go back <a href="index.php">Here</a> for another search.';    } }   ///////////////////////////// function salesbyCategory() {    $byCategory = $_POST['sltCategory'];        $sql = 'SELECT * FROM cstocksales WHERE Category = "'.$byCategory.'"';        $result = dbQuery($sql);    ///$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));    //$pagingLink = getPagingLink($sql, $rowsPerPage);        if (dbNumRows($result) == 0) //if none       {         echo '<p><font color=#CC0000><h4>No sales for the Category "'.$_POST['sltCategory'].'" were found in the records...</h4></font></p>';       echo 'Go back <a href="index.php">Here</a> for another search.';    }    else    {       echo '<h5>"'.$_POST['sltCategory'].'" sales:<p></h5>';       echo '<table width="100%"  border="1" bordercolor="#000000">';      echo'<tr>';       echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';       echo'<th scope="col" bgcolor="#66FFFF">Category</th>';       echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';       echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';       echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';       echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';       echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';     echo'</tr>';      while($row = dbFetchArray($result))      {        echo'<tr>';          echo'<td align="center">';          $SaleID = $row["SaleID"];                   echo $SaleID. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Category = $row["Category"];                   echo $Category. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Brand = $row["Brand"];                   echo $Brand. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Quantity = $row["Quantity"];                   echo $Quantity. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $PaidBy = $row["PaidBy"];                   echo $PaidBy. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Cost = $row["Cost"];                   echo $Cost. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $SaleDate = $row["SaleDate"];                   echo $SaleDate. "<br>";                   echo'</td align="center">';                   echo '</tr>';      }    echo'</table>';    echo 'Go back <a href="index.php">Here</a> for another search.';    } }  function salesbyBrand() {    $byBrand = $_POST['sltBrand'];        $sql = 'SELECT * FROM cstocksales WHERE Brand = "'.$byBrand.'"';        $result = dbQuery($sql);    ///$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));    //$pagingLink = getPagingLink($sql, $rowsPerPage);        if (dbNumRows($result) == 0) //if none       {         echo '<p><font color=#CC0000><h4>No sales for the brand  "'.$_POST['sltBrand'].'" were found in the records...</h4></font></p>';       echo 'Go back <a href="index.php">Here</a> for another search.';    }    else    {       echo '<h5>"'.$_POST['sltBrand'].'" slaes:<p></h5>';       echo '<table width="100%"  border="1" bordercolor="#000000">';      echo'<tr>';       echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';       echo'<th scope="col" bgcolor="#66FFFF">Category</th>';       echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';       echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';       echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';       echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';       echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';     echo'</tr>';      while($row = dbFetchArray($result))      {        echo'<tr>';          echo'<td align="center">';          $SaleID = $row["SaleID"];                   echo $SaleID. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Category = $row["Category"];                   echo $Category. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Brand = $row["Brand"];                   echo $Brand. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Quantity = $row["Quantity"];                   echo $Quantity. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $PaidBy = $row["PaidBy"];                   echo $PaidBy. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $Cost = $row["Cost"];                   echo $Cost. "<br>";                   echo'</td align="center">';          echo'<td align="center">';          $SaleDate = $row["SaleDate"];                   echo $SaleDate. "<br>";                   echo'</td align="center">';                   echo '</tr>';      }    echo'</table>';        echo 'Go back <a href="index.php">Here</a> for another search.';    } } ?>


    I hope I provided clear information...
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I would forget about your immediate problem and redsign the whole thing.
    Are you a self taught programmer?.
    I ask because you have developed an unorthordox style.

    There is surely no need for one function per month.
    The correct way would be one function and pass the month as a parameter.

    A function should perform one function not be used as a dump for a mishmash of SQL PHP and HTML

    I may be misjudging because I cannot read your code comfortably..
    How did you manage to get it all on one line?

    Comment

    • gnawz
      New Member
      • Nov 2006
      • 64

      #3
      Yes,
      I'm a self taught programmer.

      I have a drop-down from which one selects to view month (passed as a parameter)

      I broke it down just so that one can understand what I mean by having many function but want the same pagination function to work in each.

      I copied the code and pasted it.

      When you look at my pagination code and my post, how would I use the pagination in a page containing a series of functions?

      Originally posted by code green
      I would forget about your immediate problem and redesign the whole thing.
      Are you a self taught programmer?.
      I ask because you have developed an unorthordox style.

      There is surely no need for one function per month.
      The correct way would be one function and pass the month as a parameter.

      A function should perform one function not be used as a dump for a mishmash of SQL PHP and HTML

      I may be misjudging because I cannot read your code comfortably..
      How did you manage to get it all on one line?

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I am sorry. I have tried but cannot read your code in its present format.

        Any chance of a moderator stepping in and advising gnawz on what he has done to make his code so unreadable.

        I am referring only to the layout within the forum.
        All on one line and a proliferation of spaces

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by code green
          I am sorry. I have tried but cannot read your code in its present format.

          Any chance of a moderator stepping in and advising gnawz on what he has done to make his code so unreadable.

          I am referring only to the layout within the forum.
          All on one line and a proliferation of spaces
          I attempted to format the code, but there's a hell of a lot and I don't have the time.

          Gnawz, when you post code in the forums, please format it with indents and new lines so we here can actually read it.

          ps: maybe format the code you gave previously and post it again so people can look at it better.

          Comment

          • gnawz
            New Member
            • Nov 2006
            • 64

            #6
            Help on pagination please

            Dear all,

            I have re-posted this thread with my code outside code tags as they are difficult to edit and mess all the code

            I have a pagination function in function.php

            Code:
            <?
            //Pagination functions
            
            function getPagingQuery($sql, $itemPerPage = 10)
            {
               if (isset($_GET['page']) && (int)$_GET['page'] > 0)
               {
                  $page = (int)$_GET['page'];
               }
               else
               {
                  $page = 1;
               }
               
               // start fetching from this row number
               $offset = ($page - 1) * $itemPerPage;
               
               return $sql . " LIMIT $offset, $itemPerPage";
            }
            
            /*
               Get the links to navigate between one result page to another.
               
            */
            
            
            function getPagingLink($sql, $itemPerPage = 10, $strGet = '')
            {
               $result        = dbQuery($sql);
               $pagingLink    = '';
               $totalResults  = dbNumRows($result);
               $totalPages    = ceil($totalResults / $itemPerPage);
               
               // how many link pages to show
               $numLinks      = 10;
            
                  
               // create the paging links only if theres more than one page of results
               if ($totalPages > 1) {
               
                  $self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
                  
            
                  if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
                     $pageNumber = (int)$_GET['page'];
                  } else {
                     $pageNumber = 1;
                  }
                  
                  // print 'previous' link only if its not
                  // on page one
                  if ($pageNumber > 1) {
                     $page = $pageNumber - 1;
                     if ($page > 1) {
                        $prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";
                        ||
                        $prev = " <a href=\"$self?action=$action&$strGet/\">[Prev]</a> ";
                     } else {
                        $prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
                     }   
                        
                     $first = " <a href=\"$self?$strGet\">[First]</a> ";
                  } else {
                     $prev  = ''; // on page one, don't show 'previous' link
                     $first = ''; // nor 'first page' link
                  }
               
                  // print 'next' link only if its not
                  // on the last page
                  if ($pageNumber < $totalPages) {
                     $page = $pageNumber + 1;
                     $next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> "; || " <a href=\"$self?action=$action&$strGet/\">[Prev]</a> ";
                     $last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> "; || " <a href=\"$self?action=$action&$strGet/\">[Prev]</a> ";
                  } else {
                     $next = ''; // if on the last page, don't show 'next' link
                     $last = ''; // nor 'last page' link
                  }
            
                  $start = $pageNumber - ($pageNumber % $numLinks) + 1;
                  $end   = $start + $numLinks - 1;      
                  
                  $end   = min($totalPages, $end);
                  
                  $pagingLink = array();
                  for($page = $start; $page <= $end; $page++)   {
                     if ($page == $pageNumber) {
                        $pagingLink[] = " $page ";   // no need to create a link to current page
                     } else {
                        if ($page == 1) {
                           $pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
                        } else {   
                           $pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";
                        }   
                     }
               
                  }
                  
                  $pagingLink = implode(' | ', $pagingLink);
                  
                  // return the page navigation link
                  $pagingLink = $first . $prev . $pagingLink . $next . $last;
               }
               
               return $pagingLink;
            }
            ?>
            
            I have used it in  this page (list.php)
            
            <?php
            if (!defined('WEB_ROOT')) {
               exit;
            }
            $rowsPerPage = 5;
            
            $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
            checkUser();
            
            $sql = "SELECT * FROM fragrancestock";
            $result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
            $pagingLink = getPagingLink($sql, $rowsPerPage);
            
            $errorMessage = (isset($_GET['cstock_error']) && $_GET['cstock_error'] != '') ? $_GET['cstock_error'] : '&nbsp;';
            ?>
            <form action="index.php?view=add" method="post"  name="frmList" id="frmList">
              <table width="100%" border="0" align="center">
                <tr class="title_text">
                  <td width="100">Category</td>
                  <td width="400">Brand</td>
                  <td width="100" align="center">Stock</td>
                  <td width="100">Date Added</td>
                  <td width="50">Details</td>
                  <td width="50">Add</td>
                  <td width="50">Delete</td>
                </tr>
                <?php
            if (dbNumRows($result) > 0) {
               $i = 0;
               
               while($row = dbFetchAssoc($result)) {
                  extract($row);
                     
                  if ($i%2) {
                     $class = 'row1';
                  } else {
                     $class = 'row2';
                  }
                  
                  $i += 1;
            ?>
                <tr class="<?php echo $class; ?>">
                  <td width="100"><?php echo $Category; ?></td>
                  <td width="400"><?php echo $Brand; ?></td>
                  <td width="100" class="inner_border"><?php echo $Quantity; ?></td>
                  <td width="100"><?php echo $DateAddedStock; ?></td>
                  <td width="50"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?view=detail&StockID=<?php echo $StockID; ?>">View</a></td>
                  <td width="50"><a href="javascript:modifyStock(<?php echo $StockID; ?>);"><strong>Add
                    </strong> </a></td>
                  <td width="50"><a href="javascript:deleteStock(<?php echo $StockID; ?>);">Delete</a></td>
                </tr>
                <?php
               } // end while
            
            
            ?>
                <tr>
                  <td colspan="7" align="center">
                    <?php 
               echo $pagingLink;
               ?>
                  </td>
                </tr>
                <?php   
            } else {
            ?>
                <tr>
                  <td colspan="7" align="center">No Stock Items Yet</td>
                </tr>
                <?php
            }
            ?>
                <tr>
                  <td colspan="7">&nbsp;</td>
                </tr>
                <tr>
                  <td colspan="7" align="right"> <input name="btnAddStock" type="submit" value="New Entry" class="button_image">
                </tr>
              </table>
            </form>
            
            <label class="title_text">View Stock:</label>
            <hr>
            <table width="100%" border="0">
              <tr>
                <td><form action="index.php?view=showbybrand" method="post" name="frmByBrand">
                    <table width="100%" border="0">
                      <tr>
                        <td width="200">By Brand:</td>
                        <td width="200"><select name="sltBrand">
                            <option value="0">Select Brand</option>
                            <?
                     $sql = "SELECT DISTINCT Brand FROM cstockitems ORDER BY Brand ASC";
                     $result = dbQuery($sql);      
                     
                     if(dbNumRows($result))
                     {
                        while($row = dbFetchAssoc($result))
                        {
                        echo "<option>$row[Brand]</option>";
                        }
                     }
                            else
                           {
                        echo "<option>No Brands Present</option>";
                           }
                 ?>
                          </select></td>
                        <td width="200"><input type="submit" name="Submit" value="Show" class="button_image" onClick="return CheckShowBrandByBrand();">
                        </td>
                      </tr>
                    </table>
                  </form></td>
              </tr>
              <tr>
                <td><form name="frmByCategory" method="post" action="showbycategory.php">
                    <table width="100%" border="0">
                      <tr>
                        <td width="200">By Category:</td>
                        <td width="200"><select name="sltCategory">
                            <option value="0">Select Category</option>
                            <?
                     $sql = "SELECT DISTINCT Category FROM cstockitems ORDER BY Category ASC";
                     $result = dbQuery($sql);      
                     
                     if(dbNumRows($result))
                     {
                        while($row = dbFetchAssoc($result))
                        {
                        echo "<option>$row[Category]</option>";
                        }
                     }
                            else
                           {
                        echo "<option>No Categories Present</option>";
                           }
                 ?>
                          </select></td>
                        <td width="200"><input type="submit" name="Submit2" value="Show" class="button_image" onClick="return CheckShowBrandByCategory();"></td>
                      </tr>
                    </table>
                  </form></td>
              </tr>
              <tr>
                <td><form name="frmSearchBrand" method="post" action="showbysearch.php">
                    <table width="100%" border="0">
                      <tr>
                        <td width="200">Search:</td>
                        <td width="200"><input type="text" name="txtSearchBrand" size="40"></td>
                        <td width="200"><input type="submit" name="Submit3" value="Search" class="button_image" onClick="return CheckShowBrandBySearch();"></td>
                      </tr>
                    </table>
                  </form></td>
              </tr>
            </table>
            <label class="title_text">Print product list for re-stocking:</label>
            <hr>
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><a href="exportoutofstockfile.php">Out of stock:</a></td>
                <td><a href="exportlowstockfile.php">Low stock:</a></td>
                <td><a href="exportwarningstockfile.php">Stock running low:</a></td>
               <td><a href="printstocksheet.php">Print stock sheet</a></td>
              </tr>
            </table>
            
            
            <script>
            function CheckShowBrandByBrand()
            {
               form = window.document.frmByBrand;
               
               if (form.sltBrand.selectedIndex == 0)
               {
                  alert('You have not selected a brand to view details!');
                  return false;   
               }
               else
               {
               return true;
               }
            }
            
            function CheckShowBrandByCategory()
            {
               form = window.document.frmByCategory;
               
               if (form.sltCategory.selectedIndex == 0)
               {
                  alert('You have not selected a Category to view details!');
                  return false;   
               }
               else
               {
               return true;
               }
            }
            
            function CheckShowBrandBySearch()
            {
               form = window.document.frmSearchBrand;
               
               if (form.txtSearchBrand.value == "")
               {
                  alert('You have not entered a brand name to search!');
                  return false;   
               }
               else
               {
               return true;
               }
            }
            </script>
            <?
            ?>
            and it has worked well.

            However, I need help on how to use it in a page with multiple functions as below

            Code:
            <?
            require_once '../../functions.php';
            
            
            $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
            checkUser();
            
            $action = isset($_GET['action']) ? $_GET['action'] : '';
            //$page = $_GET['action'];
            
            switch ($action)
            {
                  case 'allsalestoday' :
                  allsales_Today();
               break;
               
                  case 'allsalessevendays' :
                  allsales_SevenDays();
               break;
               
               case 'salesbycategory' :
                  salesbyCategory();
               break;
               
               case 'salesbybrand' :
                  salesbyBrand();
               break;
               
            default :
            }
            
            $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
            checkUser();
            
            $action = isset($_GET['action']) ? $_GET['action'] : '';
            //$page = $_GET['action'];
            
            switch ($action)
            {
             
               
            default :
            }
            
            function allsales_Today($action)
            {
               $rowsPerPage = 2;
            
               //$sql = 'SELECT * FROM cstocksales WHERE SaleDate = current_date';
               $sql = 'SELECT * FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate';
               
               //$result = dbQuery($sql);
               $result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
               $pagingLink = getPagingLink($sql, $rowsPerPage);
               
               if (dbNumRows($result) == 0) //if none   
               {
                    echo '<p><font color=#CC0000><h4>No sales for today were found in the records...</h4></font></p>';
                  echo 'Go back <a href="index.php">Here</a> for another search.';
               }
               else
               {
                  echo "<h5>Today's sales sales:<p></h5>";
                  echo '<table width="100%"  border="1" bordercolor="#000000">';
                 echo'<tr>';
                  echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Category</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';
                echo'</tr>';
                 while($row = dbFetchArray($result))
                // while($row = mysql_fetch_array($result))
                 {
                    
                   echo'<tr>';
                     echo'<td align="center">';
                     $SaleID = $row["SaleID"];
                              echo $SaleID. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Category = $row["Category"];
                              echo $Category. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Brand = $row["Brand"];
                              echo $Brand. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Quantity = $row["Quantity"];
                              echo $Quantity. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $PaidBy = $row["PaidBy"];
                              echo $PaidBy. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Cost = $row["Cost"];
                              echo $Cost. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $SaleDate = $row["SaleDate"];
                              echo $SaleDate. "<br>";
                              echo'</td align="center">';
                     
                    echo '</tr>';
                   
                 }
               echo'</table>';
               
               //paginating link
               echo $pagingLink;
               
               echo "<br>"; echo "<strong>Sales figures:</strong>"; echo "<hr>";
               
               $sql_total = 'SELECT PaidBy, SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate GROUP BY PaidBy';
               
               $result = dbQuery($sql_total);
               
            
               
               while($row = dbFetchArray($result))
                    {
                   echo "Total sales by ". $row['PaidBy']. "<font color=#CC0000> =  Kshs <strong>". $row['SUM(Cost)']; echo "</font></strong>";
                  echo "&nbsp;&nbsp;"; echo"|"; echo "&nbsp;&nbsp;";
                  }
                  
               $sql_total_final = 'SELECT SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 1 DAY) <= SaleDate';
               
               $result = dbQuery($sql_total_final);
               while($row = dbFetchArray($result))
                    {
                  echo "<strong>All sales = <font color=#CC0000> Kshs " .$row['SUM(Cost)']; echo "</font></strong>" ;
                  }
               }
            
               echo "<br>";
               echo '<h4><a href="index.php">Back</a> to sales panel.';
            }
            
            function allsales_SevenDays()
            {
               $sql = 'SELECT * FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL 8 DAY) <= SaleDate';   
               $result = dbQuery($sql);
               ///$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
               //$pagingLink = getPagingLink($sql, $rowsPerPage);
               
               if (dbNumRows($result) == 0) //if none   
               {
                    echo '<p><font color=#CC0000><h4>No sales for the past seven days were found in the records...</h4></font></p>';
                  echo 'Go back <a href="index.php">Here</a> for another search.';
               }
               else
               {
                  echo '<h5>Sales in the past seven days:<p></h5>';
                  echo '<table width="100%"  border="1" bordercolor="#000000">';
                 echo'<tr>';
                  echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Category</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';
                echo'</tr>';
                 while($row = dbFetchArray($result))
                 {
                   echo'<tr>';
                     echo'<td align="center">';
                     $SaleID = $row["SaleID"];
                              echo $SaleID. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Category = $row["Category"];
                              echo $Category. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Brand = $row["Brand"];
                              echo $Brand. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Quantity = $row["Quantity"];
                              echo $Quantity. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $PaidBy = $row["PaidBy"];
                              echo $PaidBy. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Cost = $row["Cost"];
                              echo $Cost. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $SaleDate = $row["SaleDate"];
                              echo $SaleDate. "<br>";
                              echo'</td align="center">';
                     
                    echo '</tr>';
                 }
               echo'</table>';
               //
               echo 'Go back <a href="index.php">Here</a> for another search.';
               }
            }
            
            
            /////////////////////////////
            function salesbyCategory()
            {
               $byCategory = $_POST['sltCategory'];
               
               $sql = 'SELECT * FROM cstocksales WHERE Category = "'.$byCategory.'"';
               
               $result = dbQuery($sql);
               ///$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
               //$pagingLink = getPagingLink($sql, $rowsPerPage);
               
               if (dbNumRows($result) == 0) //if none   
               {
                    echo '<p><font color=#CC0000><h4>No sales for the Category "'.$_POST['sltCategory'].'" were found in the records...</h4></font></p>';
                  echo 'Go back <a href="index.php">Here</a> for another search.';
               }
               else
               {
                  echo '<h5>"'.$_POST['sltCategory'].'" sales:<p></h5>';
                  echo '<table width="100%"  border="1" bordercolor="#000000">';
                 echo'<tr>';
                  echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Category</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';
                echo'</tr>';
                 while($row = dbFetchArray($result))
                 {
                   echo'<tr>';
                     echo'<td align="center">';
                     $SaleID = $row["SaleID"];
                              echo $SaleID. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Category = $row["Category"];
                              echo $Category. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Brand = $row["Brand"];
                              echo $Brand. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Quantity = $row["Quantity"];
                              echo $Quantity. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $PaidBy = $row["PaidBy"];
                              echo $PaidBy. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Cost = $row["Cost"];
                              echo $Cost. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $SaleDate = $row["SaleDate"];
                              echo $SaleDate. "<br>";
                              echo'</td align="center">';
                     
                    echo '</tr>';
                 }
               echo'</table>';
               echo 'Go back <a href="index.php">Here</a> for another search.';
               }
            }
            
            function salesbyBrand()
            {
               $byBrand = $_POST['sltBrand'];
               
               $sql = 'SELECT * FROM cstocksales WHERE Brand = "'.$byBrand.'"';
               
               $result = dbQuery($sql);
               ///$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
               //$pagingLink = getPagingLink($sql, $rowsPerPage);
               
               if (dbNumRows($result) == 0) //if none   
               {
                    echo '<p><font color=#CC0000><h4>No sales for the brand  "'.$_POST['sltBrand'].'" were found in the records...</h4></font></p>';
                  echo 'Go back <a href="index.php">Here</a> for another search.';
               }
               else
               {
                  echo '<h5>"'.$_POST['sltBrand'].'" slaes:<p></h5>';
                  echo '<table width="100%"  border="1" bordercolor="#000000">';
                 echo'<tr>';
                  echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Category</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';
                  echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';
                echo'</tr>';
                 while($row = dbFetchArray($result))
                 {
                   echo'<tr>';
                     echo'<td align="center">';
                     $SaleID = $row["SaleID"];
                              echo $SaleID. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Category = $row["Category"];
                              echo $Category. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Brand = $row["Brand"];
                              echo $Brand. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Quantity = $row["Quantity"];
                              echo $Quantity. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $PaidBy = $row["PaidBy"];
                              echo $PaidBy. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $Cost = $row["Cost"];
                              echo $Cost. "<br>";
                              echo'</td align="center">';
                     echo'<td align="center">';
                     $SaleDate = $row["SaleDate"];
                              echo $SaleDate. "<br>";
                              echo'</td align="center">';
                     
                    echo '</tr>';
                 }
               echo'</table>';
               
               echo 'Go back <a href="index.php">Here</a> for another search.';
               }
            }
            ?>
            When I use it in functions that are contained in one page, I shows the first page but when I click next, it goes blank as it only recognises the page, not the function or action

            Eg it shows this for page 2 which is blank....



            yet I would like it to show



            and so on... ie It should maintain the search action (function) in subsequent paginations.

            Some body help

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              I won't give you a formal warning for not using code tags, as I believe you knowingly did it with the best intentions. But you must use code tags.

              Comment

              • ak1dnar
                Recognized Expert Top Contributor
                • Jan 2007
                • 1584

                #8
                Merged with duplicate thread (thread843785.h tml). Thanks

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  This is an awful lot of code, and that is in both sense of the word awful.Neverthel ess I cannot see where you have written the 'action' value in the URL.
                  that you are trying to read. [PHP]$action = isset($_GET['action']) ? $_GET['action'] : '';[/PHP]
                  But the code meanders and repeats itself so much it is too tiring to decipher.
                  I still stand by my first suggestion

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Hey, y'all. Please keep the unnecessary criticisms to a minimum and focus on solving the OP's problem.

                    We've all been beginners at one point or another; it's a stage everyone goes through, and code improves with experience.

                    gnawz, thanks for re-posting your code. I'll have a look at it shortly.

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Try passing 'action=allsale stoday' (etc.) as the third parameter to getPagingLink() .

                      It looks like anything you put in the third parameter ($strGet) gets dumped right into the paging link.

                      Comment

                      • gnawz
                        New Member
                        • Nov 2006
                        • 64

                        #12
                        Thanks, Sir

                        I always get criticism here. We can not be perfect all at once.

                        I will try that and get back to you.
                        But remember, I do not want to create another pagination function-I want to use the same (single function) in all pages. I hope I can achieve that in this case.
                        I have also improved my code.
                        I will post again.


                        Originally posted by pbmods
                        Try passing 'action=allsale stoday' (etc.) as the third parameter to getPagingLink() .

                        It looks like anything you put in the third parameter ($strGet) gets dumped right into the paging link.

                        Comment

                        • gnawz
                          New Member
                          • Nov 2006
                          • 64

                          #13
                          The following is my pagination function which I use in pages with one function and it works well.
                          Code:
                          <?
                          function getPagingQuery($sql, $itemPerPage = 10)
                          {
                          	if (isset($_GET['page']) && (int)$_GET['page'] > 0) 
                          	{
                          		$page = (int)$_GET['page'];
                          	} 
                          	else 
                          	{
                          		$page = 1;
                          	}
                          	
                          	// start fetching from this row number
                          	$offset = ($page - 1) * $itemPerPage;
                          	
                          	return $sql . " LIMIT $offset, $itemPerPage";
                          }
                          
                          /*
                          	Get the links to navigate between one result page to another.
                          	
                          */
                          
                          
                          function getPagingLink($sql, $itemPerPage = 10, $strGet = '')
                          {
                          	$result        = dbQuery($sql);
                          	$pagingLink    = '';
                          	$totalResults  = dbNumRows($result);
                          	$totalPages    = ceil($totalResults / $itemPerPage);
                          	
                          	// how many link pages to show
                          	$numLinks      = 10;
                          
                          		
                          	// create the paging links only if theres more than one page of results
                          	if ($totalPages > 1) {
                          	
                          		$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
                          		
                          
                          		if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
                          			$pageNumber = (int)$_GET['page'];
                          		} else {
                          			$pageNumber = 1;
                          		}
                          		
                          		// print 'previous' link only if its not
                          		// on page one
                          		if ($pageNumber > 1) {
                          			$page = $pageNumber - 1;
                          			if ($page > 1) {
                          				$prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";
                          			} else {
                          				$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
                          			}	
                          				
                          			$first = " <a href=\"$self?$strGet\">[First]</a> ";
                          		} else {
                          			$prev  = ''; // on page one, don't show 'previous' link
                          			$first = ''; // nor 'first page' link
                          		}
                          	
                          		// print 'next' link only if its not
                          		// on the last page
                          		if ($pageNumber < $totalPages) {
                          			$page = $pageNumber + 1;
                          			$next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> ";
                          			$last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> ";
                          		} else {
                          			$next = ''; // if on the last page, don't show 'next' link
                          			$last = ''; // nor 'last page' link
                          		}
                          
                          		$start = $pageNumber - ($pageNumber % $numLinks) + 1;
                          		$end   = $start + $numLinks - 1;		
                          		
                          		$end   = min($totalPages, $end);
                          		
                          		$pagingLink = array();
                          		for($page = $start; $page <= $end; $page++)	{
                          			if ($page == $pageNumber) {
                          				$pagingLink[] = " $page ";   // no need to create a link to current page
                          			} else {
                          				if ($page == 1) {
                          					$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
                          				} else {	
                          					$pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";
                          				}	
                          			}
                          	
                          		}
                          		
                          		$pagingLink = implode(' | ', $pagingLink);
                          		
                          		// return the page navigation link
                          		$pagingLink = $first . $prev . $pagingLink . $next . $last;
                          	}
                          	
                          	return $pagingLink;
                          }
                          I want to use it in each and every function of the following page.
                          Code:
                          <?
                          require_once '../../functions.php';
                          
                          
                          
                          $_SESSION['login_return_url'] = $_SERVER['REQUEST_URI'];
                          checkUser();
                          
                          $action = isset($_GET['action']) ? $_GET['action'] : '';
                          
                          switch ($action) 
                          {
                          	case 'month' :
                          		sales_Month();
                          	break;
                          	
                          		case 'days' :
                          		allsales_Day();
                          	
                          	case 'salesbybrand' :
                          		salesbyBrand();
                          	break;
                          	
                          default :
                          }
                          
                          function allsales_Day()
                          {
                          
                          	$day = '';
                          	if (isset($_POST['sltallSales'])) $day = $_POST['sltallSales'];
                          	elseif (isset($_GET['search'])) $day = $_GET['search'];
                          	
                          	$sql = "SELECT * FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL '$day' DAY) <= SaleDate";
                          	
                          	$result = dbQuery($sql, "search=$day");
                          	
                          	if (dbNumRows($result) == 0) //if none	
                          	{
                            		echo '<p><font color=#CC0000><h4>No sales were found in the records...</h4></font></p>';
                          		echo '<h4><a href="index.php">Back</a> to sales panel.';
                          	}
                          	else
                          	{
                          		//echo "<h5>Today's sales:<p></h5>";
                          		echo '<table width="100%"  border="1" bordercolor="#000000">';
                          	  	echo'<tr>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Category</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';
                          	 	echo'</tr>';
                          	  while($row = dbFetchArray($result))
                          	 // while($row = mysql_fetch_array($result))
                          	  {
                          	  	
                          		 echo'<tr>';
                          			echo'<td align="center">';
                          			$SaleID = $row["SaleID"];
                          						echo $SaleID. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Category = $row["Category"];
                          						echo $Category. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Brand = $row["Brand"];
                          						echo $Brand. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Quantity = $row["Quantity"];
                          						echo $Quantity. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$PaidBy = $row["PaidBy"];
                          						echo $PaidBy. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Cost = $row["Cost"];
                          						echo $Cost. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$SaleDate = $row["SaleDate"];
                          						echo $SaleDate. "<br>";
                          						echo'</td align="center">';
                          			
                          		  echo '</tr>';
                          		  
                          	  }
                          	echo'</table>';
                          	
                          	echo "<br>"; echo "<strong>Sales figures:</strong>"; echo "<hr>";
                          	
                          	$sql_total = "SELECT PaidBy, SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL '$day' DAY) <= SaleDate GROUP BY PaidBy";
                          	
                          	$result = dbQuery($sql_total);
                          	
                          
                          	
                          	while($row = dbFetchArray($result))
                          	  	{
                          	 	echo "Total sales by ". $row['PaidBy']. "<font color=#CC0000> =  Kshs <strong>". $row['SUM(Cost)']; echo "</font></strong>";
                          		echo "&nbsp;&nbsp;"; echo"|"; echo "&nbsp;&nbsp;";
                          		}
                          		
                          	$sql_total_final = "SELECT SUM(Cost) FROM cstocksales WHERE DATE_SUB(CURDATE(),INTERVAL '$day' DAY) <= SaleDate";
                          	
                          	$result = dbQuery($sql_total_final);
                          	while($row = dbFetchArray($result))
                          	  	{
                          		echo "<strong>All sales = <font color=#CC0000> Kshs " .$row['SUM(Cost)']; echo "</font></strong>" ;
                          		}	
                          
                          	echo "<br>"; 
                          	echo '<h4><a href="index.php">Back</a> to sales panel.';
                          	}
                          }
                          
                          function salesbyBrand()
                          {
                          	$byBrand = '';
                          	
                          	if (isset($_POST['sltBrand'])) $byBrand = $_POST['sltBrand'];
                          	elseif (isset($_GET['search'])) $byBrand = $_GET['search'];
                          	
                          	$sql = "SELECT * FROM cstocksales WHERE Brand = '$byBrand'";
                          	
                          	$result = dbQuery($sql, "search=$byBrand");
                          	
                          	if (dbNumRows($result) == 0) //if none	
                          	{
                            		echo "<p><font color=#CC0000><h4>No sales for '$byBrand' were found in the records...</h4></font></p>";
                          		echo "<h4><a href='index.php'>Back</a> to sales panel.";
                          	}
                          	else
                          	{
                          		echo '<h5>"'.$_POST['sltBrand'].'" slaes:<p></h5>';
                          		echo '<table width="100%"  border="1" bordercolor="#000000">';
                          	  	echo'<tr>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Category</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';
                          		echo'</tr>';
                          		
                          	  	while($row = dbFetchArray($result))
                          	  	{
                          		 	echo'<tr>';
                          			echo'<td align="center">';
                          			$SaleID = $row["SaleID"];
                          						echo $SaleID. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Category = $row["Category"];
                          						echo $Category. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Brand = $row["Brand"];
                          						echo $Brand. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Quantity = $row["Quantity"];
                          						echo $Quantity. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$PaidBy = $row["PaidBy"];
                          						echo $PaidBy. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Cost = $row["Cost"];
                          						echo $Cost. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$SaleDate = $row["SaleDate"];
                          						echo $SaleDate. "<br>";
                          						echo'</td align="center">';
                          			
                          		  echo '</tr>';
                          	  	}
                          			echo'</table>';
                          	
                          			echo "<br>"; echo "<strong>Sales figures:</strong>"; echo "<hr>";
                          	
                          			$sql_total = "SELECT PaidBy, SUM(Cost) FROM cstocksales WHERE Brand = '$byBrand' GROUP BY PaidBy";
                          			
                          			$result = dbQuery($sql_total);
                          	
                          			while($row = dbFetchArray($result))
                          	  		{
                          			echo "Total sales by ". $row['PaidBy']. "<font color=#CC0000> =  Kshs <strong>". $row['SUM(Cost)']; echo "</font></strong>"	;
                          			echo "&nbsp;&nbsp;"; echo"|"; echo "&nbsp;&nbsp;";
                          			}
                          		
                          			$sql_total_final = "SELECT SUM(Cost) FROM cstocksales WHERE Brand = '$byBrand'";
                          	
                          			$result = dbQuery($sql_total_final);
                          			while($row = dbFetchArray($result))
                          	  		{
                          			echo "<strong>All sales = <font color=#CC0000> Kshs " .$row['SUM(Cost)']; echo "</font></strong>" ;
                          			}
                          
                          			echo "<br>"; 
                          			echo '<h4><a href="index.php">Back</a> to sales panel.';
                          	}
                          }
                          
                          
                          function sales_Month()
                          {
                          
                          	$month = $_POST['MonthSales'];
                          	
                          	$sql = "SELECT * FROM cstocksales WHERE MONTH(SaleDate) = '$month' AND YEAR(SaleDate) = YEAR(CURDATE())";
                          	
                          	$result = dbQuery($sql);
                          	///$result     = dbQuery(getPagingQuery($sql, $rowsPerPage));
                          	//$pagingLink = getPagingLink($sql, $rowsPerPage);
                          	
                          	if (dbNumRows($result) == 0) //if none	
                          	{
                            		echo '<p><font color=#CC0000><h4>No sales were found in the records...</h4></font></p>';
                          		echo '<h4><a href="index.php">Back</a> to sales panel.';
                          	}
                          	else
                          	{
                          		echo '<h5>January sales:<p></h5>';
                          		echo '<table width="100%"  border="1" bordercolor="#000000">';
                          	  	echo'<tr>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Sale No</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Category</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Brand</th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Quantity </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Paid By: </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Amount </th>';
                          		echo'<th scope="col" bgcolor="#66FFFF">Date of Sale </th>';
                          	 echo'</tr>';
                          	  while($row = dbFetchArray($result))
                          	  {
                          		 echo'<tr>';
                          			echo'<td align="center">';
                          			$SaleID = $row["SaleID"];
                          						echo $SaleID. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Category = $row["Category"];
                          						echo $Category. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Brand = $row["Brand"];
                          						echo $Brand. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Quantity = $row["Quantity"];
                          						echo $Quantity. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$PaidBy = $row["PaidBy"];
                          						echo $PaidBy. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$Cost = $row["Cost"];
                          						echo $Cost. "<br>";
                          						echo'</td align="center">';
                          			echo'<td align="center">';
                          			$SaleDate = $row["SaleDate"];
                          						echo $SaleDate. "<br>";
                          						echo'</td align="center">';
                          			
                          		  echo '</tr>';
                          	  }
                          	echo'</table>';
                          	
                          	echo "<br>"; echo "<strong>Sales figures:</strong>"; echo "<hr>";
                          	
                          	$sql_total = "SELECT PaidBy, SUM(Cost) FROM cstocksales WHERE MONTH(SaleDate) = '$month' 
                          					AND YEAR(SaleDate) = YEAR(CURDATE()) GROUP BY PaidBy";
                          	
                          	$result = dbQuery($sql_total);
                          	
                          
                          	
                          	while($row = dbFetchArray($result))
                          	  	{
                          	 	echo "Total sales by ". $row['PaidBy']. "<font color=#CC0000> =  Kshs <strong>". $row['SUM(Cost)']; echo "</font></strong>";
                          		echo "&nbsp;&nbsp;"; echo"|"; echo "&nbsp;&nbsp;";
                          		}
                          		
                          	$sql_total_final = "SELECT SUM(Cost) FROM cstocksales WHERE MONTH(SaleDate) = '$month' 
                          						AND YEAR(SaleDate) = YEAR(CURDATE())";
                          	
                          	$result = dbQuery($sql_total_final);
                          	while($row = dbFetchArray($result))
                          	  	{
                          		echo "<strong>All sales = <font color=#CC0000> Kshs " .$row['SUM(Cost)']; echo "</font></strong>" ;
                          		}
                          	
                          	echo "<br>"; 
                          	echo '<h4><a href="index.php">Back</a> to sales panel.';
                          	}
                          }
                          
                          /* End of month sales*/
                          
                          ?>
                          Please help on how to go about this

                          Comment

                          • pbmods
                            Recognized Expert Expert
                            • Apr 2007
                            • 5821

                            #14
                            Wherever you call getPagingLink() , set the third parameter to whatever value you want to set the action to:

                            [code=php]
                            $pagingLink = getPagingLink($ sql, $rowsPerPage, 'action=allsale stoday');
                            [/code]

                            You'll need to specify a different action each time you call it, but this will add the action parameter to your paging links.

                            Comment

                            • gnawz
                              New Member
                              • Nov 2006
                              • 64

                              #15
                              Thanks for your help.

                              It added the action to the paging link when I look at the URL but I still get a blank page.

                              What could be the cause?

                              Comment

                              Working...