cvs: pearweb /public_html packages.php
| From: | Stig Bakken | Date: | Sat, 29 Dec 2001 06:23:35 +0000 |
| Subject: | cvs: pearweb /public_html packages.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-1747@lists.php.net to get a copy of this message | ||
ssb Sat Dec 29 01:23:35 2001 EDT
Modified files:
/pearweb/public_html packages.php
Log:
* some sql optimization
* add packages to the top category overview
* simplify package view :)
Index: pearweb/public_html/packages.php diff -u pearweb/public_html/packages.php:1.17 pearweb/public_html/packages.php:1.18 --- pearweb/public_html/packages.php:1.17 Fri Dec 28 18:30:39 2001 +++ pearweb/public_html/packages.php Sat Dec 29 01:23:34 2001 @@ -6,14 +6,13 @@ */ require 'HTML/Table.php'; - response_header("PEAR :: Packages"); /*************************************** ** Returns an appropriate query string ** for a self referencing link ***************************************/ - function getQueryString($catpid, $catname, $noempty = false){ + function getQueryString($catpid, $catname, $showempty = false){ if($catpid) $querystring[] = 'catpid='.$catpid; @@ -21,8 +20,8 @@ if($catname) $querystring[] = 'catname='.urlencode($catname); - if($noempty) - $querystring[] = 'noempty='.(int)$noempty; + if($showempty) + $querystring[] = 'showempty='.(int)$showempty; return '?'.implode('&', $querystring); } @@ -31,70 +30,102 @@ ** Check input variables ***************************************/ - // Expected url vars: catpid (category parent id), catname, noempty - $catpid = isset($HGV['catpid']) ? (int)$HGV['catpid'] : null; - $noempty = isset($HGV['noempty']) ? (bool)$HGV['noempty'] : false; + // Expected url vars: catpid (category parent id), catname, showempty + $catpid = isset($_GET['catpid']) ? (int)$_GET['catpid'] : null; + $showempty = isset($_GET['showempty']) ? (bool)$_GET['showempty'] : false; if (empty($catpid)) { - $category_where = "categories.parent IS NULL"; - $category_title = "Package Browser: Top Level Categories"; + $category_where = "IS NULL"; $catname = "Top Level"; } else { - $category_where = "categories.parent = " . $catpid; - $category_title = "Package Browser: " . urldecode($catname); + $category_where = "= " . $catpid; + $catname = isset($_GET['catname']) ? $_GET['catname'] : ''; } + $category_title = "Package Browser :: " . htmlspecialchars($catname); - $noempty_link = '<a href="'.$HVV['PHP_SELF'].getQueryString($catpid, $catname, !$noempty).'">'.($noempty ? 'Show all' : 'Hide empty').'</a>'; + $showempty_link = '<a href="'.$_SERVER['PHP_SELF'].getQueryString($catpid, $catname, !$showempty).'">'.($showempty ? 'Hide empty' : 'Show empty').'</a>'; /*************************************** ** Main part of script ***************************************/ - $dbh->setErrorHandling(PEAR_ERROR_DIE); + response_header($category_title); + +// $dbh->setErrorHandling(PEAR_ERROR_DIE); $dbh->setFetchmode(DB_FETCHMODE_ASSOC); // 1) Show categories of this level - $sth = $dbh->query("SELECT * from categories WHERE $category_where"); - $table = new HTML_Table('border="0" cellpadding="2" cellspacing="4" width="100%"'); + $sth = $dbh->query("SELECT * from categories WHERE parent $category_where"); + $table = new HTML_Table('border="0" cellpadding="6" cellspacing="2" width="100%"'); $nrow = 0; $catdata = array(); + // Get names of sub-categories + $subcats = $dbh->getAssoc("SELECT p.id AS pid, c.id AS id, c.name AS name, c.summary AS summary ". + " FROM categories c, categories p ". + " WHERE p.parent $category_where ". + " AND c.parent = p.id", + false, null, DB_FETCHMODE_ASSOC, true); + // Get names of sub-packages + $subpkgs = $dbh->getAssoc("SELECT p.category, p.id AS id, p.name AS name, p.summary AS summary". + " FROM packages p, categories c". + " WHERE c.parent $category_where AND p.category = c.id", + false, null, DB_FETCHMODE_ASSOC, true); + while ($sth->fetchInto($row)) { extract($row); - // Get names of sub categories - $sc = $dbh->query('SELECT id, name FROM categories WHERE parent = '.$id); - $subcat_links = array(); - - if($sc->numRows() != 0){ - while($sc->fetchInto($subcat) AND @count($subcat_links) < 3){ - $subcat_links[] = '<a href="'.$HVV['PHP_SELF'].'?catpid='.$subcat['id'].'&catname='.urlencode($subcat['name']).'">'.$subcat['name'].'</a>'; - } - } - $subcat_links = $sc->numrows() <= 3 ? implode(', ', $subcat_links) : implode(', ', $subcat_links).'...'; - $ncategories = ($cat_right - $cat_left - 1) / 2; - if ($noempty AND ($npackages <= 0)) { + if (!$showempty AND $ncategories < 1) { continue; // Show categories with packages } + $sub_items = 0; + $max_sub_links = 4; + + $sub_links = array(); + if (isset($subcats[$id])) { + foreach ($subcats[$id] as $subcat) { + $sub_links[] = '<b><a href="'.$_SERVER['PHP_SELF'].'?catpid='.$subcat['id'].'&catname='. + urlencode($subcat['name']).'" title="'.htmlspecialchars($subcat['summary']).'">'.$subcat['name'].'</a></b>'; + if (sizeof($sub_links) >= $max_sub_links) { + break; + } + } + } + + if (isset($subpkgs[$id])) { + foreach ($subpkgs[$id] as $subpkg) { + $sub_links[] = '<a href="pkginfo.php?pacid='.$subpkg['id'].'" title="'. + htmlspecialchars($subpkg['summary']).'">'.$subpkg['name'].'</a>'; + if (sizeof($sub_links) >= $max_sub_links) { + break; + } + } + } + + if (sizeof($sub_links) >= $max_sub_links) { + $sub_links = implode(', ', $sub_links) . ' <img src="/gifs/caret-r.gif" border="0" alt="[more]" />'; + } else { + $sub_links = implode(', ', $sub_links); + } + + settype($npackages, 'string'); settype($ncategories, 'string'); - $data = '<font size="+1"><a href="'.$HVV['PHP_SELF'].'?catpid='.$id.'&catname='.urlencode($name).'"><strong>'.$name.'</strong></a></font> ('.$npackages.')<br />';//$name; //array($name, $npackages, $ncategories, $summary); - $data .= '<font size="-1">'.$subcat_links.'</font><br />'; + $data = '<font size="+1"><b><a href="'.$_SERVER['PHP_SELF'].'?catpid='.$id.'&catname='.urlencode($name).'">'.$name.'</a></b></font> ('.$npackages.')<br />';//$name; //array($name, $npackages, $ncategories, $summary); + $data .= $sub_links.'<br />'; $catdata[] = $data; - if($nrow % 2 == 1){ + if($nrow++ % 2 == 1){ $table->addRow(array($catdata[0], $catdata[1])); $table->setCellAttributes($table->getRowCount()-1, 0, 'width="50%"'); $table->setCellAttributes($table->getRowCount()-1, 1, 'width="50%"'); $catdata = array(); } - ++$nrow; - $sc->free(); } // End while // Any left over (odd number of categories). @@ -105,58 +136,44 @@ } /*************************************** -** Print the urhere text, noempty link +** Print the urhere text, showempty link ** and the categories ***************************************/ - echo '<table border="0" width="100%"><tr><td valign="top">'; + echo '<table border="0" width="100%"><tr><th valign="top" align="left">Contents of :: '; html_category_urhere($catpid, $catname); - echo '</td><td valign="top" align="right">'.$noempty_link.'</td></tr>'; - echo '<tr><td colspan="2" align="center">Looking for something specific? Try the <a href="package-search.php">package search</a>.</td></tr></table>'; - - echo $nrow != 0 ? $table->toHtml() : ''; - $sth->free(); + echo '</th><td valign="top" align="right">'.$showempty_link.'</td></tr>'; +// echo '<tr><td colspan="2">Looking for something specific? Try the <a href="package-search.php">package search</a>.</td></tr>'; + print '</table>'; /*************************************** ** Begin code for showing packages if we ** aren't at the top level. ***************************************/ - if(isset($catpid)){ - print '<h1 align="center">Packages</h1>'; + if (isset($catpid)){ /* XXXX TODO: - Show link to direct download - Paginate results (use my Pager?) */ $nrow = 0; - $table = new HTML_Table('border="0" cellpadding="2" cellspacing="1" width="100%"'); if (!empty($catpid)) { $sth = $dbh->query("SELECT id, name, summary FROM packages WHERE category=$catpid"); - + print "<dl>\n"; while ($sth->fetchInto($row)) { extract($row); - if ($nrow == 0) { - $table->addRow(array("Name", "Summary"), 'bgcolor="#ffffff"', 'TH'); - } - $bg = ($nrow++ % 2) ? '#f0f0f0' : '#e0e0e0'; - $name = "<a href=\"pkginfo.php?pacid=$id\">$name</a>"; - $table->addRow(array($name, $summary)); - $table->setCellAttributes($nrow, 0, "width=\"20%\" bgcolor=\"$bg\""); - $table->setCellAttributes($nrow, 1, "width=\"80%\" bgcolor=\"$bg\""); + print " <dt><a href=\"pkginfo.php?pacid=$id\">$name</a></dt>\n"; + print " <dd>$summary</dd>\n"; + print " <br /><br />\n"; } } - /*************************************** - ** Print the packages - ***************************************/ - - if ($nrow == 0) { - print '<center><p>No packages in this category</p></center>'; - }else{ - html_table_border($table); - } } - + + + echo $nrow != 0 ? $table->toHtml() : ''; + $sth->free(); + response_footer(); ?>
Index: pearweb/public_html/packages.php diff -u pearweb/public_html/packages.php:1.17 pearweb/public_html/packages.php:1.18 --- pearweb/public_html/packages.php:1.17 Fri Dec 28 18:30:39 2001 +++ pearweb/public_html/packages.php Sat Dec 29 01:23:34 2001 @@ -6,14 +6,13 @@ */ require 'HTML/Table.php'; - response_header("PEAR :: Packages"); /*************************************** ** Returns an appropriate query string ** for a self referencing link ***************************************/ - function getQueryString($catpid, $catname, $noempty = false){ + function getQueryString($catpid, $catname, $showempty = false){ if($catpid) $querystring[] = 'catpid='.$catpid; @@ -21,8 +20,8 @@ if($catname) $querystring[] = 'catname='.urlencode($catname); - if($noempty) - $querystring[] = 'noempty='.(int)$noempty; + if($showempty) + $querystring[] = 'showempty='.(int)$showempty; return '?'.implode('&', $querystring); } @@ -31,70 +30,102 @@ ** Check input variables ***************************************/ - // Expected url vars: catpid (category parent id), catname, noempty - $catpid = isset($HGV['catpid']) ? (int)$HGV['catpid'] : null; - $noempty = isset($HGV['noempty']) ? (bool)$HGV['noempty'] : false; + // Expected url vars: catpid (category parent id), catname, showempty + $catpid = isset($_GET['catpid']) ? (int)$_GET['catpid'] : null; + $showempty = isset($_GET['showempty']) ? (bool)$_GET['showempty'] : false; if (empty($catpid)) { - $category_where = "categories.parent IS NULL"; - $category_title = "Package Browser: Top Level Categories"; + $category_where = "IS NULL"; $catname = "Top Level"; } else { - $category_where = "categories.parent = " . $catpid; - $category_title = "Package Browser: " . urldecode($catname); + $category_where = "= " . $catpid; + $catname = isset($_GET['catname']) ? $_GET['catname'] : ''; } + $category_title = "Package Browser :: " . htmlspecialchars($catname); - $noempty_link = '<a href="'.$HVV['PHP_SELF'].getQueryString($catpid, $catname, !$noempty).'">'.($noempty ? 'Show all' : 'Hide empty').'</a>'; + $showempty_link = '<a href="'.$_SERVER['PHP_SELF'].getQueryString($catpid, $catname, !$showempty).'">'.($showempty ? 'Hide empty' : 'Show empty').'</a>'; /*************************************** ** Main part of script ***************************************/ - $dbh->setErrorHandling(PEAR_ERROR_DIE); + response_header($category_title); + +// $dbh->setErrorHandling(PEAR_ERROR_DIE); $dbh->setFetchmode(DB_FETCHMODE_ASSOC); // 1) Show categories of this level - $sth = $dbh->query("SELECT * from categories WHERE $category_where"); - $table = new HTML_Table('border="0" cellpadding="2" cellspacing="4" width="100%"'); + $sth = $dbh->query("SELECT * from categories WHERE parent $category_where"); + $table = new HTML_Table('border="0" cellpadding="6" cellspacing="2" width="100%"'); $nrow = 0; $catdata = array(); + // Get names of sub-categories + $subcats = $dbh->getAssoc("SELECT p.id AS pid, c.id AS id, c.name AS name, c.summary AS summary ". + " FROM categories c, categories p ". + " WHERE p.parent $category_where ". + " AND c.parent = p.id", + false, null, DB_FETCHMODE_ASSOC, true); + // Get names of sub-packages + $subpkgs = $dbh->getAssoc("SELECT p.category, p.id AS id, p.name AS name, p.summary AS summary". + " FROM packages p, categories c". + " WHERE c.parent $category_where AND p.category = c.id", + false, null, DB_FETCHMODE_ASSOC, true); + while ($sth->fetchInto($row)) { extract($row); - // Get names of sub categories - $sc = $dbh->query('SELECT id, name FROM categories WHERE parent = '.$id); - $subcat_links = array(); - - if($sc->numRows() != 0){ - while($sc->fetchInto($subcat) AND @count($subcat_links) < 3){ - $subcat_links[] = '<a href="'.$HVV['PHP_SELF'].'?catpid='.$subcat['id'].'&catname='.urlencode($subcat['name']).'">'.$subcat['name'].'</a>'; - } - } - $subcat_links = $sc->numrows() <= 3 ? implode(', ', $subcat_links) : implode(', ', $subcat_links).'...'; - $ncategories = ($cat_right - $cat_left - 1) / 2; - if ($noempty AND ($npackages <= 0)) { + if (!$showempty AND $ncategories < 1) { continue; // Show categories with packages } + $sub_items = 0; + $max_sub_links = 4; + + $sub_links = array(); + if (isset($subcats[$id])) { + foreach ($subcats[$id] as $subcat) { + $sub_links[] = '<b><a href="'.$_SERVER['PHP_SELF'].'?catpid='.$subcat['id'].'&catname='. + urlencode($subcat['name']).'" title="'.htmlspecialchars($subcat['summary']).'">'.$subcat['name'].'</a></b>'; + if (sizeof($sub_links) >= $max_sub_links) { + break; + } + } + } + + if (isset($subpkgs[$id])) { + foreach ($subpkgs[$id] as $subpkg) { + $sub_links[] = '<a href="pkginfo.php?pacid='.$subpkg['id'].'" title="'. + htmlspecialchars($subpkg['summary']).'">'.$subpkg['name'].'</a>'; + if (sizeof($sub_links) >= $max_sub_links) { + break; + } + } + } + + if (sizeof($sub_links) >= $max_sub_links) { + $sub_links = implode(', ', $sub_links) . ' <img src="/gifs/caret-r.gif" border="0" alt="[more]" />'; + } else { + $sub_links = implode(', ', $sub_links); + } + + settype($npackages, 'string'); settype($ncategories, 'string'); - $data = '<font size="+1"><a href="'.$HVV['PHP_SELF'].'?catpid='.$id.'&catname='.urlencode($name).'"><strong>'.$name.'</strong></a></font> ('.$npackages.')<br />';//$name; //array($name, $npackages, $ncategories, $summary); - $data .= '<font size="-1">'.$subcat_links.'</font><br />'; + $data = '<font size="+1"><b><a href="'.$_SERVER['PHP_SELF'].'?catpid='.$id.'&catname='.urlencode($name).'">'.$name.'</a></b></font> ('.$npackages.')<br />';//$name; //array($name, $npackages, $ncategories, $summary); + $data .= $sub_links.'<br />'; $catdata[] = $data; - if($nrow % 2 == 1){ + if($nrow++ % 2 == 1){ $table->addRow(array($catdata[0], $catdata[1])); $table->setCellAttributes($table->getRowCount()-1, 0, 'width="50%"'); $table->setCellAttributes($table->getRowCount()-1, 1, 'width="50%"'); $catdata = array(); } - ++$nrow; - $sc->free(); } // End while // Any left over (odd number of categories). @@ -105,58 +136,44 @@ } /*************************************** -** Print the urhere text, noempty link +** Print the urhere text, showempty link ** and the categories ***************************************/ - echo '<table border="0" width="100%"><tr><td valign="top">'; + echo '<table border="0" width="100%"><tr><th valign="top" align="left">Contents of :: '; html_category_urhere($catpid, $catname); - echo '</td><td valign="top" align="right">'.$noempty_link.'</td></tr>'; - echo '<tr><td colspan="2" align="center">Looking for something specific? Try the <a href="package-search.php">package search</a>.</td></tr></table>'; - - echo $nrow != 0 ? $table->toHtml() : ''; - $sth->free(); + echo '</th><td valign="top" align="right">'.$showempty_link.'</td></tr>'; +// echo '<tr><td colspan="2">Looking for something specific? Try the <a href="package-search.php">package search</a>.</td></tr>'; + print '</table>'; /*************************************** ** Begin code for showing packages if we ** aren't at the top level. ***************************************/ - if(isset($catpid)){ - print '<h1 align="center">Packages</h1>'; + if (isset($catpid)){ /* XXXX TODO: - Show link to direct download - Paginate results (use my Pager?) */ $nrow = 0; - $table = new HTML_Table('border="0" cellpadding="2" cellspacing="1" width="100%"'); if (!empty($catpid)) { $sth = $dbh->query("SELECT id, name, summary FROM packages WHERE category=$catpid"); - + print "<dl>\n"; while ($sth->fetchInto($row)) { extract($row); - if ($nrow == 0) { - $table->addRow(array("Name", "Summary"), 'bgcolor="#ffffff"', 'TH'); - } - $bg = ($nrow++ % 2) ? '#f0f0f0' : '#e0e0e0'; - $name = "<a href=\"pkginfo.php?pacid=$id\">$name</a>"; - $table->addRow(array($name, $summary)); - $table->setCellAttributes($nrow, 0, "width=\"20%\" bgcolor=\"$bg\""); - $table->setCellAttributes($nrow, 1, "width=\"80%\" bgcolor=\"$bg\""); + print " <dt><a href=\"pkginfo.php?pacid=$id\">$name</a></dt>\n"; + print " <dd>$summary</dd>\n"; + print " <br /><br />\n"; } } - /*************************************** - ** Print the packages - ***************************************/ - - if ($nrow == 0) { - print '<center><p>No packages in this category</p></center>'; - }else{ - html_table_border($table); - } } - + + + echo $nrow != 0 ? $table->toHtml() : ''; + $sth->free(); + response_footer(); ?>