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

2016-01-29 13:28 5 1 收藏

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

【 tulaoshi.com - PHP 】

  导航的实现

  虽然表格列出了目录中的一些图像,但用户还需要一种查看表格中未出现的图片的方法。要真正实现分页器的导行,则需要一套标准的链接:首页、上一页、和尾页。

  清单 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>';
}
  放大图片

  现在有了一个可用的分页器为用户提供一些缩略图。相册的第二项功能是允许用户单击缩略图来查看全图。get_image_link() 函数调用了expand.php脚本,我们现在就来编写它。该脚本传递用户希望展开的文件的索引,因此必须在此列出目录并获得适当的文件名。随后的操作就很简单了,只需创建病输出image 标记即可。

  清单 5. get_image 函数

function get_image ( $index ) {
 $images = get_image_list ( 'images' );

 // Generate navigation

 $output .= '<img src="http://img.jcwcn.com/attachment/portal" />';
 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>';
  最后创建一个简单的 HTML 容器,将其命名为 expand.php。

  清单 7. get_image 导航

<!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 }
tabl

来源:https://www.tulaoshi.com/n/20160129/1490283.html

延伸阅读
标签: PHP
在第 1 部分中,相册中的每个链接都是由 get_table_link() 和 get_image_link() 两个函数生成的。通过编辑这些函数,可以在调用 Sajax 函数之前让该函数先调用历史堆栈。清单 9 以粗体显示了这些变化。 清单 9. get_table_link() 和 get_image_link() 函数的更新版本 function get_table_link ( $title, $start, $step ...
标签: PHP
下面是模版式的HMTL代码,保存在 Todo/Entry/templates/index.chunk;  <html   <body     <form name="entry" method="post"       <h3Create Entry</h3       <p       &nbs...
标签: Web开发
Ajax 由 HTML、JavaScript™ 技术、DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序。对于Ajax,最核心的一个对象是XMLHttpRequest,所有的Ajax操作都离不开对这个对象的操作。 首先我们来了解怎么在javascript中创建这个对象: varxmlHttp=newXMLHttpRequest(); 这行简单的代码在 Mozilla、Fire...
标签: PHP
 【PHPChina讯】 javascript :tagshow(event, 'HTML');" href="javascript:;" target=_self> HTML 部分: <html <head <scrīpt language="javascrīpt" function postRequest(strURL){ var xmlHttp; if(window.XMLHttpRequest){ // For Mozilla, Safari, ... var xmlHttp = new XMLHttpRequest(); } els...
标签: Web开发
表单处理 PHP为用户提供了众多方便易用的强大功能。在表单的处理方面,PHP能够自动地将由客户端表单发送的数据赋值给相应变量,从而极大地简化了整个表单的处理过程。 举例来说,用户建立如下表单: INPUT TYPE=TEXT NAME=“name” VALUE=“PETER” 当使用PHP对上述代码进行处理时,PHP自动创建一个名为$name的变量,并将变量值“P...

经验教程

166

收藏

29
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部