- windows XP |
- windows vista |
- windows 2000 |
- windows 2003 |
- windows 2008 |
- linux |
- unix |
- mac
- access |
- mysql |
- sql server |
- oracle |
- DB2
- word |
- excel |
- powerpoint |
- wps |
将 Sajax 连接到相册
利用刚刚创建的代码,我们将用 Sajax 迅速把相册从多页面应用程序转化成活动的 Ajax 应用程序。
因为相册主要有两个函数,get_table() 和 get_image(),这也是需要用 Sajax 导出的全部函数。事实上,为了通过 Sajax 调用这些函数,这些函数本身基本上不需要修改,很快我们就会看到,我们只需要修改生成的链接即可。
清单 9. Sajax 相册的头部
| <?php require("Sajax.php"); function get_image () { } // Defined later function get_thumbs_table () { } // Defined later // Standard Sajax stuff. Use Get, and export two // main functions to javascript $sajax_request_type = "GET"; sajax_init(); sajax_export("get_thumbs_table", "get_image"); sajax_handle_client_request(); ?> |
| <body> <h1>Sajax photo album</h1> <div id="window"></div> </body> |
| <head> <title>Creating a Sajax photo album</title> <style type="text/css"> body { text-align: center } div#window { margin: 0 auto 0 auto; width: 700px; padding: 10px; border: 1px solid #ccc; background: #eee; } table.image_table { margin: 0 auto 0 auto; } 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; } img.full { display: block; margin: 0 auto 0 auto; width: 300px; border: 1px solid #000 } </style> <script language="javascript"> <? sajax_show_javascript(); ?> // Outputs directly to the "window" div function to_window(output) { document.getElementById("window").innerHTML = output; } window.onload = function() { x get table to window); }; </script> </head> |
图 6. 完成的基于 Sajax 的相册(缩略图) |
可以看到 URL 仍然保持不变,并带来了更多愉快的用户体验。window div 显示在一个灰色的框中,通过 Sajax 生成的内容非常清楚。脚本不一定要知道自身或者它在服务器上的位置,因为所有的链接最终都成为直接对页面自身的 JavaScript 调用。因此我们的代码能够很好的模块化。我们只需要保持 JavaScript 和 PHP 函数在同一个页面上即可,即使页面位置发生了变化也没有关系。
扩展相册
使用 Sajax 把我们的相册变成活动的 Web 应用程序如此轻而易举,我们要再花点时间添加一些功能,进一步说明 Sajax 如何使从服务器检索数据变得完全透明。我们将为相册添加元数据功能,这样用户就能为他们的图片添加说明。
元数据
没有上下文说明的相册是不完整的,比如照片的来源、作者等。为此我们要将图像集中起来创建一个简单的 XML 文件。根节点是 gallery,它包含任意多个 photo 节点。每个 photo 节点都通过其 file 属性来编号。在 photo 节点中可以使用任意多个标记来描述照片,但本例中只使用了 date、locale 和 comment。
清单 12. 包含元数据的 XML 文件
| <?xml version="1.0"?> <gallery> <photo file="image01.jpg"> <date>August 6, 2006</date> <locale>Los Angeles, CA</locale> <comment>Here's a photo of my favorite celebrity</comment> </photo> <photo file="image02.jpg"> <date>August 7, 2006</date> <locale>San Francisco, CA</locale> <comment>In SF, we got to ride the street cars</comment> </photo> <photo file="image03.jpg"> <date>August 8, 2006</date> <locale>Portland, OR</locale> <comment>Time to end our road trip!</comment> </photo> </gallery> |
| function get_meta_data ( $file ) { // Using getimagesize, the server calculates the dimensions list($width, $height) = @getimagesize("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; |
| function get_image ( $index ) { $images = get_image_list ( 'images' ); // ... $output .= '<img src="images/' . $images[$index] . '" />'; $output .= '<div id="meta_data">' . get_meta_data( $images[$index] ) . '</div>'; return $output; } |
图 7. 使用元数据的相册 |