<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Thai Coding - มาเขียนโปรแกรมกันครับThai Coding - มาเขียนโปรแกรมกันครับ</title>
	<atom:link href="http://www.thaicoding.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thaicoding.net</link>
	<description>We love Programming</description>
	<lastBuildDate>Wed, 22 May 2013 07:14:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>เริ่มเบื้องต้นกับ Node.js</title>
		<link>http://www.thaicoding.net/%e0%b9%80%e0%b8%a3%e0%b8%b4%e0%b9%88%e0%b8%a1%e0%b8%95%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%9a%e0%b8%b7%e0%b9%89%e0%b8%ad%e0%b8%87%e0%b8%81%e0%b8%b1%e0%b8%9a-node-js/</link>
		<comments>http://www.thaicoding.net/%e0%b9%80%e0%b8%a3%e0%b8%b4%e0%b9%88%e0%b8%a1%e0%b8%95%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%9a%e0%b8%b7%e0%b9%89%e0%b8%ad%e0%b8%87%e0%b8%81%e0%b8%b1%e0%b8%9a-node-js/#comments</comments>
		<pubDate>Sun, 19 May 2013 16:03:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[server-side]]></category>
		<category><![CDATA[สอน javascript]]></category>
		<category><![CDATA[เริ่มต้นเบื้องกับ Node.js]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1613</guid>
		<description><![CDATA[Tweet เริ่มเบื้องต้นกับ Node.js Node.js ชื่อนี้ในช่วงหลังๆมักจะได้ยินกันบ่อยๆ node.js นั้นเป็นสิ่งใหม่ที่ถ้า google นั้นคิดค้นซึ่งมันจะทำงานได้นี้ใน chrome นั้นเองซึ่งประโยชน์ของ node.js นั้นส่วนมากเว็บหรือระบบใหญ่จะนำมาประยุกต์ใช้กับ application พวก real time ที่จะมีการตอบสนองทันที ที่มีเหตุการณ์เกิดขึ้น หรือที่เรียกว่า event-driven ซึ่งเจ้า node.js นั้นจะรันโดยอาศัย V8 JavaScript Engine เป็นตัวหลักและการพัฒนา node.js นั้นจะใช้ javascript แบบ server-side มันคงแปลกดีที่ภาษาที่เคยเป็น client &#8211; side มานาน อยู่ๆสามารถมาเป็น server &#8211; side เอาละที่นี้เรามาลองกันเลย สำหรับผมแล้ว node.js ที่ผมลองนั้นจะเป็น version window เพราะผมไม่ได้ใช้ os ตระกูลอื่นซะเท่าไรซึ่งมันก็ support หลาย os นั้นแหละ [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b9%2580%25e0%25b8%25a3%25e0%25b8%25b4%25e0%25b9%2588%25e0%25b8%25a1%25e0%25b8%2595%25e0%25b9%2589%25e0%25b8%2599%25e0%25b9%2580%25e0%25b8%259a%25e0%25b8%25b7%25e0%25b9%2589%25e0%25b8%25ad%25e0%25b8%2587%25e0%25b8%2581%25e0%25b8%25b1%25e0%25b8%259a-node-js%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b9%80%e0%b8%a3%e0%b8%b4%e0%b9%88%e0%b8%a1%e0%b8%95%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%9a%e0%b8%b7%e0%b9%89%e0%b8%ad%e0%b8%87%e0%b8%81%e0%b8%b1%e0%b8%9a-node-js/" data-count="vertical" data-via="" data-lang="de" data-text="เริ่มเบื้องต้นกับ Node.js &raquo; Thai Coding - มาเขียนโ [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p><strong>เริ่ม<strong>เบื้อง</strong>ต้นกับ Node.js</strong></p>
<p>Node.js ชื่อนี้ในช่วงหลังๆมักจะได้ยินกันบ่อยๆ node.js นั้นเป็นสิ่งใหม่ที่ถ้า google นั้นคิดค้นซึ่งมันจะทำงานได้นี้ใน chrome นั้นเองซึ่งประโยชน์ของ node.js นั้นส่วนมากเว็บหรือระบบใหญ่จะนำมาประยุกต์ใช้กับ application พวก real time ที่จะมีการตอบสนองทันที ที่มีเหตุการณ์เกิดขึ้น หรือที่เรียกว่า event-driven ซึ่งเจ้า node.js นั้นจะรันโดยอาศัย V8 JavaScript Engine เป็นตัวหลักและการพัฒนา node.js นั้นจะใช้ javascript แบบ server-side มันคงแปลกดีที่ภาษาที่เคยเป็น client &#8211; side มานาน อยู่ๆสามารถมาเป็น server &#8211; side เอาละที่นี้เรามาลองกันเลย</p>
<p>สำหรับผมแล้ว node.js ที่ผมลองนั้นจะเป็น version window เพราะผมไม่ได้ใช้ os ตระกูลอื่นซะเท่าไรซึ่งมันก็ support หลาย os นั้นแหละ เริ่มแรกก็ไป download ตัว install สำหรับ window ได้ที่ <a href="http://nodejs.org/">node.js</a> เมื่อโหลดเสร็จก็ทำการติดตั้งได้เลย</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/05/Untitled.png"><img class="alignnone size-full wp-image-1616" alt="node.js" src="http://www.thaicoding.net/wp-content/uploads/2013/05/Untitled.png" width="509" height="398" /></a><span id="more-1613"></span></p>
<p>เมื่อติดตั้งเสร็จแล้วก็ให้ทำการเขียนโค้ดตัวอย่างเริ่มต้นกันเลยโดยเขียนโค้ดดังนี้</p>
<pre class="brush: js">

var http = require(&#039;http&#039;);
http.createServer(function (req, res) {
res.writeHead(200, {&#039;Content-Type&#039;: &#039;text/plain&#039;});
res.end(&#039;Thaicoding\n&#039;);
}).listen(6510, &#039;127.0.0.1&#039;);
console.log(&#039;Server running at http://127.0.0.1:6510/&#039;);

</pre>
<p>เมื่อเขียนโค้ดนี้เสร็จแล้วก็ทำการเซฟไว้ที่เราต้องการ ส่วนของผมนั้นเซฟไว้ที่ D:/testnode</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/05/1.png"><img class="alignnone size-full wp-image-1617" alt="node.js" src="http://www.thaicoding.net/wp-content/uploads/2013/05/1.png" width="576" height="162" /></a></p>
<p>จากนั้นก็เปิด command line ขึ้นมาแล้วลองพิมพ์คำสั่ง node &#8211;version ถ้าติดตั้งไม่มีปัญหาก็จะขึ้น version ให้เห็นจากนั้นก็ cd ไปยัง directory ที่เราทำการเซฟโค้ดไว้ แล้วทำการรันโดยใช้คำสั่ง node ตามด้วยชื่อไฟล์ .js</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/05/2.png"><img class="alignnone size-full wp-image-1619" alt="node.js" src="http://www.thaicoding.net/wp-content/uploads/2013/05/2.png" width="587" height="194" /></a></p>
<p>เมื่อรันแล้วมันจะทำการเปิด port 6510 ตามโค้ดที่เขียนไว้ที่นี้เราก็ลอง เปิด web browser โดยมี url ดังนี้ <a href="http://127.0.0.1:6510/">http://127.0.0.1:6510/</a></p>
<p>จะได้ดังรูป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/05/3.png"><img class="alignnone size-full wp-image-1620" alt="node.js" src="http://www.thaicoding.net/wp-content/uploads/2013/05/3.png" width="382" height="110" /></a></p>
<p>จากโค้ดมันจะทำการเปิด port 6510 โดยมีการประกาศตัวแปร http ซึ่งเป็นตัวแปรของโมดูล http นั้นจะทำการ response กับไปเป็น Content-Type: &#8216;text/plain สถานะ 200 เพื่อบอกว่าสถานะทำการส่งข้อมูลสำเร็จ โดยใช้คำสั่ง res.writeHead(200, {&#8216;Content-Type&#8217;: &#8216;text/plain&#8217;});</p>
<p>และทำการส่งข้อมูล string เป็นคำว่า Thaicoding โดยใช้คำสั่ง res.end(&#8216;Thaicoding\n&#8217;); สุดท้ายจะเป็นการบอกว่าให้เจ้าโปรแกรม node ที่เราเขียนขึ้นนั้นเปิด port เพื่อรอรับ request ที่หมายเลข port ใดจากตัวอย่างจะเป็น port 6510 โดยใช้คำสั่ง</p>
<p>.listen(6510, &#8217;127.0.0.1&#8242;); ซึ่ง 127.0.0.1 เป็น ip ของ server ซึ่งก็คือเครื่องเรานั้นเอง จะเห็นว่าการใช้ node.js มันจะเหมือนกับการเขียนโปรแกรมแนว client &#8211; server เพื่อรอรับ request จะ user ซึ่งด้วยความที่ตัวมันเบาเหมาะสำหรับทำโปรแกรมแบบ real time ซึ่งในบางครั้งถ้าทำโปรแกรม real time ที่ใช้ server แบบ apache หรือ iis แล้วใช้ภาษาพวก php, asp.net นั้นมันจะเสียเวลาในการแปลงภาษาหรือประมวลผลของ server เลยทำให้ช้า แต่ node.js นั้นด้วยความที่ไม่ตัดตอนบ้างส่วนที่ซับซ้อนของ application server ออกไปเลยทำให้มันทำงานเร็วมากขึ้น ซึ่งเหมาะสำหรับโปรแกรมเฉพาะทางที่ต้องการทำงานที่เร็วมากขึ้น</p>

	Tags: <a href="http://www.thaicoding.net/tag/javascript-2/" title="javascript" rel="tag">javascript</a>, <a href="http://www.thaicoding.net/tag/node-js/" title="node.js" rel="tag">node.js</a>, <a href="http://www.thaicoding.net/tag/server-side/" title="server-side" rel="tag">server-side</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99-javascript/" title="สอน javascript" rel="tag">สอน javascript</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%a3%e0%b8%b4%e0%b9%88%e0%b8%a1%e0%b8%95%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%9a%e0%b8%b7%e0%b9%89%e0%b8%ad%e0%b8%87%e0%b8%81%e0%b8%b1%e0%b8%9a-node-js/" title="เริ่มต้นเบื้องกับ Node.js" rel="tag">เริ่มต้นเบื้องกับ Node.js</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b9%80%e0%b8%a3%e0%b8%b4%e0%b9%88%e0%b8%a1%e0%b8%95%e0%b9%89%e0%b8%99%e0%b9%80%e0%b8%9a%e0%b8%b7%e0%b9%89%e0%b8%ad%e0%b8%87%e0%b8%81%e0%b8%b1%e0%b8%9a-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การเขียน PHP แบบ OOP ตอนที่ 4 การใช้ autoload เพื่อเรียกใช้งานคลาส</title>
		<link>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-4-%e0%b8%81%e0%b8%b2%e0%b8%a3/</link>
		<comments>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-4-%e0%b8%81%e0%b8%b2%e0%b8%a3/#comments</comments>
		<pubDate>Sun, 21 Apr 2013 09:27:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[autoload]]></category>
		<category><![CDATA[การเขียนแบบ OOP]]></category>
		<category><![CDATA[สอนเขียน php]]></category>
		<category><![CDATA[หัดเขียน php]]></category>
		<category><![CDATA[เขียน php]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1603</guid>
		<description><![CDATA[Tweet ในบทความนี้เรามาตอนกันว่าด้วยในกรณีที่ถ้าสมมุติใน 1 ไฟล์เว็บเพจนั้นมีการเรียกใช้งาน class และสร้าง object ของ class ที่แตกต่างกัน เราก็ต้องทำการ include หรือ require กันเป็นชื่อไฟล์หลายๆบรรทัดตามแต่จำนวนไฟล์คลาส ที่เราใช้งาน เริ่มแรกเพื่อความเข้าใจก็ให้ทำต่อจากตัวอย่างในครั้งก่อน การเขียน PHP แบบ OOP ตอนที่ 3 การสร้าง constructor  ให้ทำการสร้างคลาสขึ้นมาอีกคลาส 1 ชื่อว่า class.Course.inc โดยมีโค้ดการทำงานง่ายๆดังนี้ &#60;?php class Course { private code; private name; function __construct($data = array()) { if(count($data) &#62; 0){ foreach($data as $name =&#62; $value){ $this-&#62;$name = $value; } [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b9%2580%25e0%25b8%2582%25e0%25b8%25b5%25e0%25b8%25a2%25e0%25b8%2599-php-%25e0%25b9%2581%25e0%25b8%259a%25e0%25b8%259a-oop-%25e0%25b8%2595%25e0%25b8%25ad%25e0%25b8%2599%25e0%25b8%2597%25e0%25b8%25b5%25e0%25b9%2588-4-%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-4-%e0%b8%81%e0%b8%b2%e0%b8%a3/" data-count="vertical" data-via="" data-lang="de" data-text="การเขียน PHP แบบ OOP ตอนที่ 4 การใช้ autoload เพื่อ [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p>ในบทความนี้เรามาตอนกันว่าด้วยในกรณีที่ถ้าสมมุติใน 1 ไฟล์เว็บเพจนั้นมีการเรียกใช้งาน class และสร้าง object ของ class ที่แตกต่างกัน เราก็ต้องทำการ include หรือ require กันเป็นชื่อไฟล์หลายๆบรรทัดตามแต่จำนวนไฟล์คลาส ที่เราใช้งาน เริ่มแรกเพื่อความเข้าใจก็ให้ทำต่อจากตัวอย่างในครั้งก่อน <a title="การเขียน PHP แบบ OOP ตอนที่ 3 การสร้าง constructor" href="http://www.thaicoding.net/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99-php-%E0%B9%81%E0%B8%9A%E0%B8%9A-oop-%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-3-%E0%B8%81%E0%B8%B2%E0%B8%A3/"><strong>การเขียน PHP แบบ OOP ตอนที่ 3 การสร้าง constructor </strong></a> ให้ทำการสร้างคลาสขึ้นมาอีกคลาส 1 ชื่อว่า class.Course.inc โดยมีโค้ดการทำงานง่ายๆดังนี้</p>
<pre class="brush: php">

&lt;?php

class Course
{
private code;
private name;

function __construct($data = array())
{
if(count($data) &gt; 0){
foreach($data as $name =&gt; $value){
$this-&gt;$name = $value;
}
}
}

public function display()
{
$display = &#039;Course Code: &#039; . $this-&gt;code;
$display .= &#039;&lt;br/&gt;&#039;;
$display .= &#039;Course Name: &#039; . $this-&gt;name;
return $display;
}
}

</pre>
<p><span id="more-1603"></span></p>
<p>เมื่อเขียนโค้ดของไฟล์ class นี้เสร็จแล้วให้ไปทำการแก้ไขการ include ไฟล์ใน index.php ให้เป็นดังนี้</p>
<pre class="brush: php">

/**
* Define autoloader.
* @param string $class_name
*/
function __autoload($class_name){
include &#039;class.&#039; . $class_name . &#039;.inc&#039;;
}

</pre>
<p>โดยทำการเพิ่ม function __autoload เข้าไปแล้วลบบรรทัด require &#8216;class.Student.inc&#8217;; ออกด้วยนะครับ เมื่อเพื่อฟังก์ชั่น __autoload ให้ทำการเขียนโค้ดลองสร้าง object ของคลาส Course ดังนี้</p>
<pre class="brush: php">

echo &#039;&lt;h2&gt;Test Class Course&lt;/h2&gt;&#039;;
$course = new Course(array(&#039;code&#039;=&gt;&#039;111-000&#039;,&#039;name&#039;=&gt;&#039;Programming&#039;));

echo &#039;&lt;h2&gt;Function Display Course&lt;/h2&gt;&#039;;
echo $course-&gt;display();
</pre>
<p>เมื่อลอง run ไฟล์ index.php ก็จะได้ผลดังรูปครับ</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled4.png"><img class="alignnone size-full wp-image-1608" alt="การเขียน PHP แบบ OOP ตอนที่ 4 การใช้ autoload เพื่อเรียกใช้งานคลาส" src="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled4.png" width="441" height="590" /></a></p>
<p>จะเห็นต่อให้มีการใช้ class กี่ class ก็ไม่ต้องเหนื่อยมานั่ง include หรือ require ไฟล์หลายๆไฟล์แล้ว แค่ใช้ megic method อย่าง __autoload แต่ autoload นั้นมันจะทำการโหลดตามรูปแบบของชื่อไฟล์ที่เรากำหนดสังเกตจากโค้ดตัวอย่าง การทำงานของมันจริงๆคือ</p>
<p>include &#8216;class.&#8217; . $class_name . &#8216;.inc&#8217;;</p>
<p>นั้นคือมันจะทำการ include ชื่อไฟล์ที่มีรูปแบบ class.ชื่อคลาส.inc ทั้งหมดเท่านั้นเอง เพราะฉะนั้นเราก็ต้องปรับเปลี่ยนไปตามการตั้งชื่อไฟล์คลาสที่เราสร้างขึ้นนครับ</p>
<p><a href="http://www.mediafire.com/?r7c2k2k62dlherr">Downlaod Source Code</a></p>

	Tags: <a href="http://www.thaicoding.net/tag/autoload/" title="autoload" rel="tag">autoload</a>, <a href="http://www.thaicoding.net/tag/php/" title="php" rel="tag">php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop/" title="การเขียนแบบ OOP" rel="tag">การเขียนแบบ OOP</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="สอนเขียน php" rel="tag">สอนเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%ab%e0%b8%b1%e0%b8%94%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="หัดเขียน php" rel="tag">หัดเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="เขียน php" rel="tag">เขียน php</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-4-%e0%b8%81%e0%b8%b2%e0%b8%a3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การเขียน PHP แบบ OOP ตอนที่ 3 การสร้าง constructor</title>
		<link>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-3-%e0%b8%81%e0%b8%b2%e0%b8%a3/</link>
		<comments>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-3-%e0%b8%81%e0%b8%b2%e0%b8%a3/#comments</comments>
		<pubDate>Sat, 06 Apr 2013 12:51:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[construtor]]></category>
		<category><![CDATA[การเขียนแบบ OOP]]></category>
		<category><![CDATA[สอนเขียน php]]></category>
		<category><![CDATA[หัดเขียน php]]></category>
		<category><![CDATA[เขียน php]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1592</guid>
		<description><![CDATA[Tweet การเขียน PHP แบบ OOP ตอนที่ 3 การสร้าง constructor เรามาตอนจากครั้งก่อน การเขียน PHP แบบ OOP ตอนที่ 2 ผมแนะนำให้ดาวโหลดโค้ดจากครั้งก่อนด้วยนะครับ เพราะผมจะทำต่อจากครั้งก่อนโดย ครั้งนี้จะเป็นการสร้าง construtor ของ class ซึ่งในหลักการของ OOP นั้นการมี constructor นั้นก็เพื่อกำหนดค่าเริ่มต้นให้ object ของ class ที่สร้างขึ้นเพื่อให้มันสามารถทำงานได้ตามที่ผู้ออกแบบคลาสตั้งใจ โดยใน PHP นั้นมันเป็นภาษาออกแนว script ผมเลยรู้สึกว่า constructor ของ php มันออกจะ dynamic พอสมควรแต่ไม่รู้ว่าจะส่งผลให้ทำงานช้ารึเปล่า โดยเริ่มแรกให้ทำการสร้าง constructor ในไฟล์ class.Student.inc โดยมีโค้ดดังนี้ function __construct($data = array()) { if(count($data) &#62; 0){ [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b9%2580%25e0%25b8%2582%25e0%25b8%25b5%25e0%25b8%25a2%25e0%25b8%2599-php-%25e0%25b9%2581%25e0%25b8%259a%25e0%25b8%259a-oop-%25e0%25b8%2595%25e0%25b8%25ad%25e0%25b8%2599%25e0%25b8%2597%25e0%25b8%25b5%25e0%25b9%2588-3-%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-3-%e0%b8%81%e0%b8%b2%e0%b8%a3/" data-count="vertical" data-via="" data-lang="de" data-text="การเขียน PHP แบบ OOP ตอนที่ 3 การสร้าง constructor &raquo; [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p><strong>การเขียน PHP แบบ OOP ตอนที่ 3 การสร้าง constructor</strong></p>
<p>เรามาตอนจากครั้งก่อน <a title="การเขียน PHP แบบ OOP ตอนที่ 2" href="http://www.thaicoding.net/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99-php-%E0%B9%81%E0%B8%9A%E0%B8%9A-oop-%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88-2/"><strong>การเขียน PHP แบบ OOP ตอนที่ 2</strong></a> ผมแนะนำให้ดาวโหลดโค้ดจากครั้งก่อนด้วยนะครับ เพราะผมจะทำต่อจากครั้งก่อนโดย ครั้งนี้จะเป็นการสร้าง construtor ของ class ซึ่งในหลักการของ OOP นั้นการมี constructor นั้นก็เพื่อกำหนดค่าเริ่มต้นให้ object ของ class ที่สร้างขึ้นเพื่อให้มันสามารถทำงานได้ตามที่ผู้ออกแบบคลาสตั้งใจ โดยใน PHP นั้นมันเป็นภาษาออกแนว script ผมเลยรู้สึกว่า constructor ของ php มันออกจะ dynamic พอสมควรแต่ไม่รู้ว่าจะส่งผลให้ทำงานช้ารึเปล่า</p>
<p>โดยเริ่มแรกให้ทำการสร้าง constructor ในไฟล์ class.Student.inc โดยมีโค้ดดังนี้</p>
<pre class="brush: php">
function __construct($data = array())
{
if(count($data) &gt; 0){
foreach($data as $name =&gt; $value){
$this-&gt;$name = $value;
}
}
}

</pre>
<p><span id="more-1592"></span></p>
<p>ใน php นั้นหากชื่อ method ขึ้นต้นด้วย under scorce 2 ตัวจะเป็นการใช้ magic method คือมันจะเป็นรูปแบบ method dynamic ใน oop ของ php ซึ่งใน construtor นั้นก็ขึ้นต้นด้วย _ 2 ตัวเหมือนกัน</p>
<p>จากนั้นก็ไปแก้ไข้ที่ไฟล์ index.php โดยให้ลบ</p>
<pre class="brush: php">

$student-&gt;setFirstName(&#039;Thaicoding&#039;);
$student-&gt;setLastName(&#039;.Com&#039;);

</pre>
<p>ออก จากนั้นบรรทัดที่ new object นั้นให้ทำการส่ง array ข้อมูลเพื่อทำการกำหนดข้อมูลเริ่มต้นให้ object โดยเปลี่ยนโค้ดเป็นดังนี้</p>
<p>$student = new Student(array(&#8216;firstName&#8217; =&gt;&#8217;Thaicoding&#8217;, &#8216;lastName&#8217; =&gt; &#8216;.Com&#8217;));</p>
<p>จากนั้นลองรันไฟล์ index.php อีกครั้ง</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled3.png"><img class="alignnone size-full wp-image-1596" alt="การเขียน PHP แบบ OOP " src="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled3.png" width="569" height="480" /></a></p>
<p>จะเห็นมา array ที่ส่งไปในตอนใช้คำสั่ง new object นั้นจะส่งไปยัง constructor เพื่อไปกำหนดให้ member ใน class นั้นเอง โดยสังเกตโค้ดใน construtor นั้น</p>
<pre class="brush: php">

if(count($data) &gt; 0){
foreach($data as $name =&gt; $value){
$this-&gt;$name = $value;
}

</pre>
<p>จะเห็นว่าจะมีการเช็คว่ามีข้อมูลส่งมาหรือถ้ามีก็จะวนลูปจนครบข้อมูลทั้งหมดจากนั้นจะแยกเป็นชื่อกับค่าแล้วกำหนดให้ member ตามชื่อ ที่คู่กับค่านั้นเอง</p>
<p>นั้นก็หมายความว่า ข้อมูล array(&#8216;firstName&#8217; =&gt;&#8217;Thaicoding&#8217;, &#8216;lastName&#8217; =&gt; &#8216;.Com&#8217;) จะเป็น &#8216;ชื่อ member&#8217; =&gt; &#8216;ค่าที่ต้องการกำหนด&#8217; ซึ่งจากวิธีการนี้เราสามารถกำหนดค่าเริ่มต้นให้ object ผ่าน constructor ให้ member ทุกตัวใน class ที่มีนั้นเองและหากมันไม่มีชื่อ member ที่เราส่งไปกับ array ข้อมูลมันก็จะไม่ error อีกด้วย ไม่เชื่อก็ลองใส่ array(&#8216;firstName&#8217; =&gt;&#8217;Thaicoding&#8217;, &#8216;lastName&#8217; =&gt; &#8216;.Com&#8217;,'test&#8217; =&gt; &#8217;123&#8242;) ไปให้ construtor ดูครับ</p>
<p><a href="http://www.mediafire.com/?7miysvscbev04n8">Download SourceCode</a></p>

	Tags: <a href="http://www.thaicoding.net/tag/construtor/" title="construtor" rel="tag">construtor</a>, <a href="http://www.thaicoding.net/tag/php/" title="php" rel="tag">php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop/" title="การเขียนแบบ OOP" rel="tag">การเขียนแบบ OOP</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="สอนเขียน php" rel="tag">สอนเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%ab%e0%b8%b1%e0%b8%94%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="หัดเขียน php" rel="tag">หัดเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="เขียน php" rel="tag">เขียน php</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-3-%e0%b8%81%e0%b8%b2%e0%b8%a3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การเขียน PHP แบบ OOP ตอนที่ 2</title>
		<link>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-2/</link>
		<comments>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-2/#comments</comments>
		<pubDate>Tue, 02 Apr 2013 14:47:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[get and set]]></category>
		<category><![CDATA[การเขียนแบบ OOP]]></category>
		<category><![CDATA[สอนเขียน php]]></category>
		<category><![CDATA[หัดเขียน php]]></category>
		<category><![CDATA[เขียน php]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1585</guid>
		<description><![CDATA[Tweet การเขียน PHP แบบ OOP ตอนที่ 2 ในตอนที่แล้ว ภาษา PHP: การเขียนแบบ OOP ตอนที่ 1   เป็นเนื้อหาเกี่ยวกับการสร้าง class และ object เบื้องต้นในครั้งนี้จะพูดถึง การสร้าง method get กับ set โดยเริ่มจากโค้ดจากตอนที่แล้วโดยเปลี่ยนโค้ดดังนี้ &#60;? class Student{ private $firstName; private $lastName; public function display() { $display = &#039;FistName: &#039; . $this-&#62;firstName; $display .= &#039;&#60;br/&#62;&#039;; $display .= &#039;LastName: &#039; . $this-&#62;lastName; return $display; } public [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b9%2580%25e0%25b8%2582%25e0%25b8%25b5%25e0%25b8%25a2%25e0%25b8%2599-php-%25e0%25b9%2581%25e0%25b8%259a%25e0%25b8%259a-oop-%25e0%25b8%2595%25e0%25b8%25ad%25e0%25b8%2599%25e0%25b8%2597%25e0%25b8%25b5%25e0%25b9%2588-2%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-2/" data-count="vertical" data-via="" data-lang="de" data-text="การเขียน PHP แบบ OOP ตอนที่ 2 &raquo; Thai Coding - มาเขียน [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p><strong>การเขียน PHP แบบ OOP ตอนที่ 2</strong></p>
<p>ในตอนที่แล้ว <a title="ภาษา PHP: การเขียนแบบ OOP ตอนที่ 1" href="http://www.thaicoding.net/%E0%B8%A0%E0%B8%B2%E0%B8%A9%E0%B8%B2-php-%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%82%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%81%E0%B8%9A%E0%B8%9A-oop-%E0%B8%95%E0%B8%AD%E0%B8%99%E0%B8%97%E0%B8%B5%E0%B9%88/"><strong>ภาษา PHP: การเขียนแบบ OOP ตอนที่ 1  </strong></a> เป็นเนื้อหาเกี่ยวกับการสร้าง class และ object เบื้องต้นในครั้งนี้จะพูดถึง การสร้าง method get กับ set</p>
<p>โดยเริ่มจากโค้ดจากตอนที่แล้วโดยเปลี่ยนโค้ดดังนี้</p>
<pre class="brush: php">

&lt;?
class Student{

private $firstName;
private $lastName;

public function display()
{
$display = &#039;FistName: &#039; . $this-&gt;firstName;
$display .= &#039;&lt;br/&gt;&#039;;
$display .= &#039;LastName: &#039; . $this-&gt;lastName;
return $display;
}

public function setFirstName($name)
{
$this-&gt;firstName = $name;
}

public function setLastName($name)
{
$this-&gt;lastName = $name;
}

public function getFirstName()
{
return $this-&gt;firstName;
}

public function getLastName()
{
return $this-&gt;lastName;
}
}

</pre>
<p>เมื่อลองรันไฟล์ index.php ที่ทำมาจากตอนก่อนก็จะได้ดังนี้</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled.png"><img class="alignnone size-full wp-image-1587" alt="php oop ตอน 2" src="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled.png" width="553" height="151" /></a></p>
<p>สังเกตว่าโค้ดใน Class Student มีการเปลี่ยน modifier จาก public เป็น private  เพื่อไม่ให้ object ที่สร้างขึ้นไม่สามารถอ้างอิง member fistName และ lastName หรือเปลี่ยนแปลงค่าได้โดยตรงจึงทำการสร้าง method get และ set ขึ้นมาตามโค้ด</p>
<p>เมื่อลองแก้ไฟล์ index.php ตามโค้ดต่อไปนี้</p>
<pre class="brush: php">

&lt;?php
require &#039;class.Student.inc&#039;;
echo &#039;&lt;h2&gt;Test Class Student&lt;/h2&gt;&#039;;
$student = new Student();

echo &#039;&lt;h2&gt;Set Property Class Student&lt;/h2&gt;&#039;;
$student-&gt;setFirstName(&#039;Thaicoding&#039;);
$student-&gt;setLastName(&#039;.Com&#039;);
echo &#039;&lt;pre&gt;&#039; . print_r($student) . &#039;&lt;/pre&gt;&lt;br/&gt;&#039;;

echo &#039;&lt;h2&gt;Function Display&lt;/h2&gt;&#039;;
echo $student-&gt;display();

echo &#039;&lt;h2&gt;Function get FirstName&lt;/h2&gt;&#039;;
echo $student-&gt;getFirstName();

echo &#039;&lt;h2&gt;Function get LastName&lt;/h2&gt;&#039;;
echo $student-&gt;getLastName();
?&gt;

</pre>
<p>เมื่อลองรันไฟล์ index.php ก็จะได้ดังรูป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled1.png"><img class="alignnone size-full wp-image-1589" alt="ภาษา PHP: การเขียนแบบ OOP" src="http://www.thaicoding.net/wp-content/uploads/2013/04/Untitled1.png" width="526" height="480" /></a></p>
<p>สังเกตว่า method setFirstName กับ setLastName จะทำการกำหนดค่า member firstName และ lastName ตามค่าที่ส่งเข้าโดย object จะสามารถทำการเรียกใช้งานให้ดูตัวอย่างในไฟล์ index.php คือ $student-&gt;setFirstName(&#8216;Thaicoding&#8217;);</p>
<p>ส่วน method get นั้นก็แค่ทำการ return ค่า member ที่ต้องการออกมาให้กับ object ที่เรียกใช้งาน ในการทำ method get set นั้นเพื่อป้องกันการเข้าถึง member ของ object ของ class โดยตรงหรือที่เรียกว่า Encapsulation เพื่อให้คนที่เอา class ไปใช้งานนั้นไม่ทำการอ้างอิงถึงจนทำให้เกิดความผิดพลาดของการทำงานของโปรแกรมเกิดขึ้น</p>
<p>ใน php นั้นจะมี magic method ของ _get กับ _set อยู่แต่ในเบื้องต้นนี้ผมเขียนบทความขั้นพื้นฐานก่อนแล้วค่อยหาโอกาสเขียนบทความในรายละเอียดต่อไป</p>
<p><a href="http://www.mediafire.com/?zgt85fxeo02bgwg">Download SourceCode</a></p>

	Tags: <a href="http://www.thaicoding.net/tag/get-and-set/" title="get and set" rel="tag">get and set</a>, <a href="http://www.thaicoding.net/tag/php/" title="php" rel="tag">php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop/" title="การเขียนแบบ OOP" rel="tag">การเขียนแบบ OOP</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="สอนเขียน php" rel="tag">สอนเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%ab%e0%b8%b1%e0%b8%94%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="หัดเขียน php" rel="tag">หัดเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="เขียน php" rel="tag">เขียน php</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php-%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ภาษา PHP: การเขียนแบบ OOP ตอนที่ 1</title>
		<link>http://www.thaicoding.net/%e0%b8%a0%e0%b8%b2%e0%b8%a9%e0%b8%b2-php-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88/</link>
		<comments>http://www.thaicoding.net/%e0%b8%a0%e0%b8%b2%e0%b8%a9%e0%b8%b2-php-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88/#comments</comments>
		<pubDate>Sat, 23 Mar 2013 08:19:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[การเขียนแบบ OOP]]></category>
		<category><![CDATA[สอนเขียน php]]></category>
		<category><![CDATA[หัดเขียน php]]></category>
		<category><![CDATA[เขียน php]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1575</guid>
		<description><![CDATA[Tweet ภาษา PHP: การเขียนแบบ OOP ตอนที่ 1 ในสมัยนี้การเขียนโปรแกรมในรูปแบบ OOP นั้นมีประโยชน์มากและเป็นรูปแบบทั่วไปสำหรับการพัฒนาโปรแกรมในสมัยนี้ เพราะมันสามารถออกแบบมารองรับการทำงานที่สลับสับซ้อนแล้วอีกอย่างหนึ่งคือมันสามารถ reusable code ได้ทำให้การพัฒนาโค้ดได้เร็วขึ้น และในภาษา PHP นั้นก็สามารถรองรับการเขียนโค้ดในรูปแบบ OOP ได้แล้ว โดยวันนี้จะเป็นการเขียนบทความเกี่ยวกับการเขียน PHP ในรูปแบบ OOP เบื้องต้น โดยมีเนื้อหาในตอนที่ 1 ดังนี้ การสร้าง class การสร้าง member , method การสร้าง object เอาละเรามาเริ่มกันเลยโดย ผมจะทำการสร้าง Class ที่เกี่ยวกับข้อมูลนักเรียนง่ายๆๆ ขึ้นมานั้นคือ Class Student โดยในตอนที่ 1 นี้จะมี member ใน class 2 ตัวคือ firstName กับ lastName และมี method [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b8%25a0%25e0%25b8%25b2%25e0%25b8%25a9%25e0%25b8%25b2-php-%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b9%2580%25e0%25b8%2582%25e0%25b8%25b5%25e0%25b8%25a2%25e0%25b8%2599%25e0%25b9%2581%25e0%25b8%259a%25e0%25b8%259a-oop-%25e0%25b8%2595%25e0%25b8%25ad%25e0%25b8%2599%25e0%25b8%2597%25e0%25b8%25b5%25e0%25b9%2588%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b8%a0%e0%b8%b2%e0%b8%a9%e0%b8%b2-php-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88/" data-count="vertical" data-via="" data-lang="de" data-text="ภาษา PHP: การเขียนแบบ OOP ตอนที่ 1 &raquo; Thai Coding - มาเ [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p><strong>ภาษา PHP: การเขียนแบบ OOP ตอนที่ 1</strong></p>
<p>ในสมัยนี้การเขียนโปรแกรมในรูปแบบ OOP นั้นมีประโยชน์มากและเป็นรูปแบบทั่วไปสำหรับการพัฒนาโปรแกรมในสมัยนี้ เพราะมันสามารถออกแบบมารองรับการทำงานที่สลับสับซ้อนแล้วอีกอย่างหนึ่งคือมันสามารถ reusable code ได้ทำให้การพัฒนาโค้ดได้เร็วขึ้น และในภาษา PHP นั้นก็สามารถรองรับการเขียนโค้ดในรูปแบบ OOP ได้แล้ว โดยวันนี้จะเป็นการเขียนบทความเกี่ยวกับการเขียน PHP ในรูปแบบ OOP เบื้องต้น โดยมีเนื้อหาในตอนที่ 1 ดังนี้</p>
<ul>
<li>การสร้าง class</li>
<li>การสร้าง member , method</li>
<li>การสร้าง object</li>
</ul>
<p>เอาละเรามาเริ่มกันเลยโดย ผมจะทำการสร้าง Class ที่เกี่ยวกับข้อมูลนักเรียนง่ายๆๆ ขึ้นมานั้นคือ Class Student โดยในตอนที่ 1 นี้จะมี member ใน class 2 ตัวคือ firstName กับ lastName และมี method 1 method คือ   display() โดยมีโค้ดดังนี้<span id="more-1575"></span></p>
<pre class="brush: php">

&lt;?
class Student{

public $firstName;
public $lastName;

public function display()
{
$display = &#039;FistName: &#039; . $this-&gt;firstName;
$display .= &#039;&lt;br/&gt;&#039;;
$display .= &#039;LastName: &#039; . $this-&gt;lastName;
return $display;
}
}

</pre>
<p>สังเกตว่าในภาษา php นั้นเวลาเขียนเป็น class ส่วนมากเขาจะนิยมไม่มี tag ปิด ?&gt;</p>
<p>ต่อมาเมื่อเราเขียน class เสร็จแล้วเราก็จะมาทำการสร้าง object ของ class Student ขึ้นเพื่อใช้งานมัน โดยที่ผมทำการสร้างไฟล์ index.php ขึ้นมาโดยมีโค้ดดังนี้</p>
<pre class="brush: php">

&lt;?php
require &#039;class.Student.inc&#039;;
echo &#039;&lt;h2&gt;Test Class Student&lt;/h2&gt;&#039;;
$student = new Student();

echo &#039;&lt;h2&gt;Set Property Class Student&lt;/h2&gt;&#039;;
$student-&gt;firstName = &#039;Thaicoding&#039;;
$student-&gt;lastName = &#039;.Com&#039;;
echo &#039;&lt;pre&gt;&#039; . print_r($student) . &#039;&lt;/pre&gt;&lt;br/&gt;&#039;;

echo &#039;&lt;h2&gt;Function Display&lt;/h2&gt;&#039;;
echo $student-&gt;display();
?&gt;

</pre>
<p>และเมื่อลองรันไฟล์ดูก็จะได้ผลลัพธ์ดังรูป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/03/Untitled.png"><img class="alignnone size-full wp-image-1580" alt="php oop" src="http://www.thaicoding.net/wp-content/uploads/2013/03/Untitled.png" width="533" height="348" /></a></p>
<p>จะเห็นว่าเราใช้คำสั่ง new เพื่อเป็นการสร้าง object ใหม่ซึ่งกลายๆกับภาษาอื่นที่รองรับการเขียนโปรแกรมแบบ OOP นั้นเองต่อมาเราได้มีการกำหนดค่าให้ firstName กับ lastName ซึ่งเราสามารถกำหนดค่าได้โดยตรงเพราะ member ทั้ง 2 ตัวนั้นมันเป็นแบบ public นั้นเอง เมื่อกำหนดค่าแล้วในไฟล์ index.php นั้นได้มีการใช้คำสั่ง print_r เพื่อแสดงค่าต่างๆใน object student นั้นสุดท้ายคือการเรียกใช้ method display ที่เราเขียนการทำงานไว้ในไฟล์ class Student ซึ่งในการอ้างอิง member หรือ method ในภาษา PHP นั้นจะใช้เครื่อง -&gt; นั้นเอง</p>
<p><a href="http://www.mediafire.com/?ee54gohwxpb6xw2">Download SourceCode</a></p>

	Tags: <a href="http://www.thaicoding.net/tag/php/" title="php" rel="tag">php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop/" title="การเขียนแบบ OOP" rel="tag">การเขียนแบบ OOP</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="สอนเขียน php" rel="tag">สอนเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%ab%e0%b8%b1%e0%b8%94%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="หัดเขียน php" rel="tag">หัดเขียน php</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="เขียน php" rel="tag">เขียน php</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b8%a0%e0%b8%b2%e0%b8%a9%e0%b8%b2-php-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99%e0%b9%81%e0%b8%9a%e0%b8%9a-oop-%e0%b8%95%e0%b8%ad%e0%b8%99%e0%b8%97%e0%b8%b5%e0%b9%88/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CodeIgniter การลบ index.php และการใช้งาน URI Routing</title>
		<link>http://www.thaicoding.net/codeigniter-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%a5%e0%b8%9a-index-php-%e0%b9%81%e0%b8%a5%e0%b8%b0%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b8%87%e0%b8%b2%e0%b8%99-uri-routing/</link>
		<comments>http://www.thaicoding.net/codeigniter-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%a5%e0%b8%9a-index-php-%e0%b9%81%e0%b8%a5%e0%b8%b0%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b8%87%e0%b8%b2%e0%b8%99-uri-routing/#comments</comments>
		<pubDate>Sun, 17 Feb 2013 09:18:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[CodeIgniter เอา index.php ออก]]></category>
		<category><![CDATA[mod rewrite]]></category>
		<category><![CDATA[URI Routing]]></category>
		<category><![CDATA[ทำ seo]]></category>
		<category><![CDATA[สอนเขียน php]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1558</guid>
		<description><![CDATA[Tweet ในการใช้ CodeIgniter Framework php ในการพัฒนาเว็บนั้นหากไม่มีการ config เพิ่มเติมนั้นมันจะมีการใช้ index.php ใน url ที่เรียกใช้ controller หากใช้งานเป็น web application ทั่วไปมันคงไม่มีปัญหาอะไรแต่หากทำเป็นเว็บไซด์แล้วมันจะมีปัญหาส่งผลต่อกการ index url ของ google หรือการทำ SEO นั้นเองโดยขั้นตอนการเอา index.php ออกมีขั้นตอนดังนี้ โดยเริ่มแรกนั้นผมจะโดย CodeIgniter เริ่มแรกมาโดยไม่ทำการ config ใดๆเลยแล้วนำไปตั้งบน Web Server ซึ่งในเครื่องของผมนั้นจะใช้ Appserv เพราะมันง่ายสุดสำหรับการใช้งาน โดยผมนำไปวางไว้แล้วตั้งชื่อ folder ว่า code เมื่อลองเรียก url ดูโดยใช้ url ที่มี index.php ซึ่ง Controller default นั้นจะเป็น Welcome แต่เมื่อลองเรียก URL ที่ไม่มี index.php [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2Fcodeigniter-%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b8%25a5%25e0%25b8%259a-index-php-%25e0%25b9%2581%25e0%25b8%25a5%25e0%25b8%25b0%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b9%2583%25e0%25b8%258a%25e0%25b9%2589%25e0%25b8%2587%25e0%25b8%25b2%25e0%25b8%2599-uri-routing%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/codeigniter-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%a5%e0%b8%9a-index-php-%e0%b9%81%e0%b8%a5%e0%b8%b0%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b8%87%e0%b8%b2%e0%b8%99-uri-routing/" data-count="vertical" data-via="" data-lang="de" data-text="CodeIgniter การลบ index.php และการใช้งาน URI Routing &raquo; Thai Coding - [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p>ในการใช้ CodeIgniter Framework php ในการพัฒนาเว็บนั้นหากไม่มีการ config เพิ่มเติมนั้นมันจะมีการใช้ index.php ใน url ที่เรียกใช้ controller หากใช้งานเป็น web application ทั่วไปมันคงไม่มีปัญหาอะไรแต่หากทำเป็นเว็บไซด์แล้วมันจะมีปัญหาส่งผลต่อกการ index url ของ google หรือการทำ SEO นั้นเองโดยขั้นตอนการเอา index.php ออกมีขั้นตอนดังนี้ โดยเริ่มแรกนั้นผมจะโดย CodeIgniter เริ่มแรกมาโดยไม่ทำการ config ใดๆเลยแล้วนำไปตั้งบน Web Server ซึ่งในเครื่องของผมนั้นจะใช้ Appserv เพราะมันง่ายสุดสำหรับการใช้งาน โดยผมนำไปวางไว้แล้วตั้งชื่อ folder ว่า code</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/11.png"><img class="alignnone  wp-image-1560" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/11.png" alt="php framework CodeIgniter" width="638" height="194" /></a></p>
<p>เมื่อลองเรียก url ดูโดยใช้ url ที่มี index.php ซึ่ง Controller default นั้นจะเป็น Welcome</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/21.png"><img class="alignnone  wp-image-1561" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/21.png" alt="php framework CodeIgniter" width="645" height="240" /></a></p>
<p><span id="more-1558"></span></p>
<p>แต่เมื่อลองเรียก URL ที่ไม่มี index.php นั้นจะได้ดังรูปคือไม่เจอหน้าเพจที่ต้องการนั้นเอง</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/31.png"><img class="alignnone  wp-image-1562" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/31.png" alt="php framework CodeIgniter" width="524" height="155" /></a></p>
<p>ที่นี้มาถึงขั้นตอนการเอา index.php ออกกัน โดยเริ่มที่การสร้างไฟล์ .htaccess ใน directory นอกสุดของ codeIgniter</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/4.png"><img class="alignnone size-full wp-image-1563" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/4.png" alt="php framework CodeIgniter" width="635" height="217" /></a></p>
<p>โดยภายในต้องกำหนด กฏการ mod rewrite แต่ขอสำคัญต้องแน่ใจว่า Web Server ที่เราใช้งานนั้นต้อง enable mod rewrite ซะก่อน</p>
<p>โดยกำหนดกฏการ mod rewrite เป็นดังนี้</p>
<p>RewriteEngine on<br />
RewriteCond $1 !^(index\.php|resources|images|robots\.txt\.jpg)<br />
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js)$<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteRule ^(.*)$ index.php/$1 [L,QSA]</p>
<p>เมื่อใส่ข้อมูลในไฟล์ .htaccess เสร็จแล้วก็ให้ไปกำหนดไฟล์ config.php ใน directory application\config</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/51.png"><img class="alignnone  wp-image-1564" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/51.png" alt="php framework CodeIgniter" width="625" height="181" /></a></p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/61.png"><img class="alignnone size-full wp-image-1565" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/61.png" alt="php framework CodeIgniter" width="566" height="398" /></a></p>
<p>โดยให้เปลี่ยน $config['index_page'] = &#8216;index.php&#8217; ให้เป็นดังรูป คือเอา index.php ออก จากนั้นก็เปลี่ยน $config['uri_protocol'] เป็น REQUEST_URI จากนั้นก็เซฟแล้วลองไปเรียก URL ที่ไม่มี index.php ดู</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/7.png"><img class="alignnone size-full wp-image-1566" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/7.png" alt="php framework CodeIgniter" width="581" height="225" /></a></p>
<p>สามารถเรียกได้แล้วครับที่นี้ก็มาทำ URI route เพื่อให้สามารถทำให้ URL เว็บของเรามี keyword ที่ต้องการแล้วก็มี parameter ที่เราต้องใช้ในการดึงข้อมูลจากฐานข้อมูลได้ โดยเราสามารถกำหนดได้ในไฟล์ routes.php ใน application\config</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/8.png"><img class="alignnone  wp-image-1567" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/8.png" alt="php framework CodeIgniter" width="598" height="341" /></a></p>
<p>โดยผมกำหนด เพิ่ม $route['products/([a-z]+)/(\d+)&#8217;] = &#8220;main/testroute/$2&#8243;;</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/9.png"><img class="alignnone size-full wp-image-1568" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/9.png" alt="php framework CodeIgniter" width="597" height="56" /></a></p>
<p>ความหมายของมันก็คือถ้ามีการเรียก URL เป็น base_url/products/ตัวอักษร a-z/แล้วก็ตัวเลข มันจะมีการไปเรียกใช้งาน controller main ฟังก์ชัน testroute หรือง่ายๆคือมันจะเปลี่ยนไปเรียก url base_url/main/testroute/ตัวเลขที่ส่งมานั้นเอง ที่นี้ก่อนที่เราจะไปทดสอบนั้นเราก็ต้องไปเขียน function testroute ใน controller main ก่อนโดยผมทำการ copy ไฟล์ welcome.php แล้วเปลี่ยนชื่อ class เป็น Main แล้วไฟล์ก็ชื่อ main.php</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/111.png"><img class="alignnone size-full wp-image-1569" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/111.png" alt="php framework CodeIgniter" width="598" height="221" /></a></p>
<pre class="brush: php">

&lt;?php if ( ! defined(&#039;BASEPATH&#039;)) exit(&#039;No direct script access allowed&#039;);

class Main extends CI_Controller {

/**
* Index Page for this controller.
*
* Maps to the following URL
*         http://example.com/index.php/welcome
*    - or -
*         http://example.com/index.php/welcome/index
*    - or -
* Since this controller is set as the default controller in
* config/routes.php, it&#039;s displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/&lt;method_name&gt;
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this-&gt;load-&gt;view(&#039;welcome_message&#039;);
}

public function testroute()
{
echo &quot;testroute&lt;br/&gt;&quot;;
echo $this-&gt;uri-&gt;segment(3);
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

</pre>
<p>เมื่อลองเรียก URL http://localhost/code/products/xxxx/1</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/12.png"><img class="alignnone  wp-image-1570" title="php framework CodeIgniter" src="http://www.thaicoding.net/wp-content/uploads/2013/02/12.png" alt="php framework CodeIgniter" width="621" height="188" /></a></p>
<p>ซึ่งจากโค้ดใน function testroute ที่เมื่อเรียก url products/xxxx/1 มันจะตรงตามกฏใน route มันก็จะเรียกใช้งาน function testroute ใน controller main ซึ่งจะเห็นว่า xxxx นั้นอาจมีค่าหรือไม่ก็ได้ในการดึงฐานข้อมูลแล้วเรายังสามารถส่งค่า 1 เข้าไปได้เพื่อใช้ในกรณีของ id นั้นเองส่วน xxx นั้นก็จะเป็น Keyword เพื่อการทำ SEO นั้นเอง</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.thaicoding.net/tag/codeigniter/" title="CodeIgniter" rel="tag">CodeIgniter</a>, <a href="http://www.thaicoding.net/tag/codeigniter-%e0%b9%80%e0%b8%ad%e0%b8%b2-index-php-%e0%b8%ad%e0%b8%ad%e0%b8%81/" title="CodeIgniter เอา index.php ออก" rel="tag">CodeIgniter เอา index.php ออก</a>, <a href="http://www.thaicoding.net/tag/mod-rewrite/" title="mod rewrite" rel="tag">mod rewrite</a>, <a href="http://www.thaicoding.net/tag/uri-routing/" title="URI Routing" rel="tag">URI Routing</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%97%e0%b8%b3-seo/" title="ทำ seo" rel="tag">ทำ seo</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-php/" title="สอนเขียน php" rel="tag">สอนเขียน php</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/codeigniter-%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%a5%e0%b8%9a-index-php-%e0%b9%81%e0%b8%a5%e0%b8%b0%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b8%87%e0%b8%b2%e0%b8%99-uri-routing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>เขียน JAVA &#8211; เซฟรูปจาก URL โดยใช้ JAVA</title>
		<link>http://www.thaicoding.net/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-java-%e0%b9%80%e0%b8%8b%e0%b8%9f%e0%b8%a3%e0%b8%b9%e0%b8%9b%e0%b8%88%e0%b8%b2%e0%b8%81-url-%e0%b9%82%e0%b8%94%e0%b8%a2%e0%b9%83%e0%b8%8a%e0%b9%89-java/</link>
		<comments>http://www.thaicoding.net/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-java-%e0%b9%80%e0%b8%8b%e0%b8%9f%e0%b8%a3%e0%b8%b9%e0%b8%9b%e0%b8%88%e0%b8%b2%e0%b8%81-url-%e0%b9%82%e0%b8%94%e0%b8%a2%e0%b9%83%e0%b8%8a%e0%b9%89-java/#comments</comments>
		<pubDate>Sun, 10 Feb 2013 09:37:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[สอน java]]></category>
		<category><![CDATA[สอนเขียน java]]></category>
		<category><![CDATA[เขียน JAVA]]></category>
		<category><![CDATA[เซฟรูปจาก URL]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1540</guid>
		<description><![CDATA[Tweet เขียน JAVA &#8211; เซฟรูปจาก URL โดยใช้ JAVA ในบางครั้งหากเรามี URL ของรูปภาพจำนวนมาก หากเราต้องการเซฟรูปเหล่านั้นมันคงจะเหนื่อยหน่อยเพราะรูปมันเยอะ เราก็ทำการเขียนโปรแกรมเซฟรูปภาพจาก URL หากเรารู้ URL แล้วเราก็แค่ List ใส่ข้อมูลเป็นไฟล์หรือ Database ก็แล้วแต่ จากนั้นก็จะใช้โปรแกรมเล็กๆที่เขียนโดย java ทำการเซฟรูป เพื่อช่วยให้เก็บรูปหรือบันทึกรูปได้เร็วขึ้นนั้นเอง เริ่มแรกผมจะเขียนโปรแกรม java โดยใช้ eclipse เรามาเริ่มกันเลยดีกว่า ขั้นแรกทำการสร้าง Project เลือกเป็น Java Project จากนั้นผมจะตั้งชื่อ Project ว่า saveImageURL เมื่อสร้างโปรเจ็คเสร็จแล้วก็ให้ทำการสร้าง Class หลักโดยกำหนดให้ชื่อเป็น Main เมื่อสร้าง Class เสร็จแล้วก็ให้เริ่มทำการเขียนโค้ดดังต่อไปนี้ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b9%2580%25e0%25b8%2582%25e0%25b8%25b5%25e0%25b8%25a2%25e0%25b8%2599-java-%25e0%25b9%2580%25e0%25b8%258b%25e0%25b8%259f%25e0%25b8%25a3%25e0%25b8%25b9%25e0%25b8%259b%25e0%25b8%2588%25e0%25b8%25b2%25e0%25b8%2581-url-%25e0%25b9%2582%25e0%25b8%2594%25e0%25b8%25a2%25e0%25b9%2583%25e0%25b8%258a%25e0%25b9%2589-java%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-java-%e0%b9%80%e0%b8%8b%e0%b8%9f%e0%b8%a3%e0%b8%b9%e0%b8%9b%e0%b8%88%e0%b8%b2%e0%b8%81-url-%e0%b9%82%e0%b8%94%e0%b8%a2%e0%b9%83%e0%b8%8a%e0%b9%89-java/" data-count="vertical" data-via="" data-lang="de" data-text="เขียน JAVA &#8211; เซฟรูปจาก URL โดยใช้ JAVA &raquo; Thai Coding -  [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p><strong>เขียน JAVA &#8211; เซฟรูปจาก URL โดยใช้ JAVA</strong></p>
<p>ในบางครั้งหากเรามี URL ของรูปภาพจำนวนมาก หากเราต้องการเซฟรูปเหล่านั้นมันคงจะเหนื่อยหน่อยเพราะรูปมันเยอะ เราก็ทำการเขียนโปรแกรมเซฟรูปภาพจาก URL หากเรารู้ URL แล้วเราก็แค่ List ใส่ข้อมูลเป็นไฟล์หรือ Database ก็แล้วแต่ จากนั้นก็จะใช้โปรแกรมเล็กๆที่เขียนโดย java ทำการเซฟรูป เพื่อช่วยให้เก็บรูปหรือบันทึกรูปได้เร็วขึ้นนั้นเอง</p>
<p>เริ่มแรกผมจะเขียนโปรแกรม java โดยใช้ eclipse เรามาเริ่มกันเลยดีกว่า ขั้นแรกทำการสร้าง Project เลือกเป็น Java Project</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/1.png"><img class="alignnone size-full wp-image-1542" title="สอนเขียน java" src="http://www.thaicoding.net/wp-content/uploads/2013/02/1.png" alt="สอนเขียน java" width="665" height="227" /></a><span id="more-1540"></span></p>
<p>จากนั้นผมจะตั้งชื่อ Project ว่า saveImageURL</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/2.png"><img class="alignnone size-full wp-image-1543" title="เขียน java" src="http://www.thaicoding.net/wp-content/uploads/2013/02/2.png" alt="เขียน java" width="533" height="716" /></a></p>
<p>เมื่อสร้างโปรเจ็คเสร็จแล้วก็ให้ทำการสร้าง Class หลักโดยกำหนดให้ชื่อเป็น Main</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/3.png"><img class="alignnone size-full wp-image-1545" title="java สร้าง class" src="http://www.thaicoding.net/wp-content/uploads/2013/02/3.png" alt="java สร้าง class" width="745" height="521" /></a></p>
<p>เมื่อสร้าง Class เสร็จแล้วก็ให้เริ่มทำการเขียนโค้ดดังต่อไปนี้</p>
<pre class="brush: java">

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class Main {

public static void main(String[] arg){
String targetURL = &quot;http://www.thaicoding.net/wp-content/uploads/2013/01/Untitled.png&quot;;
String pathSave = &quot;C:\\&quot;;
try{
URL url = new URL(targetURL);
String filename = url.getFile();
filename = filename.substring( filename.lastIndexOf(&#039;/&#039;)+1, filename.length() );
System.out.println(&quot;Target URL : &quot; + targetURL);
System.out.println(&quot;Save File : &quot; + pathSave+filename);
InputStream is = url.openStream();
OutputStream os;

os = new FileOutputStream(pathSave+filename);

byte[] b = new byte[2048];
int length;

while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}

is.close();
os.close();

}
catch(IOException e){
System.out.println(e.toString());
}
}
}

</pre>
<p>เมื่อเขียนโค้ดเสร็จแล้วก็ให้ทำการกดปุ่นรันเพื่อรันโปรแกรมที่เขียนไป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/77.png"><img class="alignnone size-full wp-image-1547" title="โปรแกรม java" src="http://www.thaicoding.net/wp-content/uploads/2013/02/77.png" alt="โปรแกรม java" width="317" height="139" /></a></p>
<p>เมื่อกดปุ่มรันแล้วจะได้ผลลัพธ์ดังรูป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/5.png"><img class="alignnone size-full wp-image-1548" title="ผลลัพธ์โปรแกรม java" src="http://www.thaicoding.net/wp-content/uploads/2013/02/5.png" alt="ผลลัพธ์โปรแกรม java" width="694" height="260" /></a></p>
<p>จากโค้ดจะเห็นว่าจะทำการเซฟไฟล์ไปที่ C:\ ซึ่งเมื่อเปิดดูที่ Drive C ก็จะได้ไฟล์รูปที่เราต้องการจาก URL แล้วครับ</p>
<p>&nbsp;</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/02/6.png"><img class="alignnone size-full wp-image-1549" title="ภาษา java" src="http://www.thaicoding.net/wp-content/uploads/2013/02/6.png" alt="ภาษา java" width="614" height="76" /></a></p>
<p>จากโค้ดตัวอย่าง จะมีการใช้ตัวแปรแบบ String 2 ตัวในการเก็บค่า URL รูปภาพที่ต้องการกับอีกค่าคือ path ที่เราต้องการเซฟรูปภาพ จากนั้นจะใช้ Object URL เพื่อสร้าง Object ในการดึงข้อมูลรูปภาพ ต่อจากนั้นจะต้องมีการสร้าง Object InputStream กับ OutputStream โดยที่ InputStream  จะเป็นตัวรับข้อมูลแบบ Stream จาก URL ต่อจากนั้นจะใช้ OutputStream  ทำการเขียนข้อมูล Stream เพื่อทำการบันทึกข้อมูลจะกลายเป็นรูปภาพที่เราต้องการโดยในการเขียนข้อมูลของ OutputStream  นั้นจะทำการเขียนเป็นไบต์โดยทำการวนลูป While จนข้อมูลหมด เมื่อทำการ Write จนหมดข้อมูลแล้วสิ่งสำคัญคือการ Close OutputStream  เพื่อทำการยันทึกรูปภาพให้เสร็จสิ้นสมบูรณ์เมื่อ Close OutputStream  แล้วก็ต้องทำการ Close InputStream ด้วยข้อสำคัญอีกอย่างก็คือต้องใส่ try catch เพื่อกันกรณีไม่สร้างดึงข้อมูลรูปภาพจาก URL หรือไม่สามารถบันทึกรูปไฟล์เก็บไว้ได้</p>
<p>&nbsp;</p>
<p><a href="http://www.mediafire.com/?ca6cisyar7nofzq">Download SourceCode</a></p>

	Tags: <a href="http://www.thaicoding.net/tag/eclipse/" title="eclipse" rel="tag">eclipse</a>, <a href="http://www.thaicoding.net/tag/java/" title="JAVA" rel="tag">JAVA</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99-java/" title="สอน java" rel="tag">สอน java</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%aa%e0%b8%ad%e0%b8%99%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-java/" title="สอนเขียน java" rel="tag">สอนเขียน java</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-java/" title="เขียน JAVA" rel="tag">เขียน JAVA</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%8b%e0%b8%9f%e0%b8%a3%e0%b8%b9%e0%b8%9b%e0%b8%88%e0%b8%b2%e0%b8%81-url/" title="เซฟรูปจาก URL" rel="tag">เซฟรูปจาก URL</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-java-%e0%b9%80%e0%b8%8b%e0%b8%9f%e0%b8%a3%e0%b8%b9%e0%b8%9b%e0%b8%88%e0%b8%b2%e0%b8%81-url-%e0%b9%82%e0%b8%94%e0%b8%a2%e0%b9%83%e0%b8%8a%e0%b9%89-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การส่งค่าผ่านระหว่าง Activity กับ Activity (ตอน 2)</title>
		<link>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-8-2/</link>
		<comments>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-8-2/#comments</comments>
		<pubDate>Thu, 24 Jan 2013 15:25:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[Mobile Programming]]></category>
		<category><![CDATA[Activity]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[การส่งค่าผ่านระหว่าง Activity กับ Activity]]></category>
		<category><![CDATA[หัดเขียน android]]></category>
		<category><![CDATA[เขียน android]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1528</guid>
		<description><![CDATA[Tweet จากที่ครั้งก่อน การส่งค่าผ่านระหว่าง Activity กับ Activity (ตอน 1) เป็นการส่งค่าจาก Activity ไปยังอีก Activity คราวนี้จะเป็นการส่งค่ากลับมาหรือส่งผลลัพธ์กลับมายัง Activity เดิมที่ทำการ request ไป หากใครไม่ได้ทำหรืออ่านจากบทความก่อนให้กลับไปอ่านก่อนแล้วทำการดาวโหลดโค้ดเพื่อความต่อเนื่อง ตอนจากครั้งก่อนก็ให้กลับไปที่ MainActivity แล้วไปที่ Source -&#62; Override/Implement Methods แล้วเลือกทำการ implement method ที่มีชื่อว่า onActivityResult เมื่อเลือกทำการ implement จะเห็นว่ามีการส่ง argument มาให้หลายค่าจะประกอบด้วย requestCode คือรหัสที่ทำการ request ไปยัง Activity ที่ 2 ส่วน resultCode นั้นจะเป็นรหัสที่ Activity ที่ 2 ตอบกับมาพร้อมกับค่าที่ใส่ใน putExtra ซึ่งใน onActivityResult นั้นผมจะทำการแสดงข้อความที่ได้รับกลับมาจาก Activity ที่ [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b8%25aa%25e0%25b9%2588%25e0%25b8%2587%25e0%25b8%2584%25e0%25b9%2588%25e0%25b8%25b2%25e0%25b8%259c%25e0%25b9%2588%25e0%25b8%25b2%25e0%25b8%2599%25e0%25b8%25a3%25e0%25b8%25b0%25e0%25b8%25ab%25e0%25b8%25a7%25e0%25b9%2588%25e0%25b8%25b2%25e0%25b8%2587-activity-8-2%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-8-2/" data-count="vertical" data-via="" data-lang="de" data-text="การส่งค่าผ่านระหว่าง Activity กับ Activity (ตอน 2) &#038;raqu [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p>จากที่ครั้งก่อน <a href="http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-%e0%b8%81/">การส่งค่าผ่านระหว่าง Activity กับ Activity (ตอน 1)</a> เป็นการส่งค่าจาก Activity ไปยังอีก Activity คราวนี้จะเป็นการส่งค่ากลับมาหรือส่งผลลัพธ์กลับมายัง Activity เดิมที่ทำการ request ไป หากใครไม่ได้ทำหรืออ่านจากบทความก่อนให้กลับไปอ่านก่อนแล้วทำการดาวโหลดโค้ดเพื่อความต่อเนื่อง</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/Untitled.png"><img class="alignnone size-full wp-image-1529" title="หัดเขียน android" src="http://www.thaicoding.net/wp-content/uploads/2013/01/Untitled.png" alt="หัดเขียน android" width="502" height="424" /></a></p>
<p>ตอนจากครั้งก่อนก็ให้กลับไปที่ MainActivity แล้วไปที่ Source -&gt; Override/Implement Methods แล้วเลือกทำการ implement method ที่มีชื่อว่า onActivityResult<span id="more-1528"></span></p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/13.png"><img class="alignnone size-full wp-image-1530" title="สอน android implement" src="http://www.thaicoding.net/wp-content/uploads/2013/01/13.png" alt="สอน android implement" width="527" height="466" /></a></p>
<p>เมื่อเลือกทำการ implement จะเห็นว่ามีการส่ง argument มาให้หลายค่าจะประกอบด้วย requestCode คือรหัสที่ทำการ request ไปยัง Activity ที่ 2 ส่วน resultCode นั้นจะเป็นรหัสที่ Activity ที่ 2 ตอบกับมาพร้อมกับค่าที่ใส่ใน putExtra</p>
<p>ซึ่งใน onActivityResult นั้นผมจะทำการแสดงข้อความที่ได้รับกลับมาจาก Activity ที่ 2 โดยเขียนโค้ดดังนี้</p>
<pre class="brush: java">

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case REQUEST_CODE:
if(resultCode == RESULT_CODE){
Toast.makeText(this, data.getExtras().getString(&quot;response message&quot;), Toast.LENGTH_LONG).show();
}
break;

}
super.onActivityResult(requestCode, resultCode, data);
}

</pre>
<p>จากนั้นกลับไปที่ ResponseActivity โดยทำการสร้างปุ่มขึ้นมา 1 ปุ่มเพื่อเวลาคลิกจะได้ส่งค่ากลับไปให้ MainActivity นั้นเอง โดยไปสร้างปุ่มที่ไฟล์ response_activity.xml</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/32.png"><img class="alignnone size-full wp-image-1531" title="android สร้างปุ่ม" src="http://www.thaicoding.net/wp-content/uploads/2013/01/32.png" alt="android สร้างปุ่ม" width="532" height="447" /></a></p>
<p>โดยสร้างปุ่มดังรูปโดยกำหนด Text เป็นคำว่าอะไรก็ได้โดยที่นี้ผมกำหนดเป็น Response จะนั้นก็ทำการ แก้ไขไฟล์ xml ของไฟล์ response_activity.xml ว่าเวลาคลิกปุ่มให้ไปเรียก method ที่ชื่อว่า responseAction โดยมีโค้ดบางส่วนดังนี้</p>
<pre class="brush: xml">

&lt;Button
android:id=&quot;@+id/button1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginBottom=&quot;58dp&quot;
android:onClick=&quot;responseAction&quot;
android:text=&quot;Response&quot; /&gt;

</pre>
<p>จากนั้นก็ทำการสร้าง method ที่ชื่อว่า responseAction ตามที่กำหนด Action ให้กับปุ่มก่อนหน้านี้โดยมีโค้ดดังนี้</p>
<pre class="brush: java">

public void responseAction(View view)
{
Intent intent = new Intent();
intent.putExtra(&quot;response message&quot;, &quot;I am fine, thank you&quot;);
setResult(RESULT_CODE, intent);
finish();
}

</pre>
<p>สังเกตว่าจะมีการสร้าง Intent ใหม่เวลาจะสั่งให้อีก Activity ทำงาน เมื่อจะทำการส่งค่าไปหรือกลับจะใช้คำสั่ง putExtra รอบนี้ส่งกลับโดยใช้ชื่อข้อมูลว่า response message และส่งค่าที่เป็น string กลับไปเป็นคำว่า &#8220;I am fine, thank you&#8221; จากนั้นก็ทำการใช้คำสั่ง setResult เพื่อส่งกลับไปยัง Activity เดิมนั้นเองจะสังเกตว่ามีการใช้ค่าคงที RESULT_CODE นั้นเป็นค่าสำหรับตอบกลับไปว่า result code ที่ return กลับไปนั้นคือโค้ดอะไรจากนั้นก็จบการทำงาน</p>
<p>หากใครสั่งเกตและย้อนกลับไปดูว่าฝ่าย MainActivity นั้นรอรับข้อมูลถูกหรือไม่จะมีการ switch case แล้วก็ if อีกชั้น 1 เพื่อกรองค่าที่ส่งกลับมาว่าเกิดจากที่ไหนส่งอะไรกลับมาจะได้แยกถูกเพื่อในกรณัมีการส่งค่าผ่าน Activity หลายกรณีนั้นเอง</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/42.png"><img class="alignnone size-full wp-image-1532" title="android ผลลัพธ์" src="http://www.thaicoding.net/wp-content/uploads/2013/01/42.png" alt="android ผลลัพธ์" width="568" height="518" /></a></p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/52.png"><img class="alignnone size-full wp-image-1533" title="android ผลลัพธ์" src="http://www.thaicoding.net/wp-content/uploads/2013/01/52.png" alt="android ผลลัพธ์" width="568" height="518" /></a></p>
<p>จากผลลัพธ์จะเห็นว่าการส่งกลับข้อมูลสามารถทำได้ครับ</p>
<p><a href="http://www.mediafire.com/?tsab2ytmia6nkc1"><strong>Download SourceCode</strong></a></p>

	Tags: <a href="http://www.thaicoding.net/tag/activity/" title="Activity" rel="tag">Activity</a>, <a href="http://www.thaicoding.net/tag/android/" title="android" rel="tag">android</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-%e0%b8%81/" title="การส่งค่าผ่านระหว่าง Activity กับ Activity" rel="tag">การส่งค่าผ่านระหว่าง Activity กับ Activity</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%ab%e0%b8%b1%e0%b8%94%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-android/" title="หัดเขียน android" rel="tag">หัดเขียน android</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-android/" title="เขียน android" rel="tag">เขียน android</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-8-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การส่งค่าผ่านระหว่าง Activity กับ Activity (ตอน 1)</title>
		<link>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-%e0%b8%81/</link>
		<comments>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-%e0%b8%81/#comments</comments>
		<pubDate>Mon, 21 Jan 2013 16:32:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JAVA]]></category>
		<category><![CDATA[Mobile Programming]]></category>
		<category><![CDATA[Activity]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[การส่งค่าผ่านระหว่าง Activity กับ Activity]]></category>
		<category><![CDATA[หัดเขียน android]]></category>
		<category><![CDATA[เขียน android]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1508</guid>
		<description><![CDATA[Tweet ในการเขียนโปรแกรมบน android นั้นบ้างครั้งเราก็จำเป็นต้องส่งค่าไปมาระหว่าง Activity หรือที่เรียกว่าหน้าจอนั้นแหละซึ่งวันนี้เลยเอามาเขียนเป็นบทความเพื่อใครกำลังหาวิธีอยู่ โดยจะแบ่งออกเป็น 2ตอน ตอนแรกนี้จะเป็นการส่งค่าจาก Activity หลักไปยังอีก Activity โดยจะทำการ start Activity พร้อมส่งข้อความ String ไป เมื่อ Activity หลักนั้นทำการกดปุ่ม โดยเริ่มแรกผมก็ทำการสร้าง Project ใหม่ขึ้นมาโดยมีรายละเอียดตามภาพ ทำการสร้าง Project ใหม่รองรับ 2.2 -4.0 เลือกเป็น Blank Activity เพื่อความสะดวกและรวดเร็ว ทำการตั้งชื่อ activity หลัก จากนั้นให้ไปที่ res ของ Project แล้วไปที่ layout แล้วทำการ new android xml file ตามรูป จากนั้นตั้งชื่อไฟล์พร้อมเลือก layout ที่ต้องการโดยผมเลือกเป็น RelativeLayout &#160; จากนั้นกลับมาแก้ไฟล์ layout [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b8%25aa%25e0%25b9%2588%25e0%25b8%2587%25e0%25b8%2584%25e0%25b9%2588%25e0%25b8%25b2%25e0%25b8%259c%25e0%25b9%2588%25e0%25b8%25b2%25e0%25b8%2599%25e0%25b8%25a3%25e0%25b8%25b0%25e0%25b8%25ab%25e0%25b8%25a7%25e0%25b9%2588%25e0%25b8%25b2%25e0%25b8%2587-activity-%25e0%25b8%2581%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-%e0%b8%81/" data-count="vertical" data-via="" data-lang="de" data-text="การส่งค่าผ่านระหว่าง Activity กับ Activity (ตอน 1) &#038;raqu [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p>ในการเขียนโปรแกรมบน android นั้นบ้างครั้งเราก็จำเป็นต้องส่งค่าไปมาระหว่าง Activity หรือที่เรียกว่าหน้าจอนั้นแหละซึ่งวันนี้เลยเอามาเขียนเป็นบทความเพื่อใครกำลังหาวิธีอยู่ โดยจะแบ่งออกเป็น 2ตอน ตอนแรกนี้จะเป็นการส่งค่าจาก Activity หลักไปยังอีก Activity โดยจะทำการ start Activity พร้อมส่งข้อความ String ไป เมื่อ Activity หลักนั้นทำการกดปุ่ม</p>
<p>โดยเริ่มแรกผมก็ทำการสร้าง Project ใหม่ขึ้นมาโดยมีรายละเอียดตามภาพ</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/12.png"><img class="alignnone size-full wp-image-1511" title="android สร้างโปรเจ็ค" src="http://www.thaicoding.net/wp-content/uploads/2013/01/12.png" alt="android สร้างโปรเจ็ค" width="636" height="543" /></a></p>
<p>ทำการสร้าง Project ใหม่รองรับ 2.2 -4.0<span id="more-1508"></span></p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/21.png"><img class="alignnone size-full wp-image-1512" title="2" src="http://www.thaicoding.net/wp-content/uploads/2013/01/21.png" alt="" width="636" height="590" /></a></p>
<p>เลือกเป็น Blank Activity เพื่อความสะดวกและรวดเร็ว</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/31.png"><img class="alignnone size-full wp-image-1513" title="เขียน android" src="http://www.thaicoding.net/wp-content/uploads/2013/01/31.png" alt="เขียน android" width="636" height="590" /></a></p>
<p>ทำการตั้งชื่อ activity หลัก</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/41.png"><img class="alignnone  wp-image-1515" title="android สร้าง layout activity" src="http://www.thaicoding.net/wp-content/uploads/2013/01/41.png" alt="android สร้าง layout activity" width="647" height="373" /></a></p>
<p>จากนั้นให้ไปที่ res ของ Project แล้วไปที่ layout แล้วทำการ new android xml file ตามรูป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/51.png"><img class="alignnone size-full wp-image-1516" title="android new layout" src="http://www.thaicoding.net/wp-content/uploads/2013/01/51.png" alt="android new layout" width="525" height="563" /></a></p>
<p>จากนั้นตั้งชื่อไฟล์พร้อมเลือก layout ที่ต้องการโดยผมเลือกเป็น RelativeLayout</p>
<p>&nbsp;</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/6.png"><img class="alignnone  wp-image-1517" title="android สร้างปุ่ม" src="http://www.thaicoding.net/wp-content/uploads/2013/01/6.png" alt="android สร้างปุ่ม" width="648" height="347" /></a></p>
<p>จากนั้นกลับมาแก้ไฟล์ layout ของ Activity หลักคือ activity_main.xml โดยทำการสร้างปุ่มขึ้นมา 1 ปุ่มโดยเปลี่ยน Text ให้เป็นคำว่า Request</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/7.png"><img title="สร้าง class" src="http://www.thaicoding.net/wp-content/uploads/2013/01/7.png" alt="สร้าง class" width="451" height="531" /></a></p>
<p>ต่อจากนั้นทำการสร้าง class สำหรับ Activity อีกอัน โดยให้เลือก สืบทอดมาจาก Activity</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/8.png"><img class="alignnone size-full wp-image-1519" title="8" src="http://www.thaicoding.net/wp-content/uploads/2013/01/8.png" alt="" width="521" height="390" /></a></p>
<p>เมื่อสร้างเสร็จให้ทำการ Implement Method onCreate โดยไปที่ Source &#8211; &gt; แล้วเลือกตามรูป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/9.png"><img class="alignnone size-full wp-image-1520" title="implement onCreate" src="http://www.thaicoding.net/wp-content/uploads/2013/01/9.png" alt="implement onCreate" width="527" height="466" /></a></p>
<p>เมื่อทำตามรูปเสร็จแล้วต่อไปก็จะเป็นการเขียน Event เพื่อส่งค่าไปยัง ResponseActivity ครับ โดยให้ไปที่ MainActivity ทำการเขียน method ดังนี้</p>
<pre class="brush: java">

private static final int REQUEST_CODE = 100;

&amp;amp;amp;amp;nbsp;

public void sendRequest(View view)
{
Intent intent = new Intent(this, ResponseActivity.class);
intent.putExtra(&quot;Request Message&quot;, &quot;Hello&quot;);
startActivityForResult(intent,REQUEST_CODE);
}

</pre>
<p>จะเห็นว่ามีการสร้าง Intent ขึ้นมาใหม่พร้อมทั้งระบุว่าให้ ResponseActivity เป็น Activity ที่ต้องการจากนั้นมีการใช้คำสั่ง .putExtra ซึ่งมันต้องการ param  2 ตัวคือชื่อของ message หรือค่าที่ส่งไป อีกตัวจะเป็นค่าที่ส่งไปจะเป็นข้อความหรือตัวเลขก็ได้ จากนั้นทำการรัน ResponseActivity โดยใช้คำสั่ง startActivityForResult เพื่อรอรับผลส่งค่ากลับจาก ResponseActivity ซึ่งจะต่อในครั้งน่า ส่วนอีกค่าที่ส่งไปใน method นี้คือ Request code ที่กำหนดขึ้นเพื่อที่จะเป็นตัวระบุค่าหรือเหตุการณ์ที่ต้องการในการทำงานของโปรแกรม</p>
<p>เมื่อทำเสร็จแล้วให้ไปที่ activity_main.xml  จากนั้นเพื่อ tag onClick เพื่อกำหนดว่าเมื่อมีการ click จะเป็นเรียกใช้ method sendRequest  ซึ่ง param ของ sendRequest นั้นมันบังคับว่าต่องมี View หลังจากทำในส่วนของ MainActivity ไปแล้วต่อไปก็ฝ่าย ResponseActivity  โดยใน ในไฟล์ response_activity.xml นั้นให้ทำการสร้าง TextView ขึ้นเป็นแบบ Large แต่ไม่ต้องกำหนด Text เพื่อที่จะเอามาแสดงข้อความ Request</p>
<pre class="brush: xml">

&lt;TextView
android:id=&quot;@+id/txtRequest&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginTop=&quot;163dp&quot;
android:textAppearance=&quot;?android:attr/textAppearanceLarge&quot; /&gt;
</pre>
<p>ต่อจากนั้นมาทำการเขียนโค้ดเพิ่มใน ResponseActivity.java</p>
<pre class="brush: java">

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.response_activity);
TextView txtRequest = (TextView)findViewById(R.id.txtRequest);
Intent intent = getIntent();

if(intent != null &amp;amp;amp;amp;&amp;amp;amp;amp; intent.getExtras() != null){
String request = intent.getExtras().getString(&quot;Request Message&quot;);
txtRequest.setText(request);
}
}

</pre>
<p>โดยมีการกำหนด layout ให้กับ Activity นี้โดยใช้คำสั่ง setContentView ต่อจากนั้นก็สร้างตัวแปร TextView เพื่อจะใช้ควบคุม TextView ที่สร้างไปก่อนหน้า จากนั้นก็สร้าง Intent จาก Intent ที่ส่งมาจาก Activity ก่อน ต่อจากนั้นก็มีการ check เงื่อนไขเล็กน้อยเพื่อความแน่ใจในการส่งข้อมูลและไม่ทำให้โปรแกรมทำงานผิดพลาด จากนั้นก็มีการดึงค่าจาก key ที่ชื่อว่า &#8220;Request Message&#8221; ที่ส่งมาจาก MainActivity จากนั้นก็เอาไปแสดงใน txtRequest  ก็เป็นอันเสร็จสิ้น</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/10.png"><img class="alignnone size-full wp-image-1521" title="ผลลัพธ์" src="http://www.thaicoding.net/wp-content/uploads/2013/01/10.png" alt="ผลลัพธ์" width="568" height="518" /></a></p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/111.png"><img class="alignnone size-full wp-image-1522" title="ผลลัพธ์" src="http://www.thaicoding.net/wp-content/uploads/2013/01/111.png" alt="ผลลัพธ์" width="568" height="518" /></a></p>
<p>ผลลัพธ์ก๋ได้ประมาณนี้ครับ</p>
<p><strong>สรุป</strong></p>
<p>การส่งค่าข้อมูลไปอีก Acitivity ต้องใช้ putExtra ซึ่งส่งค่าไป 2 ตัวคือชื่อ tag และค่าที่ต้องการส่งสามารถส่งได้หลายขนิดข้อมูล หลังจากนั้นก็ใช้ method startActivityForResult ส่ง Intent ไป พร้อม request code เพื่อที่ฝ่ายรับจะไปแยกว่าได้รับ request code อะไรมา</p>
<p>ส่วนฝั่ง Acitivity รับก็จะใช้    Intent intent = getIntent(); และ intent.getExtras().getString(&#8220;Request Message&#8221;); ถ้ามีการส่งค่า int ก็มี getInt นะครับส่วนฝ่าย ResponseActivity ตอบกลับนั้นขอเป็นตอนหน้านะครับ</p>
<p><a href="http://www.mediafire.com/?8b461kwd77iv35e"><strong>Download SourceCode</strong></a></p>

	Tags: <a href="http://www.thaicoding.net/tag/activity/" title="Activity" rel="tag">Activity</a>, <a href="http://www.thaicoding.net/tag/android/" title="android" rel="tag">android</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-%e0%b8%81/" title="การส่งค่าผ่านระหว่าง Activity กับ Activity" rel="tag">การส่งค่าผ่านระหว่าง Activity กับ Activity</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%ab%e0%b8%b1%e0%b8%94%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-android/" title="หัดเขียน android" rel="tag">หัดเขียน android</a>, <a href="http://www.thaicoding.net/tag/%e0%b9%80%e0%b8%82%e0%b8%b5%e0%b8%a2%e0%b8%99-android/" title="เขียน android" rel="tag">เขียน android</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%aa%e0%b9%88%e0%b8%87%e0%b8%84%e0%b9%88%e0%b8%b2%e0%b8%9c%e0%b9%88%e0%b8%b2%e0%b8%99%e0%b8%a3%e0%b8%b0%e0%b8%ab%e0%b8%a7%e0%b9%88%e0%b8%b2%e0%b8%87-activity-%e0%b8%81/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>การติดตั้ง mongodb บน window</title>
		<link>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%95%e0%b8%b4%e0%b8%94%e0%b8%95%e0%b8%b1%e0%b9%89%e0%b8%87-mongodb-%e0%b8%9a%e0%b8%99-window/</link>
		<comments>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%95%e0%b8%b4%e0%b8%94%e0%b8%95%e0%b8%b1%e0%b9%89%e0%b8%87-mongodb-%e0%b8%9a%e0%b8%99-window/#comments</comments>
		<pubDate>Tue, 08 Jan 2013 15:59:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[NOSQL]]></category>
		<category><![CDATA[การติดตั้ง mongodb]]></category>
		<category><![CDATA[การติดตั้ง mongodb บน window]]></category>
		<category><![CDATA[ฐานข้อมูล]]></category>

		<guid isPermaLink="false">http://www.thaicoding.net/?p=1497</guid>
		<description><![CDATA[Tweet mongodb เป็นระบบฐานข้อมูลแบบที่เรียกว่า NOSQL นั้นก็คือมีการเก็บข้อมูลแต่จะไม่ใช้คำสั่ง SQL นั้นการ query หาข้อมูลนั้นเอง โดยฐานข้อมูลที่เป็นแบบ NOSQL นั้นมีหลายค่ายหลายรูปแบบทั้งเก็บแบบ document, big table และอื่นอีกแต่หากใครคิดจะเริ่มต้นลองใช้ ฐานข้อมูลที่เป็น NOSQL สักตัวผมว่า mongodb เป็นตัวที่น่าลองเล่นดูได้ง่ายนะครับ ส่วนวันนี้ผมจะมาเขียนเกี่ยวกับการติดตั้ง mongodb บน window นั้นเอง เริ่มแรกเราก็ไปทำการดาวโหลดจากเว็บ www.mongodb.org เมื่อโหลดเสร็จให้ทำการแตกไฟล์ เราจะได้ folder ติดตั้งมาอัน 1 จะมีชื่อประมาณ mongodb-win32-x86_64-[version] ให้ทำการเปลี่ยนชื่อเพื่อการง่ายเป็น mongodb นำไปไว้ที่ drive ที่ต้องการจะติดตั้งเช่น drive c, drive d หลังจากนั้นก็ให้ทำการรัน command line ของ window ขึ้นมา จากนั้นก็ใช้คำสั่ง cd เพื่อเข้าไปยัง drive ที่ทำการเก็บ [...]]]></description>
				<content:encoded><![CDATA[<div class="wp_twitter_button" style="float: right; margin-left: 10px;">
					<a href="http://twitter.com/share?counturl=http%3A%2F%2Fwww.thaicoding.net%2F%25e0%25b8%2581%25e0%25b8%25b2%25e0%25b8%25a3%25e0%25b8%2595%25e0%25b8%25b4%25e0%25b8%2594%25e0%25b8%2595%25e0%25b8%25b1%25e0%25b9%2589%25e0%25b8%2587-mongodb-%25e0%25b8%259a%25e0%25b8%2599-window%2F" class="twitter-share-button" data-url="http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%95%e0%b8%b4%e0%b8%94%e0%b8%95%e0%b8%b1%e0%b9%89%e0%b8%87-mongodb-%e0%b8%9a%e0%b8%99-window/" data-count="vertical" data-via="" data-lang="de" data-text="การติดตั้ง mongodb บน window &raquo; Thai Coding - มาเขียนโปรแ [...]">Tweet</a><br />
					<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
				</div>
<p>mongodb เป็นระบบฐานข้อมูลแบบที่เรียกว่า NOSQL นั้นก็คือมีการเก็บข้อมูลแต่จะไม่ใช้คำสั่ง SQL นั้นการ query หาข้อมูลนั้นเอง โดยฐานข้อมูลที่เป็นแบบ NOSQL นั้นมีหลายค่ายหลายรูปแบบทั้งเก็บแบบ document, big table และอื่นอีกแต่หากใครคิดจะเริ่มต้นลองใช้ ฐานข้อมูลที่เป็น NOSQL สักตัวผมว่า mongodb เป็นตัวที่น่าลองเล่นดูได้ง่ายนะครับ ส่วนวันนี้ผมจะมาเขียนเกี่ยวกับการติดตั้ง mongodb บน window นั้นเอง</p>
<p>เริ่มแรกเราก็ไปทำการดาวโหลดจากเว็บ <a href="http://www.mongodb.org/downloads">www.mongodb.org</a> เมื่อโหลดเสร็จให้ทำการแตกไฟล์ เราจะได้ folder ติดตั้งมาอัน 1 จะมีชื่อประมาณ mongodb-win32-x86_64-[version] ให้ทำการเปลี่ยนชื่อเพื่อการง่ายเป็น mongodb นำไปไว้ที่ drive ที่ต้องการจะติดตั้งเช่น drive c, drive d หลังจากนั้นก็ให้ทำการรัน command line ของ window ขึ้นมา จากนั้นก็ใช้คำสั่ง cd เพื่อเข้าไปยัง drive ที่ทำการเก็บ folder mongodb เหมือนของผมเก็บที่ drive d เลยใช้คำสั่งดังรูป</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/11.png"><img class="alignnone size-full wp-image-1500" title="การติดตั้ง mongodb" src="http://www.thaicoding.net/wp-content/uploads/2013/01/11.png" alt="การติดตั้ง mongodb" width="279" height="80" /></a></p>
<p>จากนั้นก็ใช้คำสั่ง cd เข้าไปยัง folder mongodb แล้วก็เข้าไปให้ถึง folder ภายในที่ชื่อว่า bin<span id="more-1497"></span></p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/2.png"><img class="alignnone size-full wp-image-1501" title="mongodb" src="http://www.thaicoding.net/wp-content/uploads/2013/01/2.png" alt="mongodb" width="369" height="86" /></a></p>
<p>หลังจากนั้น mongodb นั้นต้องการ folder ที่เก็บไฟล์ข้อมูลเพราะตัว mongodb นั้นจะอาศัยหลักการในการใช้ไฟล์มาใช้ในการเก็บข้อมูลเพื่อให้ง่ายต่อการเปลี่ยนตัวอุปกรณ์เก็บข้อมูล</p>
<p>จากหน้าจอเมื่อกี้ก็ให้พิมพ์ mongod.exe &#8211;dbpath d:\test\mongodb\data</p>
<p>หลักคำสั่ง &#8211;dbpath นั้นเราสามารถเปลี่ยนได้ตามต้องการเพื่อเก็บไฟล์ข้อมูล</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/3.png"><img class="alignnone  wp-image-1502" title="การติดตั้ง mongodb บน window" src="http://www.thaicoding.net/wp-content/uploads/2013/01/3.png" alt="การติดตั้ง mongodb บน window" width="686" height="226" /></a></p>
<p>รูปตัวอย่างหลังการใช้งานจะเห็นมามีไฟล์เกิดขึ้นและใหญ่ขึ้นเรื่อยๆ</p>
<p>หลังจากนั้นก็ใช้คำสั่ง mongod.exe</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/4.png"><img class="alignnone size-full wp-image-1503" title="การติดตั้ง mongodb บน window" src="http://www.thaicoding.net/wp-content/uploads/2013/01/4.png" alt="การติดตั้ง mongodb บน window" width="677" height="342" /></a></p>
<p>หลังจากนั้นก็ให้เปิดหน้าต่าง command line แล้วใช้ cd เข้าไปยัง folder bin ใน mongodb แล้วใช้คำสั่ง mongo.exe เพื่อเชื่อต่อฐานข้อมูลที่ start ไปก่อนหน้านี้ครับ</p>
<p><a href="http://www.thaicoding.net/wp-content/uploads/2013/01/5.png"><img class="alignnone size-full wp-image-1504" title="mongodb" src="http://www.thaicoding.net/wp-content/uploads/2013/01/5.png" alt="mongodb" width="545" height="216" /></a></p>
<p>ส่วนวิธีการใช้งานแล้วก็คำสั่งนั้นไว้ให้ผมศึกษาเข้าใจมากกว่านี้นะครับจะมาเขียนบทความใหม่</p>

	Tags: <a href="http://www.thaicoding.net/tag/nosql/" title="NOSQL" rel="tag">NOSQL</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%95%e0%b8%b4%e0%b8%94%e0%b8%95%e0%b8%b1%e0%b9%89%e0%b8%87-mongodb/" title="การติดตั้ง mongodb" rel="tag">การติดตั้ง mongodb</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%95%e0%b8%b4%e0%b8%94%e0%b8%95%e0%b8%b1%e0%b9%89%e0%b8%87-mongodb-%e0%b8%9a%e0%b8%99-window/" title="การติดตั้ง mongodb บน window" rel="tag">การติดตั้ง mongodb บน window</a>, <a href="http://www.thaicoding.net/tag/%e0%b8%90%e0%b8%b2%e0%b8%99%e0%b8%82%e0%b9%89%e0%b8%ad%e0%b8%a1%e0%b8%b9%e0%b8%a5/" title="ฐานข้อมูล" rel="tag">ฐานข้อมูล</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.thaicoding.net/%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%95%e0%b8%b4%e0%b8%94%e0%b8%95%e0%b8%b1%e0%b9%89%e0%b8%87-mongodb-%e0%b8%9a%e0%b8%99-window/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
