Pagination with PHP & MySQL
0 comments
Many CRUD applications display long lists of search results- perhaps showing all rows in a table. Pagination, or splitting the search results into pages, is a great way to assist users so that they don't get a mile-long list of items and saves database processing power, too! Here is a simple method for paginating search results...

PHP Directory Traversal
0 comments
Here is a simple function to recursively analyze a given directory and print hyperlinks to all of the files in each directory. Directory names are printed but not hyperlinked.
<?php /** * This function recursively reads through a directory and prints hyperlinks to the files in it and in all subdirectories * example: To print all contents in the current directory, use: <?php read_directory('.'); ?> * @param $directory string the relative path to the directory to search * @param $delim string a string to be printed to the left of each directory and file. * @param $repeat integer the number of times to repeat $delim at each directory level * @param $depth used only in recursive function calls * */ <?php function read_directory($directory, $delim = '-', $repeat = 5, $depth = 0){ if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { $path = $directory . '/' . $file; if(is_dir($path)){ echo '' . str_repeat($delim,$depth * $repeat) . '' . $file . "/<br />\r\n"; read_directory($path, $delim,$repeat, $depth + 1); } else { echo str_repeat($delim,$depth * 5) . '<a href="' . $path . ' ">' . $file . "</a><br />\r\n"; } } } closedir($handle); } } ?>
This function should work on both *nix and Windows hosts.

Simple PHP Script to Change Navigation Style Based on Current Page
0 comments
Here's a short script which can be used to generate dynamic menus and change the CSS style of a given menu element based on the current page. New elements can be added to the navigation menu by adding them to the $menu array; this array can be easily generated via database calls or configuration files. When generating the menu, a different class name will be set for that menu item corresponding to the current page. This isn't a fool-proof method and works best for small sites with simple navigation (i.e., every page has a navigation link).
<?php $menu = array(); $menu['Home'] = 'index.php'; $menu['News & Events'] = 'newsAndEvents.php'; $menu['Photos'] = 'photos.php'; $menu['Comments'] = 'comments.php'; ?> <ul> <?php $i = 0; foreach ($menu as $key => $value) { ?> <li class="menu_item<?php echo(strripos($_SERVER['PHP_SELF'],$value) > 0 ? '_highlighted':'') ?>" id="header_<?php echo $i++ ?>"> <a href="<?php echo $value; ?>" class="header"><?php echo $key; ?></a> </li> <?php }?> </ul>