[35223] trunk/www/ports.php

eridius at macports.org eridius at macports.org
Thu Mar 20 23:04:53 PDT 2008


Revision: 35223
          http://trac.macosforge.org/projects/macports/changeset/35223
Author:   eridius at macports.org
Date:     2008-03-20 23:04:51 -0700 (Thu, 20 Mar 2008)

Log Message:
-----------
Add fairly simple implementation of paging to the by=all view

Modified Paths:
--------------
    trunk/www/ports.php

Modified: trunk/www/ports.php
===================================================================
--- trunk/www/ports.php	2008-03-21 05:34:48 UTC (rev 35222)
+++ trunk/www/ports.php	2008-03-21 06:04:51 UTC (rev 35223)
@@ -20,6 +20,8 @@
     }
     $by = isset($_GET['by']) ? $_GET['by'] : '';
     $substr = isset($_GET['substr']) ? $_GET['substr'] : '';
+    $page = isset($_GET['page']) ? max($_GET['page'], 0) : '';
+    $pagesize = isset($_GET['pagesize']) ? max($_GET['pagesize'], 0) : 50; # arbitrary setting
 
     print_header('The MacPorts Project -- Available Ports', 'utf-8');
 ?>
@@ -110,6 +112,7 @@
             break;
         case 'all':
             $criteria = '';
+            $paging = true;
             break;
         default:
             $criteria = '0';
@@ -117,12 +120,42 @@
         }
         $where = ($criteria == '' ? '' : "WHERE $criteria");
         $query = "SELECT DISTINCT $fields FROM $tables $where ORDER BY name";
+        if ($paging) {
+            $offset = $pagesize * $page;
+            $query .= " LIMIT $pagesize OFFSET $offset";
+            # get the total count
+            $countquery = "SELECT COUNT(*) FROM $tables $where ORDER By name";
+            $result = mysql_query($countquery);
+            $row = mysql_fetch_array($result); # only 1 row
+            $totalcount = $row[0];
+            $pagecount = ceil($totalcount / $pagesize);
+            # generate a paging control and cache it so we can show it twice
+            $pagecontrol = "<p>Page ";
+            for ($i = 0; $i < $pagecount; $i++) {
+                if ($i != 0) {
+                    $pagecontrol .= " | ";
+                }
+                if ($i == $page) {
+                    $pagecontrol .= "<b>$i</b>";
+                } else {
+                    $pagecontrol .= "<a href=\"$_SERVER[PHP_SELF]?by=$by&amp;substr=$substr&amp;page=$i&amp;pagesize=$pagesize\">$i</a>";
+                }
+            }
+            $pagecontrol .= "</p>";
+        }
         $result = mysql_query($query);
         
         /* Main query sent to the DB */
         if ($result) {
-            print '<h3>Query Results</h3><p><i>' . mysql_num_rows($result) . ' ' . (mysql_num_rows($result) == 1 ? 'Portfile' : 'Portfiles')
-            . ' Selected</i></p>';
+            print '<h3>Query Results</h3>';
+            $resultrows = mysql_num_rows($result);
+            if ($paging) {
+                print $pagecontrol;
+                $numrows = ($offset+1) . "-" . ($offset + $resultrows) . " of $totalcount Portfile" . ($totalcount == 1 ? '' : 's');
+            } else {
+                $numrows = "$resultrows Portfile" . ($resultrows == 1 ? '' : 's');
+            }
+            print "<p><i>$numrows Selected</i></p>";
 
             print '<dl>';
             /* Iterate over the entire set of returned ports */
@@ -210,6 +243,10 @@
             } /* while (listing of macthing ports) */
             print '</dl>';
 
+            if ($paging) {
+                print $pagecontrol;
+            }
+
         /* When we hit this else, the query failed for some reason */
         } else {
             print '<p>An Error Occurred: '. mysql_error($portsdb_info['connection_handler']) . '</p>';

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20080320/8628ed83/attachment.html 


More information about the macports-changes mailing list