การเขียนโปรแกรมของแต่งละคนมีวิธีที่เขียนแตกต่างกัน แต่วิธีที่ผมจะสอนในวันนี้จะเป็นวิธีที่ผมจะแยกส่วนออกมาให้เป็นระเบียบ ประกอบกับการเขียนโปรแกรมในรูปแบบการเรียก function ซึ่งเป็นการยกระดับการเขียนอีก 1 ระดับทีเดียว

การนำเข้าข้อมูลจากไฟล์ต่างๆ มีฟังชั่นในการนำเข้าข้อมูลอยู่ 4 รูปแบบคือ

  • include วิธีนี้คือการนำเข้าข้อมูลในไฟล์นั้นๆ ซึ่งหากไม่พบ file นั้น ระบบก็จะทำการแจ้งเตือน (Warning)
  • include_once มีความหมายเหมือนกับ include แต่หากว่าเคยนำเข้าแล้ว ระบบก็จะไม่นำเข้าซ้ำ
  • require วิธีนี้คือการนำเข้าข้อมูลในไฟล์นั้นๆ ซึ่งหากไม่พบ file นั้น ระบบก็จะแจ้ง Error และหยุดทำงานทันที
  • require_once มีความหมายเหมือนกับ require แต่หากว่าเคยนำเข้าแล้ว ระบบก็จะไม่นำเข้าซ้ำ

ตัวอย่าง ผมจะเอาตัวอย่างง่ายๆ มาให้ดูก็แล้วกันครับ
แบบแรก เขียนแบบธรรมดาไม่มีการนำเข้ามูลทั้งสิ้น
File team.php

<html>
 <head>
  <title>Inquiry</title>
 </head>
 <body>
  <?
  $conn = mysql_connect("localhost","root","");
  mysql_select_db("football");
  $query = mysql_query("Select id,name from team order by id");
  while($row = mysql_fetch_array($query)){
   $id = $row["id"];
   $name = $row["name"];
  ?>
  <h3>Team <?=$name?></h3>
  <?
   $query2 = mysql_query("select name,position,number from player where team = ".$id." order by number");
   while($row2 = mysql_fetch_array($query2)){
    echo $row2["number"]." ".$row2["name"]." (".$row2["position"].")<br />";
   }
   echo "<hr />";
  }
  ?>
 </body>
</hmtl>

แบบที่ 2 การเขียนแบบแยกส่วน
File connect.php

<?
$conn = mysql_connect("localhost","root","");
mysql_select_db("football");
function excuteQuery($sql){
 $query = mysql_query("Select id,name from team order by id");
 $return = array();
 while($row=mysql_fetch_array($query)) {
  $return[] = $row;
 }
return $return;
}
?>

File team.php

require("connect.php"); //นำเข้าข้อมูลใน connect.php เหมือนกับว่าไฟลนี้มีทุกๆ อย่างเหมือน connect.php
<html>
 <head>
  <title>Inquiry</title>
 </head>
 <body>
  <?
  $team = excuteQuery("Select id,name from team order by id");
  for($i=0; $i<count($team); $i++){
  ?>
  <h3>Team <?=$team[$i]["name"]?></h3>
  <?
   $player = excuteQuery("select name,position,number from player where team = ".$team[$i]["id"]." order by number");
   for($n=0; $n<count($player); $n++){
    echo $player[$n]["number"]." ".$player[$n]["name"]." (".$player[$n]["position"].")<br />";
   }
   echo "<hr />";
  }
  ?>
 </body>
</hmtl>

ซึ่งข้อมูลเบื้องหลังก็จะได้

$conn = mysql_connect("localhost","root","");
mysql_select_db("football");
function excuteQuery($sql){
 $query = mysql_query("Select id,name from team order by id");
 $return = array();
 while($row=mysql_fetch_array($query)) {
  $return[] = $row;
 }
return $return;
}
<html>
 <head>
  <title>Inquiry</title>
 </head>
 <body>
  <?
  $team = excuteQuery("Select id,name from team order by id");
  for($i=0; $i<count($team); $i++){
  ?>
  <h3>Team <?=$team[$i]["name"]?></h3>
  <?
   $player = excuteQuery("select name,position,number from player where team = ".$team[$i]["id"]." order by number");
   for($n=0; $n<count($player); $n++){
    echo $player[$n]["number"]." ".$player[$n]["name"]." (".$player[$n]["position"].")<br />";
   }
   echo "<hr />";
  }
  ?>
 </body>
</hmtl>

Related Posts

Tagged with:  

2 Responses to PHP แยกส่วนการเขียนให้เป็นระเบียบ

  1. MayHong says:

    ดีค่ะ เผื่อใครกำลังศึกษา PHP จะได้เข้าใจ concept เพิ่มขึ้น

    ReplyReply
  2. ขอบคุณมาก ๆครับ โค้ดจะได้มีระเีบียบ ดูง่ายขึ้น

    ReplyReply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>