1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu PHP and MySQL by Example- P8 doc

50 476 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 1,83 MB

Nội dung

3 function get_info(){ $record=array(); 4 $result_set=mysql_query("SELECT * FROM Shippers"); 5 while($row=mysql_fetch_assoc($result_set)){ array_push($record, $row); } 6 return($record); } ?> <br /><br /> <b> <?php 7 $company_records=get_info(); 8 foreach ( $company_records as $key=>$value){ 9 foreach( $value as $field_key=>$val){ print "$field_key => $val<br />"; } print "<hr>"; } ?> </b> </font> </body> </html> Explanation " ?0-!9:9!mysql_connect()!$%&'()*&!*1-&6!%1!+!'*&&-'()*&!(*!(0-!QbGcR! 6-/B-/!+&.!/-(%/&6!+!2)&@!(*!(0-!6-/B-/!6*!(0+(! &*5 !5-!'+&!+''-66!+!.+(+,+6-7 D ?0)6!)6!50-/-!(0-!.+(+,+6-!)6!6-2-'( 7!#!1*1%2+/!.+(+,+6-!'+22 !northwind!)6! *1-& !+&.!+!/-6*%/'-!0+&.2-!)6!/-(%/& !(*!3)B-!+''-66!(*!)(7!#!/-6*%/'-!(>1-! 0*2.6!+!61-')+2!0+&.2-/!(*!(0-!.+(+,+6-!'*&&-'()*&!'/-+( !50-&!(0-!.+(+ , + 6-! '*&&-'()*&!5+6!4+ 7 K ?0-!$%&'()*&=!get_info()=!)6! $)& 7!;(!5)22!/-(/)-B-!+22!(0-!/-'*/.6!$/*4!(0-! Shippers!(+,2-!+&.!/-(%/&!(0-4!(*!(0-!'+22-/7 N ?0-!mysql_query()!$%&'()*&!6-&.6!+!_%-/>!(*!(0-!'%//-&(2>!+'()B-!northwind! .+(+,+6-!+&.!/-(%/&6!+!/-$-/-&'-!(*!(0-!.+(+ ! )(!6-2-'( Y!(0+(!)6=!+22!*$!(0-! /-'*/.6!)&!Shippers7 P ?0-!mysql_fetch_assoc()!$%&'()*&!/-(%/&6!+&!+66*')+()B-!+//+>!(0+(! '*//-61*&.6!(*!(0-!$-('0 !/*5!+&.!4*B-6!(0-!)&(-/&+2!.+(+!1*)&(-/!+0-+.!(*! (0-!&-<(!/*57 L #!6-(!*$!/-'*/.6!$/*4!(0-!Shippers!(+,2-!+/-!/-(%/& !(*!(0-!'+22-/!+6!+&!+//+>! *$!+66*')+()B-!+//+>67 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ] ?0-!get_info()!$%&'()*&!)6!'+22 7!#&!+//+>!*$!/-'*/.6!F+66*')+()B-!+//+>6M!)6! /-(%/& !+&.!+66)3& !(*!$company_list7 ^ ?0-!foreach!2**1!)6!%6 !(*!)(-/+(-!(0/*%30!(0-!@->6!+&.!B+2%-6!*$!(0-!(0/ ! +//+>6=!-+'0!*$!50)'0!'*&(+)&6!+&!+66*')+()B-!+//+>7 S ?0)6!)&&-/!foreach!2**1!/-(/)-B-6!(0-!@->6!+&.!B+2%-6!$/*4!-+'0!*$!(0-! +66*')+()B-!+//+>6!/-(%/& !$/*4!(0-!$%&'()*&=!50)'0!)6!-+'0!/-'*/.!)&!(0-! Shippers!(+,2-7!?0-!*%(1%(!)6!60*5&!)&!8)3%/-!S7"K7 Figure 9.13. A function that returns an associative array. Output from Example 9.10. ! 9.1.4. Using Callback Functions A callback function is one that is not directly invoked in the program; instead it is invoked by another function. What that means is that one function takes another function, called the callback function, as its argument. Callback functions are often used to perform some arbitrary task on each item in a list of items. One function traverses the list, and the other provides the code to represent the task that will be performed on each item. PHP provides a number of array functions that utilize “callbacks.” Some of these functions are preg_replace(), array_filter(), array_map(), and so on. (Callback functions cannot only be simple functions, but also object methods including static class methods. See Chapter 17, “Objects.”) A PHP function is simply passed by its name as a string. You can pass any built-in or user-defined function with the exception of array(), echo(), empty(), eval(), exit(), isset(), list(), print(), and unset(). The array_map() Function The following example uses PHP’s array_map() function to demonstrate the use of a callback function. The array_map() function returns an array containing all the elements of the original array after applying the callback function to each element. The number of parameters that the callback function accepts should match the number of arrays passed to array_map(). Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Format array array_map ( callback callback, array arr1 [, array ] ) ! Example: function square($n){ // User-defined callback function return $n * $n; } $numbers=array(1,4,6,8); $squared_list = array_map("square", "$numbers"); Example 9.11. <?php 1 function salestax($price){ // Callback function $tax = 1.15; 2 return($price * $tax); } 3 $before_prices=array(1.50, 3.55, 4.75, 6.00); 4 // array_map $after_prices=array_map("salestax", $before_prices); print "<b>Before map: "; foreach ($before_prices as $value){ 5 printf("\$%.2f ",$value); } echo "\n<br /><br />"; print "After map: "; foreach ($after_prices as $value){ 6 printf("\$%.2f ",$value); } ?> Explanation " ?0)6!)6!+!%6-/A $)& !'+22,+'@!$%&'()*&7!;(!5)22!,-!'+22 !$*/!-+'0!-2-4-&(=! $price=!)&!+&!+//+>!*$!B+2%-6!(0+(!)(!/-'-)B-6!$/*4!+&*(0-/!$%&'()*&=!'+22 ! array_map()7 D ?0-!B+2%-!*$!-+'0!-2-4-&(!*$!+&!+//+>!5)22!,-!/-(%/& !+$(-/!(0-!6+2-6!(+<!0+6! , &!+112) !(*!)(7 K #&!+//+>!'+22 !$after_prices!)6!/-(%/& !,>!(0-!'+22,+'@!$%&'()*&!+$(-/!)(!0+6! 1-/$*/4 !+!'+2'%2+()*&!*&!-+'0!-2-4-&(!*$!$before_prices7 N ?0-!array_map()!$%&'()*&!+112)-6!+!61-')$)'!(+6@!(*!-+'0!-2-4-&(!*$!+&!+//+>! ,>!%6)&3!+!'+22,+'@!$%&'()*&!(*!1-/$*/4!(0-!(+6@7!;&!(0)6!-<+412-=!(0-! array_map()!$%&'()*&!(+@-6!(5*!+/3%4-&(6T!$salesprice=!(0-!&+4-!*$!(0-! '+22,+'@!$%&'()*&!F1+66 !+6!+!6(/)&3M!(0+(!5)22!,-!'+22 !$*/!-+'0!-2-4-&(!)&!+&! +//+>Y!+&.!$before_prices=!(0-!&+4-!*$!(0-!+//+>!*&!50)'0!(0-!'+22,+'@! $%&'()*&!5)22!1-/$*/4!(0-!6+4-!(+6@!$*/!-+'0!*$!(0-!-2-4-&(67!G)412>=!-+'0! 1/)'-!)&!(0-!2)6(!5)22!0+B-!+!6+2-6!(+<!+112) !(*!)(7!?0-!&-5!+//+>=! $after_prices=!5)22!/-$2-'(!(0-!1/)'-!+$(-/!(0-!6+2-6!(+<!5+6!+ !)&7 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. P ?0-!*/)3)&+2!1/)'-6!+/-!1/)&( 7 L ?0-!&-5!1/)'-6=!+$(-/!(0-!6+2-6!(+<!)6!'+2'%2+( =!+/-!1/)&( 7!G !8)3%/-!S7"N7 ! Figure 9.14. A callback function. Output from Example 9.11. The array_walk_recursive() Function The array_walk_recursive() applies a user-defined callback function to every element of an array and will recurse into nested arrays. Normally the array_walk_recursive() function takes two arguments: the first one the array being walked over, and the second one the value of the key or index of the array. A third, optional argument is an additional value that you can send to the callback function. The functon returns true on success and false on failure. If your callback function needs to be working with the actual values of the array, specify the first parameter of the callback as a reference. Then, any changes made to those elements will be made in the original array itself. Format bool array_walk_recursive ( array &input, callback funcname [, mixed userdata] ) ! Example: $employee=array("Name"=>"Bob", "Title"=>"President"); array_walk_recursive($employee, 'callback_function'); Example 9.12. U* !E)-5T! <html><head><title>Walking Through a Multidimensional Array </title> </head><body> <div align="center"> <h3>Using a Callback Function with a Multidimensional Array</h3> <?php 1 $numbers=array(array(1,2,3,4), array(4,8,10,12), Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. array(20,25,30,35), ); 2 function cube(&$element,$index){ print $index; // prints 012301230123 $element=$element*$element*$element; } ?> <table border='1'><caption><font size='-1'>The <em> array_walk_recursive()</em> function</font></caption> <?php 3 array_walk_recursive($numbers,'cube'); for($i=0; $i < 3; $i++){ echo "<tr bgcolor='999FFF'>"; for($j=0; $j<4; $j++){ echo "<td><b>".$numbers[$i][$j] ; } echo "</td></tr>"; } echo "</table>"; ?> </div> </body> </html> Explanation " $numbers!)6!+!&%4-/)'!+//+>!'*&(+)&)&3!(0/ !+//+>67 D ?0)6!'+22,+'@!$%&'()*&!(+@-6!+!/-$-/-&'-!(*!(0-!+//+>!,-)&3!5+2@ !*B-/!+ 6!)( 6!$)/6( ! +/3%4-&(!+&.!$key=!50)'0!)6!(0-!)& <!*$!-+'0!-2-4-&(!*$!(0-!+//+>7!;(6!$%&'()*&!)6!(*! 5+2@!(0/*%30!(0-!+//+>!'%,)&3!-+'0!*$!)(6!-2-4-&(67 K ?0-!array_walk_recursive()!$%&'()*&!(+@-6!(0-!+//+>!+6!)(6!$)/6(!+/3%4-&(=!+&.!(0-! &+4-!*$!(0-!$%&'()*&=!'cube'=!+6!+!6(/)&3!B+2%-!$*/!)(6!6-'*&.!+/3%4-&(7!?0-!cube()! $%&'()*&!)6!(0-!'+22,+'@!$%&'()*&!(0+(!5)22!,-!+112) !(*!-+'0!-2-4-&(!*$!(0-!(5*A .)4-&6)*&+2!+//+>=!$numbers7!G !8)3%/-!S7"P7 ! ! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Figure 9.15. Using a callback function. Output from Example 9.12. ! 9.1.5. Scope Scope refers to the parts of a program that can see or access a variable; that is, where the variable is visible. Lifetime is how long the variable exists. There are three types of scope for PHP variables: local, global, and static. Local variables are visible within a function and their life ends when the function ends. Global variables are visible to a script, but not normally to a function. They live throughout the run of the script, and die when the script ends. Static variables are local to a function but retain their value from one function call to another. They die when the script ends. Let’s look at some examples to see how scope and lifetime work. Local Scope By default, all variables declared within functions are local to the function; that is, they are not visible outside the function and die when the function exits. In computer jargon, these variables are pushed onto a stack when the function starts and popped off when the function ends. Example 9.13. <?php 1 $friend = "Sam"; // Global variable, visible outside functions 2 function who(){ 3 $friend = "Joe"; // Local variable; disappears when // function ends print "In the function $friend is your local friend.<br />"; } 4 who(); // Function call 5 print "Out of the function, your friend is $friend.<br.>"; ?> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Explanation " $friend!)6!+!32*,+2!B+/)+,2-!)&!(0+(!)(!)6!B)6),2-!(*!(0-!4+)&!6'/)1(=!+&.!5)22!.)6+11-+/! 50-&!(0-!6'/)1(!-&.67!;(!5)22!&*(!,-!B)6),2-!5)(0)&!$%&'()*&67 D ?0-!$%&'()*&!'+22 !who()!)6! $)& 7 K [B-&!(0*%30!(0)6!B+/)+,2-!)6!+26*!'+22 !$friend=!)(!)6!+!.)$$-/-&(!B+/)+,2-7!;(!)6!2*'+2!(*!(0-! $%&'()*&!+&.!5)22!.)6+11-+/!50-&!(0-!$%&'()*&! -&.67! ;$!(0)6!B+/)+,2-!0+.!&*(!, &! $)& =!(0-!*/)3)&+2!$friend!5*%2.!6()22!&*(!,-!B)6),2-!0-/-7 N ?0-!$%&'()*&=!who()=!)6!'+22 7!`&'-!)&!(0-!$%&'()*&!(0-!$friend!B+2%-!Sam!)6!*%(!*$!6'*1-! %&()2!(0-!$%&'()*&!-&.67!Joe!)6!(0-!$/)-&.7 P `%(6) !(0-!$%&'()*&=!32*,+2!B+/)+,2-!$friend!)6!&*5!)&!6'*1-!+&.!Sam!)6!(0-!$/)-&.7!G ! 8)3%/-!S7"L7 ! Figure 9.16. Local scope. Output from Example 9.13. Global Scope From within a function you cannot access variables that were declared outside the function unless you pass the variable to the function as an argument, by reference. If you need to access an external variable within a function without passing it by reference, then you can use the global statement. By placing the global keyword in front of a variable name, the variable that was defined outside the function can now be accessed. Format global variable_name1, variable_name2, variable_name3 ; ! Example: global$salary; Example 9.14. <html><head><title>Function Arguments</title></head> <body bgcolor="lightgreen"><font size="+1" face="arial"> <?php 1 function raise_sal(){ 2 global $salary; 3 $salary *= 1.1; // 10 percent raise } 4 $salary = 50000; 5 raise_sal(); Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ?> 6 Congratulations! Your new salary is $<?= $salary?>.<br />"; </body> </html> Explanation " ?0-!$%&'()*&!raise_sal()!)6! $)& 7 D ?0-!@->5*/.=!global=!+22*56!(0)6!$%&'()*&!(*!0+B-!+''-66!(*!(0-!B+/)+,2-=!$salary=! $)& !*%(6) !(0)6!$%&'()*&7 K ?0-!32*,+2!$salary!)6!,-)&3!4*.)$) !)&!(0-!$%&'()*&7 N ?0-!B+/)+,2-=!$salary=!)6! $)& !*%(6) !(0-!$%&'()*&!+&.!)6!&*(!+B+)2+,2-!(*!(0-! $%&'()*&=!%&2-66!)(!)6!1+66 !,>!/-$-/-&'-!*/!)6!4+ !32*,+2!5)(0)&!(0-!$%&'()*&7 P ?0-!raise_sal()!$%&'()*&!)6!'+22 7 L ?0-!:?QR!*%(1%(!60*56!(0+(!(0-!6+2+/>!5+6!'0+&3 !)&!(0-!$%&'()*&7!G !8)3%/-!S7 "]7 ! Figure 9.17. Global scope. Output from Example 9.14. The $GLOBALS() Array The other way to access a global variable within a function is with the $GLOBALS[] array. It contains all the variables that have global scope in the script (i.e., any variables declared outside of functions). Example 9.15. <html><head><title>Function Arguments</title></head> <body bgcolor="lightgreen"><font size="+1" face="arial"> <?php function raise_sal(){ 1 $GLOBALS['salary'] *= 1.1; } $salary = 50000; raise_sal(); ?> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 2 Congratulations! Your new salary is $<?=$salary?>.<br /> </body> </html> Explanation " ?0-!$_GLOBALS[]!+//+>!3)B-6!(0-!$%&'()*&!+''-66!(*!B+/)+,2-6! $)& !$/*4!*%(6) !(0-! $%&'()*&Y!)&!(0)6!'+6-=!$salary7!?0-!&+4-!*$!(0-!B+/)+,2-!,-'*4-6!(0-!@->!)&!(0-! $_GLOBALS[]!+66*')+()B-!+//+>7!?0)6!-<+412-!)6!-<+'(2>!2)@-![<+412-!S7"N!-<'-1(=!)&!(0+(! -<+412-=!(0-!global!@->5*/.!5+6!%6 !(*!4+@-!(0-!B+/)+,2-!$salary!+B+)2+,2-!(*!(0-! $%&'()*&7 D ?0-!:?QR!*%(1%(!60*56!(0+(!(0-!6+2+/>!5+6!'0+&3 !)&!(0-!$%&'()*&7!G !8)3%/-!S7 "^7 ! Figure 9.18. Using the $GLOBALS array. Output from Example 9.15. Static Variables The variables declared within functions normally disappear when the function ends. They are local to the function. They are created when the function is called and die when it ends. A static variable is local to the function, meaning it is not visible outside of the function, but it does not die when the function ends. Once initialized a static variable will not lose its value between function calls. It “remembers” the value it held from one call to the next. Example 9.16. <?php function trackme(){ 1 static $count=0; 2 $count++; echo "You have been here $count times.\n<br />"; } 3 trackme(); trackme(); trackme(); ?> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Explanation " V>!4+@)&3!(0-!B+/)+,2-!6(+()'=!)(!5)22!/-(+)&!)(6!B+2%-!-B-&!+$(-/!( 0-! $%&'()*&!-<)(67![+'0! ()4-!trackme()!)6!'+22 =!(0-!'*%&(!5)22!,-!)&'/-4-&( !,>!"7!?0-!B+/)+,2-=!$count=!)6! *&2>!)&)()+2)W !(*!W-/*!(0-!$)/6(!()4-!(0-!$%&'()*&!)6!'+22 7 D ?0-!B+2%-!*$!$count!)6!)&'/-4-&( !,>!"7 K ?0-!$%&'()*&!trackme()!)6!'+22 !(0/ !()4-67![+'0!()4-!)(!)6!'+22 =!(0-!B+2%-!*$!(0-! '*%&(-/!)6!)&'/-4-&( !+&.!.)612+> 7!G !8)3%/-!S7"S7 Figure 9.19. Static variables. Output from Example 9.16. Example 9.17. U* !E)-5T! <html><head><title>Function Arguments</title></head> <body bgcolor="lightgreen"><font face="verdana"> <?php 1 function increase_font($size){ 2 static $total=0; // The value of $total will persist // between calls 3 $newfont= $size++ ; 4 $total += $newfont; // Keep a running total ?> 5 <font size='<?=+$newfont?>'>bigger<br /> <?php 6 if ($total > 10){ print "+$total: Too big.<br />"; exit; } } 7 for ($n=0; $n<=10; $n++){ increase_font($n); Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... file, it sends it to PHP to be processed PHP knows how the form was sent and provides special variables to hold both GET and POST data as well as variables containing information about the environment, such as server, browser, and so on PHP automatically converts the names of the form elements into PHP variables and executes any PHP statements, substituting the output within the HTML document (discussed... establishes a TCP/IP connection and sends a request in an HTTP format The server examines the request and responds The first line of the HTTP request might look like this: GET /file .php HTTP/1.1   This line specifies that the method to handle the incoming data is GET, that the file to retrieve is file .php, and that the version of HTTP is 1.1 PHP runs as part of the server and has access to the form data... function described in the PHP manual: http://www .php. net/manual/en/function.ini-set .php With require() the missing file would cause the script to exit, whereas with include(), the program will continue to run You can also use an absolute path such as require('C:\pub\library\file .php' ) or include('/usr/htdocs/file .php' ) Just like require() and include(), the require_once() and include_once() statements,... database, updating a file, sending an e-mail message to the user, setting preferences, and so on The action listed here sends the form date to a PHP script under the document root of the server The method is POST 10.3 PHP and Forms There are number of ways that a PHP script can accept user input As you might recall, in an HTML form, the form elements... enclose the function within PHP tags, because, when a file is included, if the PHP tags are missing, PHP automatically starts processing the included file as an HTML document The only difference between include() and require() is how they deal with failure The include() produces a warning and allows the script to continue executing while require() produces a fatal error For PHP to find the file you want... library file in a PHP script as shown here: Format Code  View:   < ?php require("mylibrary .php" ); // Now functions in the library can be called echo "The average is ", ave(array(11,3,5,7,34)); ?> -< ?php include("mylibrary .php" ); echo "The average is ", ave(array(11,3,5,7,34)); ?> The include() and require() constructs cause a specified file to be included and evaluated... More on PHP Forms 10.1 Introduction We discussed forms briefly in Chapter 3, PHP Quick Start,” and now we are ready to delve into a more thorough discussion about how PHP handles form information The first part of this chapter is a review of how to set up HTML forms, the HTTP (HyperText Transmission Protocol) methods, and how form information is transmitted from the browser to the server and back...  spaces and  dots   in  the  input  device  name, PHP  has  replaced  them  with  the  underscore  to   make  them  valid  variable  names  See  Figure  10.9 Figure 10.9 PHP replaced spaces and dots with an underscore Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark Stripping out Slashes If the magic_quotes_gpc directive is enabled in the php. ini file (and it is by default),... After PHP has finished handling the form data, the server will send back a response header and send the processed information back to the browser A server response looks like this: HTTP/1.1 200 OK Content-type: text/html   When the browser receives the page, it renders the HTML code and then displays the page in the browser 10.2.3 Creating HTML Forms Before getting PHP to...  function  defined  in  another  file?   12 What  is  the  difference  between  require() and  include()?   9.2.2 What’s Next? Now that you have a good handle on functions, it is time to talk about how PHP and HTML work together when producing forms In Chapter 10, “More on PHP Forms,” you will see a number of functions to handle incoming data, most of them user defined Chapter 9 Lab 1 Ask  the  user  how . require('C:publibraryfile .php& apos;) or include('/usr/htdocs/file .php& apos;). Just like require() and include(), the require_once() and include_once(). within PHP tags, because, when a file is included, if the PHP tags are missing, PHP automatically starts processing the included file as an HTML document.

Ngày đăng: 21/01/2014, 09:20

TỪ KHÓA LIÊN QUAN