"Front-end reading"--the design skills of notes Web World with PHP and MySQL

Source: Internet
Author: User
Tags one table php and mysql

Second, design skills

programming habits vary from person to person, here only to provide some experience, can be consulted.

1. Use include to modularize your program code

The include function basically says, "It's like reading another file (HTML or PHP program) and inserting it in the include." By using this feature moderately, you can modularize the program code and make the program less verbose.

    • Simplifying HTML modifications with include

With a typical Web page: links on the left and text on the right.

    1. Question: If a page has 10 pages in this section, if you want to change a hyperlink, you must change 10 files (of course, now editors such as HomeSite can do once, if you use Dreamweaver or FrontPage, not to mention)
    2. Solution: Using PHP, we can separate the parts of the hyperlink, and all the places used to include in.
// file leftlink.phtml<p><a href = "link1.html" >link 1</a><p><a href = "link2.html" > Link 2</a><p><a href = "link3.html" >link 3</a><p><a href = "link4.html" >Link 4</ A><p><a href = "link5.html" >link 5</a>
// index.phtml<div align= "center" ><table width= "80%" border= "1" >   <tr>          <td Width= "14%" align= "center" >                  <!--Left hyperlinks--                  <?  Include "leftlink.phtml";? >          </td>           <td width= "86%" valign= "Top" >                   <p> This is a simple example that illustrates the include ().                    <p> The hyperlink on the left can also be buttons.           </td>   </tr></table></div>, .....

This way , when you want to change the HTML code of the link section, just change the leftlink.phtml. This method is usually used in the " repeating characteristics " of the layout, quite convenient.

    • Using the Include modular function
    1.   if you write a Web page with Notepad, you'll get a copy of the JavaScript functions, such as a function to change the graph, and every page you use. In PHP, you may also have some of your own functions, such as processing Chinese, check the ID card size, etc., in many places will be used repeatedly. In these cases, the use of include will be much easier.
    2. In other words, include can also insert PHP program fragments, such as a simple e-mail processing program:
 <?  email-rewrite function  function  Rewrite_email ( $str  ) {  $str  = str_replace  (" "," ",  $str ); //         $str  = strtolower  (  $str ); //        change to lowercase  return  ( $str ); //  returns  } ?  

Each time you use this function:

<?  include "Email_rewrite.phtml"; // remember to include first  $email = rewrite_emal ($email);? >

development of a long time, nature will accumulate some custom functions, according to the characteristics of the same nature placed in a folder, will slowly play the role of "function library", quite convenient.

2. Use <form> as a bridge for interaction

Web page download to the browser, basically a bunch of HTML code, completely in the local end of the line, so there should be " interactive " effect, in addition to the Mouse over change diagram, change the colour, or use JavaScript to cut off another screen , the most direct and effective and flexible way is to "send some information back to the server side", let the server side of the program interpretation, processing, response. to achieve this goal, just use <form> on the line.

<form> for PHP, it's equivalent to passing some parameters between pages. (Of course, the transfer of parameters in addition to the form, you can directly specify such as test.phtml?123 or use cookies)

The most common example: the "previous page/Next page" of a query result (such as a search engine), as long as the page is delivered by using form, like this:

 // index.phtml   here is the result of the third page of the query ...  <form anction= "search.phtml" method= "POST" > <!-- Use the hidden field to tell the server the current number of pages --<input type= "Hidden" name= "Cur_page" value= "3" > <input type= "Submit" name= "Showprev" value= "on One page "></form><form action=" search.phtml "method=" POST "> <input type=" Hidden "name=" Cur_page "value=" 3 "> <input type=" Submit "name=" Shownext "value=" Next "></FORM> 
</body> pre>
 // search.phtml Read fragment  <? if  (isset  ( $showprev ): //  show previous   $showpage  =  $cur _page  -1... else  if  (isset  ( $shownext ): //  show next   $showpage  =  $cur _page  +1... end   IIf; ?  

Using the parameters of the transmission, the feeling of the Web page immediately different, such as the interactive nature of the procurement page, step by step to ask the consumer opinion, finally find the most suitable products, is the most common example.

3. If ... else do Process Control

<form> and other ways can be passed between different pages of the parameters, of course, can also pass to their own ! This is a process control that needs to be used later.

// index.phtmlif(empty($keyname)):?><!-- Name not entered, display Form--><form action= "index.phtml" method= "POST" > Please enter name:<input type= "Text" Name= "KeyName" ><input type= "Submit" name= "Submit" value= "go!" ></form><? else:    echo$keyname"; endif;? ></body>

This pattern, in the CGI programming also often see, appropriate use can streamline many interactive processes, as the process control, of course, too many layers, the program will be more complex.

4, if necessary, replace the database operation with files

The PHP used with the database is very useful, but sometimes the amount of data is small, the chance of change is not big, the use of files to save data is more efficient . As for the way the file is read, you can use include or fgets.

For example: to achieve the date () output of the week (0~6) and the Chinese "Monday ..." conversion, you can use a database, create a table, and simultaneously build two fields, such as the following table:

Weekid Weekname
0 "Sunday"
1 "Monday"
2 "Tuesday"
3 "Wednesday"
4 "Thursday"
5 "Friday"
6 "Saturday"

Then in each place to use, open the database link (mysql_pconnect ()), send out QueryString, read the results, display. But it's a little overkill, we just have to write another file:

// weekname.phtml<?     $weekname Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");? >

Places to use:

<?  include "Weekname.phtml";   Echo "Today is." $weekname [Date("W")];? >

5. The choice between time and space

This means that when planning the tables structure of a database, it should be considered: what is the impact on storage space, program complexity, and so on, if more than one field is used or if more than one table is used to place data?

For example: a very simple communication thin, if need to save 3 things: Name, telephone, service unit, the simplest way to solve with a table.

Name Phone Service Units
Wang Xiaojun 010-22334455 Microsoft
Chen Cannon 010-3344567 Pharmacy
Promise Jian 010-7654321 Winery

can also be resolved with two tables, such as the following two tables:

number service units
1 Microsoft
2 pharmacy
name phone unit number
Wang Xiaojun 01-22334455 1
Chen Cannon 09-3344567 2
Xu Zhangjian 03-7654321 2

Can see, with two tables, in the amount of data large (such as 10,000) and repeated more (such as pharmacies have 5,000, Microsoft has 3,000, hehe!) ), the use of hard disk space will be more savings . In turn, the amount of data is small, the difference between each big, so there is no need to spend more of those brains, directly in exchange for space for time.

a similar situation is quite important. : Please remember that the Web is an open space, visitors at any time from all directions, if everyone needs to do very complex calculations, produce the same results, you should consider how to use hard disk space, simplify each visit , the more visitors, the principle of the more important .

NOTE: Reprint please indicate the source

"Front-end reading"--the design skills of notes Web World with PHP and MySQL

Related Article

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.