醉如網

結合AJAX進行PHP開發之入門3

放大圖片
  
    現在有了一個可用的分頁器為用戶提供一些縮略圖。相冊的第二項功能是允許用戶單擊縮略圖來查看全圖。get_image_link() 函數調用了 expand. 腳本,我們現在就來編寫它。該腳本傳遞用戶希望展開的文件的索引,因此必須在此列出目錄並獲得適當的文件名。隨後的操作就很簡單了,只需創建病輸出 image 標記即可。
  
    清單 5. get_image 函數
  
  function get_image ( $index ) {
   $images = get_image_list ( 'images' );
  
   // Generate navigation
  
   $output .= '<img src=" cool.com/lanmu/images/' . $images[$index] . '" />';
   return $output;
  }
  
    接下來還要提供與分頁器類似的導航機制。“上一張” 導航到編號為 $index-1 的圖像,“下一張” 導航到編號為 $index+1 的圖像,“返回” 則回到分頁器。
  
    清單 6. get_image 導航
  
  $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>';
  
    最後創建一個簡單的 容器,將其命名為 expand. 。
  
    清單 7. get_image 導航
  
  <!DOCTYPE PUBLIC "-//W3C//DTD X 1.0 Strict//EN"
  " 1-20000126/DTD/x 1-strict.dtd">
  < ns=" ">
  <head>
  <title>Creating a simple picture album viewer</title>
  
  <style type="text/ ">
  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>
  <?
  
  $index = isset($_REQUEST['index']) ? $_REQUEST['index'] : 0;
  echo get_image($index);
  
  ?>
  </body>
  </ >
  
    這樣我們就完成了相冊。用戶可以看到所有的圖片,而且很容易導航。自然,用戶可以來回切換,甚至能通過書籤功能返回喜歡的圖片。