当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  结合AJAX进行PHP开发之入门2

 结合AJAX进行PHP开发之入门2

点击次数:24 次 发布日期:2008-11-26 11:40:32 作者:源代码网
源代码网推荐      /*
源代码网推荐  * 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="http://www.zzchn.com/edu/20080727/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;
源代码网推荐  }
源代码网推荐  
源代码网推荐    这个表很简单,它从索引号 $limit_start 开始遍历图片列表。然后放上每个图片的缩略图,每五张图片作为一行。达到 $limit_start + $limit_step 的时候循环结束。
源代码网推荐  
源代码网推荐    该表是目录列表的可视化表示,因此需要一个函数列出目录中的所有图像。清单 1 中的 get_file_list() 函数用索引数组返回 /images 目录中的所有图片列表。下面是一个示例实现。
源代码网推荐  
源代码网推荐    清单 2. get_file_list 实现
源代码网推荐  
源代码网推荐  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>";
源代码网推荐  
源代码网推荐    最后还要编写 get_image_link() 和 get_table_link() 函数,让用户将缩略图展开成完整的图像(参见清单 4)。注意,脚本 index.php(以及后面要创建的 expand.php)只在这两个函数中调用。这样就很容易改变链接的功能。事实上在下面与 Sajax 进行集成时,只有这两个函数需要修改。
源代码网推荐  
源代码网推荐    清单 4. get_image_link、get_table_link 实现
源代码网推荐  
源代码网推荐  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>";
源代码网推荐  }
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华