- windows XP |
- windows vista |
- windows 2000 |
- windows 2003 |
- windows 2008 |
- linux |
- unix |
- mac
- access |
- mysql |
- sql server |
- oracle |
- DB2
- word |
- excel |
- powerpoint |
- wps |
图 1. 分页器提供了显示用户照片的一种方式 |
| /* * Find a list of images in /images and provide thumbnails */ function get_table ( $limit_start = 0, $limit_step = 5 ) { $images = get_image_list('images'); // Generate navigation for Previous and Next buttons // Code given below $output .= '<table class="image_table">'; $columns = 5; foreach ($images as $index => $image) { // Begin directory listing at item number $limit_start if ( $index < $limit_start ) continue; // End directory listing at item number $limit_end if ( $index >= $limit_start + $limit_step ) continue; // Begin column if ( $index - $limit_start % $columns == 0 ) { $output .= '<tr>'; } // Generate link to blown up image (see below) $thumbnail = '<img src="thumbnails/' . $image . '" />'; $output .= '<td>' . get_image_link($thumbnail, $index) . '</td>'; // Close column if ( $index - $limit_start % $columns == $columns - 1 ) { $output .= '</tr>'; } } $output .= '</table>'; return $nav . $output; } |
| function get_image_list ( $image_dir ) { $d = dir($image_dir); $files = array(); if ( !$d ) return null; while (false !== ($file = $d->read())) { // getimagesize returns true only on valid images if ( @getimagesize( $image_dir . '/' . $file ) ) { $files[] = $file; } } $d->close(); return $files; } |
注意:本文后面还要使用 get_file_list() 函数。有一点很重要,无论何时调用该函数,返回的数组都是不变的。因为提供的实现要进行目录搜索,必须保证目录中的指定文件不会改变,每次都要按字母顺序排序。
导航的实现
虽然表格列出了目录中的一些图像,但用户还需要一种查看表格中未出现的图片的方法。要真正实现分页器的导行,则需要一套标准的链接:首页、上一页、下一页和尾页。
清单 3. 分页器导航
| // Append navigation $output = '<h4>Showing items ' . $limit_start . '-' . min($limit_start + $limit_step - 1, count($images)) . ' of ' . count($images) . '<br />'; $prev_start = max(0, $limit_start - $limit_step); if ( $limit_start > 0 ) { $output .= get_table_link('<<', 0, $limit_step); $output .= ' | ' . get_table_link('Prev', $prev_start, $limit_step); } else { $output .= '<< | Prev'; } // Append next button $next_start = min($limit_start + $limit_step, count($images)); if ( $limit_start + $limit_step < count($images) ) { $output .= ' | ' . get_table_link('Next',$next_start, $limit_step); $output .= ' | ' . get_table_link('>>',(count($images) - $limit_step), $limit_step); } else { $output .= ' | Next | >>'; } $output .= '</h4>'; |
| function get_table_link ( $title, $start, $step ) { $link = "index.php?start=$start&step=$step"; return '<a href="' . $link . '">' . $title .'</a>'; } function get_image_link ( $title, $index ) { $link = "expand.php?index=$index"; return '<a href="' . $link . '">' . $title . '</a>'; } |
| function get_image ( $index ) { $images = get_image_list ( 'images' ); // Generate navigation $output .= '<img src="images/' . $images[$index] . '" />'; return $output; } |
| $output .= '<h4>Viewing image ' . $index .' of ' . count($images) . '<br />'; if ( $index > 0 ) { $output .= get_image_link('<<', 0); $output .= ' | ' . get_image_link('Prev', $index-1); } else { $output .= '<< | Prev'; } $output .= ' | ' . get_table_link('Up', $index, 5); if ( $index < count($images) ) { $output .= ' | ' . get_image_link('Next', $index+1); $output .= ' | ' . get_image_link('>>', count($images)); } else { $output .= ' | Next | >>'; } $output .= '</h4>'; |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Creating a simple picture album viewer</title> <style type="text/css"> body { text-align: center } table.image_table { margin: 0 auto 0 auto; width: 700px; padding:10px; border: 1px solid #ccc; background: #eee; } table.image_table td { padding: 5px } table.image_table a { display: block; } table.image_table img { display: block; width: 120px; padding: 2px; border: 1px solid #ccc; } </style> </head> <body> <h1>Creating a simple picture album viewer</h1> <?php $index = isset($_REQUEST['index']) ? $_REQUEST['index'] : 0; echo get_image($index); ?> </body> </html> |
图 2. 完成的相册 |