结合AJAX进行PHP开发之入门7
点击次数:35 次 发布日期:2008-11-26 11:39:35 作者:源代码网
|
源代码网推荐 源代码网推荐 function get_meta_data ( $file ) { 源代码网推荐 源代码网推荐 // Using getimagesize, the server calculates the dimensions 源代码网推荐 list($width, $height) = @getimagesize("http://www.zzchn.com/edu/20080727/images/$file"); 源代码网推荐 $output = "<p>Width: {$width}px, Height: {$height}px</p>"; 源代码网推荐 源代码网推荐 // Use SimpleXML package in PHP_v5: 源代码网推荐 // http://us3.php.net/manual/en/ref.simplexml.php 源代码网推荐 $xml = simplexml_load_file("gallery.xml"); 源代码网推荐 源代码网推荐 foreach ( $xml as $photo ) { 源代码网推荐 if ($photo["id"] == $file) { 源代码网推荐 $output .= !empty($photo->date) ? "<p>Date taken:{$photo->date}</p>" : ""; 源代码网推荐 $output .= !empty($photo->locale) ? "<p>Location:{$photo->locale}>/p>" : ""; 源代码网推荐 $output .= !empty($photo->comment) ? "<p>Comment:{$photo->comment}</p>" : ""; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 return $output; 源代码网推荐 源代码网推荐 要注意的是,get_meta_data() 函数中还使用 getimagesize()(一个核心 PHP 函数,不需要 GD)计算图像的大小。 源代码网推荐 源代码网推荐 再回到 get_image() 函数,它包含由 get_image_list() 生成的文件名的列表。查找元数据只需要将文件名传递给该函数即可。 源代码网推荐 源代码网推荐 清单 14. 添加元数据 源代码网推荐 源代码网推荐 function get_image ( $index ) { 源代码网推荐 $images = get_image_list ( "images" ); 源代码网推荐 源代码网推荐 // ... 源代码网推荐 源代码网推荐 $output .= "<img src="http://www.zzchn.com/edu/20080727/images/" . $images[$index] . "" />"; 源代码网推荐 $output .= "<div id="meta_data">" . 源代码网推荐 get_meta_data( $images[$index] ) . "</div>"; 源代码网推荐 return $output; 源代码网推荐 } 源代码网推荐 源代码网推荐 重新打开页面将看到服务器请求的结果。图 7 显示了带有元数据的放大的图像。 源代码网推荐
源代码网推荐 图 7. 使用元数据的相册 源代码网推荐 源代码网推荐 结束语 源代码网推荐 源代码网推荐 我们看到,使用 Sajax 可以消除客户机和服务器之间的障碍,程序员能够进行无缝远程函数调用而不用担心传输层、HTTP GET 和 POST 请求。我们可以花更多时间编写提供数据的 PHP 脚本以及表示层和控制层的 JavaScript。在这个相册例子中,我们让客户机直接连接到图像数据库。通过添加简单的元数据,我们看到让用户直接访问服务器上的信息是多么简单,无需担心协议的问题。 源代码网推荐 源代码网推荐 与所有的 Ajax 应用程序一样,我们的相册也有一个致命的弱点:没有使用浏览器的 “访问历史”,因为破坏了后退按钮的功能。在 “利用 PHP 开发 Ajax 应用程序” 系列的第 2 部分中,我们将通过实现历史记录缓冲和状态跟踪机制来解决这个问题。 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
