<?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>Php LifeStyle :My Php and My LifeStyle &#187; No MySQL</title>
	<atom:link href="http://www.phplifestyle.com/tag/no-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phplifestyle.com</link>
	<description>PHP Security and Optimization</description>
	<lastBuildDate>Mon, 05 Apr 2010 22:22:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>แก้ปัญหาการย้ายสิ่งของ ด้วย PHP</title>
		<link>http://www.phplifestyle.com/79/%e0%b9%81%e0%b8%81%e0%b9%89%e0%b8%9b%e0%b8%b1%e0%b8%8d%e0%b8%ab%e0%b8%b2%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%a2%e0%b9%89%e0%b8%b2%e0%b8%a2%e0%b8%aa%e0%b8%b4%e0%b9%88%e0%b8%87%e0%b8%82%e0%b8%ad%e0%b8%87/</link>
		<comments>http://www.phplifestyle.com/79/%e0%b9%81%e0%b8%81%e0%b9%89%e0%b8%9b%e0%b8%b1%e0%b8%8d%e0%b8%ab%e0%b8%b2%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%a2%e0%b9%89%e0%b8%b2%e0%b8%a2%e0%b8%aa%e0%b8%b4%e0%b9%88%e0%b8%87%e0%b8%82%e0%b8%ad%e0%b8%87/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 19:35:27 +0000</pubDate>
		<dc:creator>tumaji</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[No MySQL]]></category>

		<guid isPermaLink="false">http://www.phplifestyle.com/?p=79</guid>
		<description><![CDATA[พอดีว่าผมไปเจอโจทย์ในการแก้ปัญหาด้วย PHP &#038; MySQL โดยโจทย์มีดังนี้

เกี่ยวกับเรื่อง ฐานข้อมูล กับ ตาราง
ตอนนี้มีตารางข้อมูล ที่ได้มาโดยการ select
itemlist_id    item name    quantity   capacity    user_id     slot use
 5                  ข้าวโพด     [...]]]></description>
			<content:encoded><![CDATA[<p>พอดีว่าผมไปเจอโจทย์ในการแก้ปัญหาด้วย PHP &#038; MySQL โดยโจทย์มีดังนี้</p>
<p><code><br />
เกี่ยวกับเรื่อง ฐานข้อมูล กับ ตาราง<br />
ตอนนี้มีตารางข้อมูล ที่ได้มาโดยการ select</p>
<p>itemlist_id    item name    quantity   capacity    user_id     slot use<br />
 5                  ข้าวโพด             2            15            1             1<br />
 32                   ขี้ไก่               9            50             1             1<br />
 76             ปลากระโทงแทง       6             5             1             2<br />
 79                 ปลาฉลาม          2             1             1             2<br />
 168                  เกลือ              1            50            1             1<br />
*quantity คือ จำนวนของฐานของมูลขอลแต่ละ item name ได้มาจากการ count<br />
  capacity คือ จำนวนความจุ ที่ตั้งใจจะนำไปแบ่งแต่ละcell<br />
  slot use = ceil(capacity/quantity)</p>
<p>โดยจะนำข้อมูลทั้งหมดไปใส่ในตารางที่ผ่านการ loop<br />
สิ่งที่ตั้งใจทำก็คือ จะแบ่งช่องของสิ่งของเหล่านี้ โดยให้ค่า quantity ไม่เกิน capacity ในแต่ละช่อง<br />
ถ้าสำเร็จผลที่ได้รับคือ</p>
<p>ข้าวโพด2 ขี้ไก่9 ปลาฯ5 ปลาฯ1 ปลาฉลาม1 ปลาฉลาม1 เกลือ1<br />
</code><br />
พอดีว่าผมขี้เกียจสร้าง database เพื่อแก้ปัญหาเลยขอประยุกต์ด้วย Array สำหรับใครที่ต้องการจะเอาไปใช้กับ database อื่นๆก็ดัดแปลงดูแล้วกันครับ</p>
<pre name=code class=php>
< ?
$data[0] = array('itemlist_id'=>5,'item_name'=>'ข้าวโพด','quantity'=>2,'capacity'=>15,'user_id'=>1,'slot_use'=>1) ;
$data[1] = array('itemlist_id'=>32,'item_name'=>'ขี้ไก่','quantity'=>9,'capacity'=>50,'user_id'=>1,'slot_use'=>1) ;
$data[2] = array('itemlist_id'=>76,'item_name'=>'ปลากระโทงแทง','quantity'=>6,'capacity'=>5,'user_id'=>1,'slot_use'=>2) ;
$data[3] = array('itemlist_id'=>79,'item_name'=>'ปลาฉลาม','quantity'=>2,'capacity'=>1,'user_id'=>1,'slot_use'=>2) ;
$data[4] = array('itemlist_id'=>168,'item_name'=>'เกลือ','quantity'=>1,'capacity'=>50,'user_id'=>1,'slot_use'=>1) ;

if(is_array($data))
{
	// read from data I
    foreach($data as $row)
    {
		while($row['quantity'] > 0)
		{
			$quantity = ($row['quantity'] > $row['capacity'] ? $row['capacity'] : $row['quantity']  )  ;
			$row['quantity'] -=  $quantity ;
			// insert to data II
			$data2[] = array('itemlist_id'=>$row['itemlist_id'],'item_name'=>$row['item_name'],'quantity'=>$quantity,'capacity'=>$row['capacity'],'user_id'=>$row['user_id'],'slot_use'=>1) ;
			echo  'itemlist_id=>',$row['itemlist_id'],', item_name=>',$row['item_name'],', quantity=>',$quantity,', capacity=>',$row['capacity'],', user_id=>',$row['user_id'],', slot_use=>',1 , "";
		}
    }
}

?>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.phplifestyle.com/79/%e0%b9%81%e0%b8%81%e0%b9%89%e0%b8%9b%e0%b8%b1%e0%b8%8d%e0%b8%ab%e0%b8%b2%e0%b8%81%e0%b8%b2%e0%b8%a3%e0%b8%a2%e0%b9%89%e0%b8%b2%e0%b8%a2%e0%b8%aa%e0%b8%b4%e0%b9%88%e0%b8%87%e0%b8%82%e0%b8%ad%e0%b8%87/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
