<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title>Angel's Blog</title>
		<link>https://angelcool.net/sphpblog/blog_index.php</link>
		<description><![CDATA[No Footer]]></description>
		<copyright>Copyright 2026, Angel</copyright>
		<managingEditor>Angel</managingEditor>
		<language>en-US</language>
		<generator>SPHPBLOG 0.7.0</generator>
		<item>
			<title>Websocket example with PHP</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry170729-021842</link>
			<description><![CDATA[<pre><br />[acool@localhost websocket-fun]$<br />[acool@localhost websocket-fun]$ # start web server<br />[acool@localhost websocket-fun]$ php -S localhost:8080<br />PHP 5.6.29 Development Server started at Fri Jul 28 22:21:15 2017<br />Listening on <a href="http://localhost:8080" >http://localhost:8080</a><br />Document root is /home/acool/websocket-fun<br />Press Ctrl-C to quit.</pre><br /><br /><pre>[acool@localhost websocket-fun]$ <br />[acool@localhost websocket-fun]$ # start websocket application<br />[acool@localhost websocket-fun]$ php -q server.php </pre><br /><br />Open two browser windows go to localhost:8080 and start chatting with yourself hehe<br /><br />Window 1<br /><img src="http://angelcool.net/assets/chat1_2017-07-28.png" width="622" height="579" alt="" /><br /><br /><br />Window 2<br /><img src="http://angelcool.net/assets/chat2_2017-07-28.png" width="629" height="580" alt="" /><br /><br /><br />server.php<br /><pre>&lt;?php<br />$host = &#039;localhost&#039;; //host<br />$port = &#039;9000&#039;; //port<br />$null = NULL; //null var<br /><br />//Create TCP/IP sream socket<br />$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);<br />//reuseable port<br />socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);<br /><br />//bind socket to specified host<br />socket_bind($socket, 0, $port);<br /><br />//listen to port<br />socket_listen($socket);<br /><br />//create &amp; add listning socket to the list<br />$clients = array($socket);<br /><br />//start endless loop, so that our script doesn&#039;t stop<br />while (true)<br />{<br />	//manage multipal connections<br />	$changed = $clients;<br />	//returns the socket resources in $changed array<br />	socket_select($changed, $null, $null, 0, 10);<br />	<br />	//check for new socket<br />	if (in_array($socket, $changed))<br />	{<br />		$socket_new = socket_accept($socket); //accpet new socket<br />		$clients[] = $socket_new; //add socket to client array<br />		<br />		$header = socket_read($socket_new, 1024); //read data sent by the socket<br />		perform_handshaking($header, $socket_new, $host, $port); //perform websocket handshake<br />		<br />		socket_getpeername($socket_new, $ip); //get ip address of connected socket<br />		$response = mask(json_encode(array(&#039;type&#039;=&gt;&#039;system&#039;, &#039;message&#039;=&gt;$ip.&#039; connected&#039;))); //prepare json data<br />		send_message($response); //notify all users about new connection<br />		<br />		//make room for new socket<br />		$found_socket = array_search($socket, $changed);<br />		unset($changed[$found_socket]);<br />	}<br />	<br />	//loop through all connected sockets<br />	foreach ($changed as $changed_socket)<br />	{<br />		<br />		//check for any incomming data<br />		while(socket_recv($changed_socket, $buf, 1024, 0) &gt;= 1)<br />		{<br />			$received_text = unmask($buf); //unmask data<br />			$tst_msg       = json_decode($received_text); //json decode <br />			$user_name     = $tst_msg-&gt;name; //sender name<br />			$user_message  = $tst_msg-&gt;message; //message text<br />			$user_color    = $tst_msg-&gt;color; //color<br />			<br />			//prepare data to be sent to client<br />			$response_text = mask(json_encode(array(&#039;type&#039;=&gt;&#039;usermsg&#039;, &#039;name&#039;=&gt;$user_name, &#039;message&#039;=&gt;$user_message, &#039;color&#039;=&gt;$user_color)));<br />			send_message($response_text); //send data<br />			break 2; //exist this loop<br />		}<br />		<br />		$buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ);<br />		if ($buf === false)<br />		{ // check disconnected client<br />			// remove client for $clients array<br />			$found_socket = array_search($changed_socket, $clients);<br />			socket_getpeername($changed_socket, $ip);<br />			unset($clients[$found_socket]);<br />			<br />			//notify all users about disconnected connection<br />			$response = mask(json_encode(array(&#039;type&#039;=&gt;&#039;system&#039;, &#039;message&#039;=&gt;$ip.&#039; disconnected&#039;)));<br />			send_message($response);<br />		}<br />	}<br />}<br />// close the listening socket<br />socket_close($socket);<br /><br />function send_message($msg)<br />{<br />	global $clients;<br />	foreach($clients as $changed_socket)<br />	{<br />		@socket_write($changed_socket,$msg,strlen($msg));<br />	}<br />	return true;<br />}<br /><br /><br />//Unmask incoming framed message<br />function unmask($text)<br />{<br />	$length = ord($text[1]) &amp; 127;<br />	if($length == 126) {<br />		$masks = substr($text, 4, 4);<br />		$data = substr($text, 8);<br />	}<br />	elseif($length == 127) {<br />		$masks = substr($text, 10, 4);<br />		$data = substr($text, 14);<br />	}<br />	else {<br />		$masks = substr($text, 2, 4);<br />		$data = substr($text, 6);<br />	}<br />	$text = &quot;&quot;;<br />	for ($i = 0; $i &lt; strlen($data); ++$i) {<br />		$text .= $data[$i] ^ $masks[$i%4];<br />	}<br />	return $text;<br />}<br /><br />//Encode message for transfer to client.<br />function mask($text)<br />{<br />	$b1 = 0x80 | (0x1 &amp; 0x0f);<br />	$length = strlen($text);<br />	<br />	if($length &lt;= 125)<br />		$header = pack(&#039;CC&#039;, $b1, $length);<br />	elseif($length &gt; 125 &amp;&amp; $length &lt; 65536)<br />		$header = pack(&#039;CCn&#039;, $b1, 126, $length);<br />	elseif($length &gt;= 65536)<br />		$header = pack(&#039;CCNN&#039;, $b1, 127, $length);<br />	return $header.$text;<br />}<br /><br />//handshake new client.<br />function perform_handshaking($receved_header,$client_conn, $host, $port)<br />{<br />	$headers = array();<br />	$lines = preg_split(&quot;/\r\n/&quot;, $receved_header);<br />	foreach($lines as $line)<br />	{<br />		$line = chop($line);<br />		if(preg_match(&#039;/\A(\S+): (.*)\z/&#039;, $line, $matches))<br />		{<br />			$headers[$matches[1]] = $matches[2];<br />		}<br />	}<br /><br />	$secKey = $headers[&#039;Sec-WebSocket-Key&#039;];<br />	$secAccept = base64_encode(pack(&#039;H*&#039;, sha1($secKey . &#039;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&#039;)));<br />	//hand shaking header<br />	$upgrade  = &quot;HTTP/1.1 101 Web Socket Protocol Handshake\r\n&quot; .<br />	&quot;Upgrade: websocket\r\n&quot; .<br />	&quot;Connection: Upgrade\r\n&quot; .<br />	&quot;WebSocket-Origin: $host\r\n&quot; .<br />//	&quot;WebSocket-Location: ws://$host:$port/demo/shout.php\r\n&quot;.<br />	&quot;Sec-WebSocket-Accept:$secAccept\r\n\r\n&quot;;<br />	socket_write($client_conn,$upgrade,strlen($upgrade));<br />}</pre><br /><br />index.php<br /><pre>&lt;!DOCTYPE html&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;<br />&lt;style type=&quot;text/css&quot;&gt;<br /><br />.panel{<br /><br />	<br />margin-right: 3px;<br />}<br /><br />.button {<br />    background-color: #4CAF50;<br />    border: none;<br />    color: white;<br />	margin-right: 30%;   <br />	margin-left: 30%;<br />    text-decoration: none;<br />    display: block;<br />    font-size: 16px;<br />    cursor: pointer;<br />	width:30%;<br />    height:40px;<br />	margin-top: 5px;<br />	 <br />}<br />input[type=text]{<br />		width:100%;<br />		margin-top:5px;<br />		<br />	}<br /><br /><br />.chat_wrapper {<br />	width: 70%;<br />	height:472px;<br />	margin-right: auto;<br />	margin-left: auto;<br />	background: #3B5998;<br />	border: 1px solid #999999;<br />	padding: 10px;<br />	font: 14px &#039;lucida grande&#039;,tahoma,verdana,arial,sans-serif;<br />}<br />.chat_wrapper .message_box {<br />	background: #F7F7F7;<br />	height:350px;<br />		overflow: auto;<br />	padding: 10px 10px 20px 10px;<br />	border: 1px solid #999999;<br />}<br />.chat_wrapper  input{<br />	//padding: 2px 2px 2px 5px;<br />}<br />.system_msg{color: #BDBDBD;font-style: italic;}<br />.user_name{font-weight:bold;}<br />.user_message{color: #88B6E0;}<br /><br />@media only screen and (max-width: 720px) {<br />    /* For mobile phones: */<br />    .chat_wrapper {<br />        width: 95%;<br />	height: 40%;<br />	}<br />    <br /><br />	.button{ width:100%;<br />	margin-right:auto;   <br />	margin-left:auto;<br />	height:40px;}<br />	<br />	<br />	<br />	<br />	<br />				<br />}<br /><br />&lt;/style&gt;<br />&lt;/head&gt;<br />&lt;body&gt;	<br />&lt;?php <br />$colours = array(&#039;007AFF&#039;,&#039;FF7000&#039;,&#039;FF7000&#039;,&#039;15E25F&#039;,&#039;CFC700&#039;,&#039;CFC700&#039;,&#039;CF1100&#039;,&#039;CF00BE&#039;,&#039;F00&#039;);<br />$user_colour = array_rand($colours);<br />?&gt;<br /><br /><br />&lt;script src=&quot;jquery-3.1.1.js&quot;&gt;&lt;/script&gt;<br /><br /><br />&lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt;  <br />$(document).ready(function(){<br />	//create a new WebSocket object.<br />	var wsUri = &quot;ws://localhost:9000/demo/server.php&quot;; 	<br />	websocket = new WebSocket(wsUri); <br />	<br />	websocket.onopen = function(ev) { // connection is open <br />		$(&#039;#message_box&#039;).append(&quot;&lt;div class=\&quot;system_msg\&quot;&gt;Connected!&lt;/div&gt;&quot;); //notify user<br />	}<br /><br />	$(&#039;#send-btn&#039;).click(function(){ //use clicks message send button	<br />		var mymessage = $(&#039;#message&#039;).val(); //get message text<br />		var myname = $(&#039;#name&#039;).val(); //get user name<br />		<br />		if(myname == &quot;&quot;){ //empty name?<br />			alert(&quot;Enter your Name please!&quot;);<br />			return;<br />		}<br />		if(mymessage == &quot;&quot;){ //emtpy message?<br />			alert(&quot;Enter Some message Please!&quot;);<br />			return;<br />		}<br />		document.getElementById(&quot;name&quot;).style.visibility = &quot;hidden&quot;;<br />		<br />		var objDiv = document.getElementById(&quot;message_box&quot;);<br />		objDiv.scrollTop = objDiv.scrollHeight;<br />		//prepare json data<br />		var msg = {<br />		message: mymessage,<br />		name: myname,<br />		color : &#039;&lt;?php echo $colours[$user_colour]; ?&gt;&#039;<br />		};<br />		//convert and send data to server<br />		websocket.send(JSON.stringify(msg));<br />	});<br />	<br />	//#### Message received from server?<br />	websocket.onmessage = function(ev) {<br />		var msg = JSON.parse(ev.data); //PHP sends Json data<br />		var type = msg.type; //message type<br />		var umsg = msg.message; //message text<br />		var uname = msg.name; //user name<br />		var ucolor = msg.color; //color<br /><br />		if(type == &#039;usermsg&#039;) <br />		{<br />			$(&#039;#message_box&#039;).append(&quot;&lt;div&gt;&lt;span class=\&quot;user_name\&quot; style=\&quot;color:#&quot;+ucolor+&quot;\&quot;&gt;&quot;+uname+&quot;&lt;/span&gt; : &lt;span class=\&quot;user_message\&quot;&gt;&quot;+umsg+&quot;&lt;/span&gt;&lt;/div&gt;&quot;);<br />		}<br />		if(type == &#039;system&#039;)<br />		{<br />			$(&#039;#message_box&#039;).append(&quot;&lt;div class=\&quot;system_msg\&quot;&gt;&quot;+umsg+&quot;&lt;/div&gt;&quot;);<br />		}<br />		<br />		$(&#039;#message&#039;).val(&#039;&#039;); //reset text<br />		<br />		var objDiv = document.getElementById(&quot;message_box&quot;);<br />		objDiv.scrollTop = objDiv.scrollHeight;<br />	};<br />	<br />	websocket.onerror	= function(ev){$(&#039;#message_box&#039;).append(&quot;&lt;div class=\&quot;system_error\&quot;&gt;Error Occurred - &quot;+ev.data+&quot;&lt;/div&gt;&quot;);}; <br />	websocket.onclose 	= function(ev){$(&#039;#message_box&#039;).append(&quot;&lt;div class=\&quot;system_msg\&quot;&gt;Connection Closed&lt;/div&gt;&quot;);}; <br />});<br /><br /><br /><br /><br />&lt;/script&gt;<br />&lt;div class=&quot;chat_wrapper&quot;&gt;<br />&lt;div class=&quot;message_box&quot; id=&quot;message_box&quot;&gt;&lt;/div&gt;<br />&lt;div class=&quot;panel&quot;&gt;<br />&lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; placeholder=&quot;Your Name&quot; maxlength=&quot;15&quot; /&gt;<br /><br />&lt;input type=&quot;text&quot; name=&quot;message&quot; id=&quot;message&quot; placeholder=&quot;Message&quot; maxlength=&quot;80&quot; <br />onkeydown = &quot;if (event.keyCode == 13)document.getElementById(&#039;send-btn&#039;).click()&quot;  /&gt;<br /><br /><br /><br /><br /><br />&lt;/div&gt;<br /><br />&lt;button id=&quot;send-btn&quot; class=button&gt;Send&lt;/button&gt;<br /><br />&lt;/div&gt;<br /><br />&lt;/body&gt;<br />&lt;/html&gt;</pre><br /><br />Thanks to sanwebe.com]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry170729-021842</guid>
			<author>Angel</author>
			<pubDate>Sat, 29 Jul 2017 02:18:42 GMT</pubDate>
		</item>
		<item>
			<title>PHP - Detecting palindromes example - not production grade.</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry161114-213251</link>
			<description><![CDATA[<pre>&lt;?php<br /><br />function isPalindrome($string=null)<br />{<br />  if($string === null)<br />    return 0;<br />  <br />  if(is_numeric($string))<br />    return 0;<br />  <br />  $string = preg_replace(&#039;/\s+/&#039;, &#039;&#039;, $string);<br />  <br />  $reverse = strrev($string);<br />  <br />  if($reverse == $string)<br />    return 1;<br />  else<br />    return 0;<br /><br />}<br /><br />echo isPalindrome(&#039;lion oil&#039;);<br /></pre>]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry161114-213251</guid>
			<author>Angel</author>
			<pubDate>Mon, 14 Nov 2016 21:32:51 GMT</pubDate>
		</item>
		<item>
			<title>PHP: Poor man&#039;s alternative to array_chunk</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry160220-010111</link>
			<description><![CDATA[Useful when having to insert a large set of data in chunks to a databse:<br /><pre>&lt;?php<br />$numbers=array (<br />  &#039;1&#039;,<br />  &#039;2&#039;,<br />  &#039;3&#039;,<br />  &#039;4&#039;,<br />  &#039;5&#039;,<br />  &#039;6&#039;,<br />  &#039;7&#039;,<br />  &#039;8&#039;,<br />  &#039;9&#039;,<br />  &#039;10&#039;,<br />  &#039;11&#039;,<br />);<br /><br />$output = array();<br /><br />foreach($numbers as $i=&gt;$number)<br />{<br />    $output[]= $number;<br /><br />    if(($i+1) % 3  == 0 || $i==count($numbers)-1)//last key<br />    {<br />        var_dump($output);<br />        $output=array();<br />    }<br />}</pre><br />The above outputs:<br /><pre>array(3) {<br />  [0]=&gt;<br />  string(1) &quot;1&quot;<br />  [1]=&gt;<br />  string(1) &quot;2&quot;<br />  [2]=&gt;<br />  string(1) &quot;3&quot;<br />}<br />array(3) {<br />  [0]=&gt;<br />  string(1) &quot;4&quot;<br />  [1]=&gt;<br />  string(1) &quot;5&quot;<br />  [2]=&gt;<br />  string(1) &quot;6&quot;<br />}<br />array(3) {<br />  [0]=&gt;<br />  string(1) &quot;7&quot;<br />  [1]=&gt;<br />  string(1) &quot;8&quot;<br />  [2]=&gt;<br />  string(1) &quot;9&quot;<br />}<br />array(2) {<br />  [0]=&gt;<br />  string(2) &quot;10&quot;<br />  [1]=&gt;<br />  string(2) &quot;11&quot;<br />}<br /></pre><br /><a href="http://stackoverflow.com/questions/35492759/using-phps-modulus-to-dump-dataset-every-nth-iteration" >http://stackoverflow.com/questions/3549 ... -iteration</a><br /><br />Thank you very much for your time !!!!!!]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry160220-010111</guid>
			<author>Angel</author>
			<pubDate>Sat, 20 Feb 2016 01:01:11 GMT</pubDate>
		</item>
		<item>
			<title>Properly escaping a double quote in CSV</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry150506-205935</link>
			<description><![CDATA[Amazing trick. This is one of the first thing to try if your CSV spreadsheet is not rendering how you expect.<br /><br />Original Question:<br />======================================================<br />I have a line like this in my CSV:<br /><br />&quot;Samsung U600 24&quot;&quot;,&quot;10000003409&quot;,&quot;1&quot;,&quot;10000003427&quot;<br /><br />Quote next to 24 is used to express inches, while the quote just next to that quote closes the field. I&#039;m reading the line with fgetcsv but the parser makes a mistake and reads the value as:<br /><br />Samsung U600 24&quot;,10000003409&quot;<br /><br />I tried putting a backslash before the inches quote, but then I just get a backslash in the name:<br /><br />Samsung U600 24\&quot;<br /><br />Is there a way to properly escape this in the CSV, so that the value would be Samsung U600 24&quot; , or do I have to regex it in the processor?<br />======================================================<br /><br /><br />Answer:<br />======================================================<br />Use 2 quotes:<br /><br />&quot;Samsung U600 24&quot;&quot;&quot;<br />======================================================<br /><br /><a href="http://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv" >http://stackoverflow.com/questions/1780 ... ote-in-csv</a>]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry150506-205935</guid>
			<author>Angel</author>
			<pubDate>Wed, 06 May 2015 20:59:35 GMT</pubDate>
		</item>
		<item>
			<title>Mimicking Ajax Call with PHP</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry141021-100859</link>
			<description><![CDATA[<pre> /*********************************************************<br /> |<br /> | Creates Ajax-like request<br /> |<br /> *********************************************************/<br /> function sendRequest($params,$url)<br /> {<br />  $postdata = http_build_query($params);<br />  $opts = array(&#039;http&#039; =&gt;<br />      array(<br />          &#039;method&#039;  =&gt; &#039;POST&#039;,<br />          &#039;header&#039;  =&gt; &quot;Content-type: application/x-www-form-urlencoded \r\n&quot;.<br />                       &quot;X-Requested-With: XMLHttpRequest \r\n&quot;,<br />          &#039;content&#039; =&gt; $postdata<br />      )<br />  );<br />  $context  = stream_context_create($opts);<br />  return json_decode(<br />              file_get_contents($url.time(), false, $context)<br />          );<br /> }<br /><br />   $params=array(<br />           &#039;email&#039; =&gt; &#039;me[at]example.com&#039;,<br />           &#039;choCountry&#039; =&gt; &#039;Mexico&#039;,<br />           &#039;zip&#039; =&gt; &#039;91744&#039;,<br />       );<br />   $url= &#039;http://www.barney-example.com/subscribe&#039;; <br />   $data = sendRequest($params,$url); <br /><br /> </pre>]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry141021-100859</guid>
			<author>Angel</author>
			<pubDate>Tue, 21 Oct 2014 17:08:59 GMT</pubDate>
		</item>
		<item>
			<title>Mimicking MySQL LIMIT with PHP array_slice</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry140115-171651</link>
			<description><![CDATA[<pre><br />//SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15<br /><br />$input = array(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;10&quot;,&quot;11&quot;,&quot;12&quot;,&quot;13&quot;,&quot;14&quot;,&quot;15&quot;,&quot;16&quot;,&quot;17&quot;,&quot;18&quot;,&quot;19&quot;);<br />print_r(array_slice($input,5,10));<br /></pre><br /><br />Output:<br /><pre>Array<br />(<br />    [0] =&gt; 6<br />    [1] =&gt; 7<br />    [2] =&gt; 8<br />    [3] =&gt; 9<br />    [4] =&gt; 10<br />    [5] =&gt; 11<br />    [6] =&gt; 12<br />    [7] =&gt; 13<br />    [8] =&gt; 14<br />    [9] =&gt; 15<br />)</pre><br /><br />Complete Pagination Example:<br /><pre>function PaginateArray($input, $page, $show_per_page) {<br /><br />  $page = $page &lt; 1 ? 1 : $page;<br /><br />  $start = ($page - 1) * ($show_per_page);<br />  $offset = $show_per_page;<br /><br />  $outArray = array_slice($input, $start, $offset);<br /><br />  var_export($outArray);<br /> }<br /><br />  $input =   array(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;,&quot;6&quot;,&quot;7&quot;,&quot;8&quot;,&quot;9&quot;,&quot;10&quot;,&quot;11&quot;,&quot;12&quot;,&quot;13&quot;,&quot;14&quot;,&quot;15&quot;,&quot;16&quot;,&quot;17&quot;,&quot;18&quot;,&quot;19&quot;,&quot;20&quot;);<br /><br />  //page 1<br />  PaginateArray($input, 1, 3);//inputs: array, page, records per page<br /> /*<br />  array (<br />   0 =&gt; &#039;1&#039;,<br />   1 =&gt; &#039;2&#039;,<br />   2 =&gt; &#039;3&#039;,<br /> )<br />  */<br /><br />  //page 2<br />  PaginateArray($input, 2, 3);//inputs: array, page, records per page<br /> /*<br /> array (<br />  0 =&gt; &#039;4&#039;,<br />  1 =&gt; &#039;5&#039;,<br />  2 =&gt; &#039;6&#039;,<br />)<br /> */</pre><br />]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry140115-171651</guid>
			<author>Angel</author>
			<pubDate>Thu, 16 Jan 2014 00:16:51 GMT</pubDate>
		</item>
		<item>
			<title>Download/Extract HTML table data as CSV with JQuery and PHP</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry131226-173136</link>
			<description><![CDATA[//using table2csv plugin<br /><pre><br />//HTML:<br />&lt;form action=&quot;getCSV.php&quot; method =&quot;post&quot; &gt; <br />&lt;input type=&quot;hidden&quot; name=&quot;csv_text&quot; id=&quot;csv_text&quot;&gt;<br />&lt;input type=&quot;submit&quot; value=&quot;Get CSV File&quot; <br />   onclick=&quot;getCSVData()&quot;<br />&lt;/form&gt;<br />&lt;script&gt;<br />function getCSVData(){<br />  var csv_value=$(&#039;#tableID&#039;).table2CSV({delivery:&#039;value&#039;});<br />  $(&quot;#csv_text&quot;).val(csv_value);  <br />}<br />&lt;/script&gt;<br /><br />//php<br />&lt;?php<br />header(&quot;Content-type: application/octet-stream&quot;);<br />header(&quot;Content-Disposition: attachment; filename=\&quot;my-data.csv\&quot;&quot;);<br />$data=stripcslashes($_REQUEST[&#039;csv_text&#039;]);<br />echo $data; <br />?&gt;<br /><br /></pre>]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry131226-173136</guid>
			<author>Angel</author>
			<pubDate>Fri, 27 Dec 2013 00:31:36 GMT</pubDate>
		</item>
		<item>
			<title>Abbreviating Monetary Values</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry131218-115457</link>
			<description><![CDATA[<pre><br />Tested solution:<br /><br />function CurrencyFormat($cash) {<br />    // strip any commas <br />    $cash = (0 + str_replace(&#039;,&#039;, &#039;&#039;, $cash));<br /><br /> <br />    // filter and format it <br />    if($cash&gt;1000000000000){ <br />		return &quot;$&quot;.round(($cash/1000000000000),1).&#039;T&#039;;<br />    }elseif($cash&gt;1000000000){ <br />		return &quot;$&quot;.round(($cash/1000000000),1).&#039;B&#039;;<br />    }elseif($cash&gt;1000000){ <br />		return &quot;$&quot;.round(($cash/1000000),1).&#039;M&#039;;<br />    }elseif($cash&gt;1000){ <br />		return &quot;$&quot;.round(($cash/1000),1).&#039;K&#039;;<br />	}<br /> <br />   <br />}<br /><br /><br />echo CurrencyFormat(&#039;1,560,000&#039;);//outputs $1.6M<br /></pre>]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry131218-115457</guid>
			<author>Angel</author>
			<pubDate>Wed, 18 Dec 2013 18:54:57 GMT</pubDate>
		</item>
		<item>
			<title>CSV file to MySQL with PHP</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry131210-173759</link>
			<description><![CDATA[//draft<br /><br /><pre><br />$csv=&#039;...CSV file (string) exported from open office spread sheet&#039;;<br /><br />$lines = explode(&quot;\n&quot;, $csv);<br /><br />$array = array();<br />foreach ($lines as $line) {<br />    $array =str_getcsv($line);<br />	<br />echo &quot;INSERT INTO TestTable SET `Field1`=&#039;&quot;.addslashes($array[3]).&quot;&#039;,<br />`Field2`=&#039;&quot;.addslashes($array[3]).&quot;&#039;,&quot;;<br />	<br />}<br /></pre>]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry131210-173759</guid>
			<author>Angel</author>
			<pubDate>Wed, 11 Dec 2013 00:37:59 GMT</pubDate>
		</item>
		<item>
			<title>Adjacency List Hierarchy Array to HTML  Unordered  List</title>
			<link>https://angelcool.net/sphpblog/blog_index.php?entry=entry131208-160530</link>
			<description><![CDATA[<pre>&lt;?php<br /><br />/*<br />    Sample data.<br />*/<br />$items = array(<br />   array(&#039;id&#039;=&gt;1, &#039;title&#039;=&gt;&#039;Home&#039;,     &#039;parent_id&#039;=&gt;0),<br />   array(&#039;id&#039;=&gt;2, &#039;title&#039;=&gt;&#039;News&#039;,     &#039;parent_id&#039;=&gt;1),<br />   array(&#039;id&#039;=&gt;3, &#039;title&#039;=&gt;&#039;Sub News&#039;, &#039;parent_id&#039;=&gt;2),<br />   array(&#039;id&#039;=&gt;4, &#039;title&#039;=&gt;&#039;Articles&#039;, &#039;parent_id&#039;=&gt;0),<br />   array(&#039;id&#039;=&gt;5, &#039;title&#039;=&gt;&#039;Article&#039;,  &#039;parent_id&#039;=&gt;4),<br />   array(&#039;id&#039;=&gt;6, &#039;title&#039;=&gt;&#039;Article2&#039;, &#039;parent_id&#039;=&gt;4)<br />);<br /><br />/*<br />    Group by parent.<br />*/<br />$itemsByParent = array();<br />foreach ($items as $item)<br />{<br />  if (!isset($itemsByParent[$item[&#039;parent_id&#039;]]))<br />      $itemsByParent[$item[&#039;parent_id&#039;]] = array();<br /><br />  $itemsByParent[$item[&#039;parent_id&#039;]][] = $item;<br />}<br /><br />/*<br />    Print list recursively.<br />*/<br />function printList($items, $parentId = 0)<br />{<br />   echo &#039;&lt;ul&gt;&#039;;<br />   foreach ($items[$parentId] as $item)<br />   {<br />       echo &#039;&lt;li&gt;&#039;;<br />       echo $item[&#039;title&#039;];<br />       $curId = $item[&#039;id&#039;];<br />       //if there are children<br />       if (!empty($items[$curId]))<br />       {<br />           printList($items, $curId);<br />       }           <br />       echo &#039;&lt;/li&gt;&#039;;<br />   }<br />   echo &#039;&lt;/ul&gt;&#039;;<br />}<br /><br />/*<br />    Finds top parent given node id<br />*/<br />function findTopParent($id,$ibp)<br />{<br />    foreach($ibp as $parentID=&gt;$children)<br />    {<br />        foreach($children as $child)<br />        {<br />          if($child[&#039;id&#039;]==$id)<br />          {<br />            if($child[&#039;parent_id&#039;]!=0)<br />               return findTopParent($child[&#039;parent_id&#039;],$ibp);<br />             else<br />               return $child;<br />          }<br />        }<br />    }<br />}<br /><br />/*<br />    Get all parents, aka. breadcrumbs.<br />*/<br />function getAllParents($id,$ibp)<br />{<br />  foreach($ibp as $parentID=&gt;$nodes)<br />  {<br />    foreach($nodes as $node)<br />    {<br />       if($node[&#039;id&#039;]==$id)<br />       {<br />          if($node[&#039;parent_id&#039;]!=0)<br />          {<br />             $a=getAllParents($node[&#039;parent_id&#039;],$ibp);<br />             array_push($a,$node[&#039;parent_id&#039;]);<br />             return $a;<br />          }<br />          else<br />            return array();<br />       }<br />    }<br />  }<br />}<br /><br />/*<br />    Gets all subnodes; children, grand children, etc...<br />*/<br /> function getDescendants($id,$ibp)<br /> {<br />     if(array_key_exists($id,$ibp))<br />     {<br />         $kids=array();<br />         foreach($ibp[$id] as $child)<br />         {<br />            array_push($kids,$child[&#039;id&#039;]);<br />            <br />            if(array_key_exists($child[&#039;id&#039;],$ibp))<br />                $kids=array_merge($kids,getDescendants($child[&#039;id&#039;],$ibp));<br />         }<br />         return $kids;<br />     }<br />     else<br />        return array();//supplied $id has no kids<br />}<br /><br />// Print it!!<br />printList($itemsByParent);<br /><br />// Find top parent!!<br />print_r(findTopParent(6,$itemsByParent));<br />/*<br />Array<br />(<br />    [id] =&gt; 4<br />    [title] =&gt; Articles<br />    [parent_id] =&gt; 0<br />)<br />*/<br /><br />// Find path!!<br />print_r(getAllParents(3,$itemsByParent));<br />/*<br />Array<br />(<br />    [0] =&gt; 1<br />    [1] =&gt; 2<br />)<br />*/<br /><br />print_r(getDescendants(1,$itemsByParent));<br />/*<br />Array<br />(<br />    [0] =&gt; 2<br />    [1] =&gt; 3<br />)<br />*/<br /><br />print_r(getDescendants(4,$itemsByParent));<br />/*<br />Array<br />(<br />    [0] =&gt; 5<br />    [1] =&gt; 6<br />)<br />*/<br /><br />print_r(getDescendants(0,$itemsByParent));<br />/*<br />Array<br />(<br />    [0] =&gt; 1<br />    [1] =&gt; 2<br />    [2] =&gt; 3<br />    [3] =&gt; 4<br />    [4] =&gt; 5<br />    [5] =&gt; 6<br />)<br />*/</pre><br /><br />Output of printList:<br />
<ul><li>Home<ul><li>News<ul><li>Sub News</li></ul></li></ul></li><li>Articles<ul><li>Article</li><li>Article2</li></ul></li></ul>
]]></description>
			<category>- PHP Notes</category>
			<guid isPermaLink="true">https://angelcool.net/sphpblog/blog_index.php?entry=entry131208-160530</guid>
			<author>Angel</author>
			<pubDate>Sun, 08 Dec 2013 23:05:30 GMT</pubDate>
		</item>
	</channel>
</rss>
