login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Navigatie systemen > sql_navigation()

sql_navigation()

Auteur: CelestialCelebi - 01 september 2004 - 19:19 - Gekeurd door: Dennisvb - Hits: 4830 - Aantal punten: 2.92 (6 stemmen)





Uhm, check uitleg? ^_^

Code:
  1. <?php
  2. /*
  3. ***************************************************************************
  4. * Function sql_navigation(), used to create a link-index of page numbers *
  5. * for paging through the records which come from your MySQL-database. *
  6. * Copyright (C) 2004 The Celestial Celebi. *
  7. * This program is free software; you can redistribute it and/or modify it *
  8. * under the terms of the GNU General Public License as published by the *
  9. * Free Software Foundation; either version 2 of the License, or (at your *
  10. * option) any later version. *
  11. * This program is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  14. * General Public License for more details. *
  15. * You should have received a copy of the GNU General Public License along *
  16. * with this program; if not, write to the Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************
  19. */
  20.  
  21. /*
  22. ***************************************************************************
  23. * @function: sql_navigation(): Returns an array for creating a *
  24. * pagination. *
  25. * @param $rDatabaseconnection (resource): Your database connection, we *
  26. * need it to get the total number of records from the database. This *
  27. * parameter must be given by reference. *
  28. * @param $aTable (string / array): The table where we need to take out *
  29. * the records for the navigation, can be an array of two elements: 1st *
  30. * is the table and the 2nd is a piece of query after the FROM. *
  31. * @param $sUrl (string): The name of the file in your URL, plus (if there *
  32. * is any) the query string, needed for making correct navigation links. *
  33. * @param $sAlias (string): The alias you want to give the navigation you *
  34. * are making now, it becomes part of the URL: index.php?MYALIAS=40. *
  35. * @param $iPerpage (int): The number of records you want to display per *
  36. * page. *
  37. * @param $iType (int): The type of navigation, you can use either the *
  38. * constants or integer numbers, see constants for more information. *
  39. * @return array: All information you need to display your navigation. *
  40. *-------------------------------------------------------------------------*
  41. * @started on: 12 May 2004 at 20:25:56 by The Celestial Celebi. *
  42. * @last edited on: 20 June 2004 at 11:36:38 by The Celestial Celebi. *
  43. * @constructs used: array, else, elseif, if, for, return. *
  44. * @functions used: ceil, intval, is_array, isset, mysql_error, *
  45. * mysql_query, mysql_result, trigger_error, str_replace, strstr, trim. *
  46. ***************************************************************************
  47. */
  48.  
  49. function sql_navigation(&$rDatabaseconnection, $aTable, $sUrl, $sAlias, $iPerpage, $iType)
  50. {
  51. if(is_array($aTable))
  52. {
  53. $sQuerybit = ' ' . $aTable[1];
  54. $sTable = $aTable[0];
  55. }
  56. else
  57. {
  58. $sTable = $aTable;
  59. $sQuerybit = '';
  60. }
  61. $sSqlgetnumberofrecordsstring = "SELECT COUNT(1) AS totalrecords FROM " . $sTable . $sQuerybit;
  62. if(!$rSqlgetnumberofrecordsquery = mysql_query($sSqlgetnumberofrecordsstring, $rDatabaseconnection))
  63. {
  64. trigger_error('sql_navigation(): Could not query the total number of records from database, ' . mysql_error(), E_USER_WARNING);
  65. return;
  66. }
  67. else
  68. {
  69. $iTotalrecords = mysql_result($rSqlgetnumberofrecordsquery, 0, 'totalrecords');
  70. $iTotalpages = ($iTotalrecords / $iPerpage);
  71. $iTotalpages = ceil($iTotalpages);
  72. if(!isset($_GET[$sAlias]) || $_GET[$sAlias] > $iTotalpages || $_GET[$sAlias] < 1 || intval($_GET[$sAlias]) != $_GET[$sAlias]) // a whole block.. it's here to validate the pagenumber as some users might f*ck it up
  73. {
  74. $iCurrent = 1;
  75. }
  76. else
  77. {
  78. $iCurrent = intval($_GET[$sAlias]);
  79. }
  80. $iStartpoint = (($iCurrent - 1) * $iPerpage);
  81. if($iCurrent == $iTotalpages && ($iTotalrecords % $iPerpage) != 0)
  82. {
  83. $iEndpoint = ($iTotalrecords % $iPerpage);
  84. if($iEndpoint == 0)
  85. {
  86. $iEndpoint += 1;
  87. }
  88. }
  89. elseif($iTotalrecords == 0)
  90. {
  91. $iEndpoint = 0;
  92. }
  93. else
  94. {
  95. $iEndpoint = $iPerpage;
  96. }
  97. $sNavigation = '';
  98. if($iTotalpages != 1 && $iTotalrecords != 0)
  99. {
  100. if(strstr($sUrl, '?'))
  101. {
  102. $sUrlseperator = '&';
  103. }
  104. else
  105. {
  106. $sUrlseperator = '?';
  107. }
  108. if($iType == NAV_BOTH || $iType == NAV_PAGENUMBERS)
  109. {
  110. for($i = 1; $i <= $iTotalpages; $i++)
  111. {
  112. if($iCurrent == $i)
  113. {
  114. $sNavigation .= ' <b>' . $i . '</b> ';
  115. }
  116. else
  117. {
  118. $sNavigation .= ' <a class="navigation" href="' . $sUrl . $sUrlseperator . $sAlias . '=' . $i . '">' . $i . '</a> ';
  119. }
  120. }
  121. }
  122. if($iType == NAV_BOTH || $iType == NAV_PREVIOUSNEXT)
  123. {
  124. if($iCurrent != 1)
  125. {
  126. $sNavigation = '<a class="navigation" href="' . $sUrl . $sUrlseperator . $sAlias . '=' . ($iCurrent - 1) . '">vorige</a> ' . $sNavigation;
  127. }
  128. else
  129. {
  130. $sNavigation = 'vorige ' . $sNavigation;
  131. }
  132. if($iCurrent < $iTotalpages)
  133. {
  134. $sNavigation .= ' <a class="navigation" href="' . $sUrl . $sUrlseperator . $sAlias . '=' . ($iCurrent + 1) . '">volgende</a>';
  135. }
  136. else
  137. {
  138. $sNavigation .= ' volgende';
  139. }
  140. }
  141. $sNavigation = str_replace(' ', ' ', $sNavigation);
  142. $sNavigation = trim($sNavigation);
  143. }
  144. return array(
  145. 'current' => $iCurrent,
  146. 'navigation' => $sNavigation,
  147. 'startpoint' => $iStartpoint,
  148. 'endpoint' => $iEndpoint,
  149. 'totalrecords' => $iTotalrecords,
  150. 'totalpages' => $iTotalpages
  151. );
  152. }
  153. }
  154. ?>
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Geen reacties (0)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.031s