Introduction to PHP development with Ajax (1)

Source: Internet
Author: User

Asynchronous JavaScript and XML (Asynchronous JavaScript and XML, Ajax) are undoubtedly the most popular new Web technologies. The "PHP development with Ajax" series consists of two parts. We will use PHP and Simple Ajax Toolkit (Sajax) to create a Simple album as an online Web application. We first use the standard PHP development method to compile a simple album, and then use Sajax to turn it into an active Web application.

Create a simple album

This article uses two methods to create a simple album: traditional Web applications and Sajax-based applications. We will write an album in PHP, read the content in a directory, and display a table composed of thumbnails. If you click a thumbnail, the image is fully expanded. Because traditional applications are compiled, each click is a new HTTP request, and the parameters are transmitted as part of the URL.

You will learn how to apply the Sajax library to photo albums and how Sajax can accelerate application development.

Add a page sharer table

To access the album, users need to quickly view the photo. Because many large photos are not easily displayed on one page, you need to create a simple table that displays a small number of thumbnails each time. You also need to write navigation to help you move back and forth in the image list.

Figure 1. The page splitter provides a way to display User photos

498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 297 alt = the binder provides a way to show user photos src = "http://www.bkjia.com/uploadfile/2013/0904/20130904094446269.jpg" width = 450>

First, collect at least 20. jpg images and put them in a folder. Each image also has a thumbnail saved in a separate thumbnail folder. Although you can use the GD package to generate thumbnails, this article assumes that you have prepared the thumbnails. You can also use the photos and thumbnails provided in this Article.

To complete the rest of this article, we assume that the image is saved in the/images subdirectory and the thumbnail is placed in/images/thumbnails. You can make appropriate changes in the Code. In addition, we also assume that the thumbnail and the corresponding image use the same name.

The page splitter should pass two parameters: start is the index number of the first photo displayed in alphabetical order, and step is the number of photos displayed.

List 1. album Viewer

/*
* 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. ='



// 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. ='


// Generate link to blown up image (see below)
$ Thumbnail = '2017) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'src = "thumbnails/'. $ image. '"/> ';
$ Output. ='







';$ Columns = 5;Foreach ($ images as $ index => $ image ){ ';} ';// Close columnIf ($ index-$ limit_start % $ columns = $ columns-1 ){$ Output. =' ';}}$ Output. ='
'. Get_image_link ($ thumbnail, $ index)
.'
';
Return $ nav. $ output;
}

This table is very simple. It starts to traverse the image list from the index number $ limit_start. Then, the thumbnail of each image is placed, and each five images is taken as a line. When $ limit_start + $ limit_step is reached, the cycle ends.

This table is a visualized representation of the directory list. Therefore, a function is required to list all the images in the directory. The get_file_list () function in Listing 1 returns the list of all images in the/images directory using an indexed array. The following is an example.

Listing 2. get_file_list implementation

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;
}

Note: The get_file_list () function is also used later in this article. It is important that the returned array remains unchanged no matter when this function is called. Because the provided implementation requires Directory Search, it is necessary to ensure that the specified files in the directory do not change, and each time they are sorted alphabetically.1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.