<?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>Witslog Wiki &#187; PHP Code</title>
	<atom:link href="http://witslog.com/wiki/category/technical/php/code-php/feed" rel="self" type="application/rss+xml" />
	<link>http://witslog.com/wiki</link>
	<description>Technical Log</description>
	<lastBuildDate>Fri, 14 May 2010 06:17:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>fetch the records through ajax and jquery in json format using kohana</title>
		<link>http://witslog.com/wiki/technical/php/fetch-the-records-through-ajax-and-jquery-in-json-format-using-kohana</link>
		<comments>http://witslog.com/wiki/technical/php/fetch-the-records-through-ajax-and-jquery-in-json-format-using-kohana#comments</comments>
		<pubDate>Fri, 14 May 2010 06:17:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Kohana]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Code]]></category>

		<guid isPermaLink="false">http://witslog.com/wiki/?p=140</guid>
		<description><![CDATA[HI All,
 After spending lot&#8217;s  of time, i got result in josn format in kohana. I want to fetch all records of last 3 minutes of comment table and it will display on my site after check is record already show on browser or not.
As...]]></description>
			<content:encoded><![CDATA[<p>HI All,</p>
<p><em><strong> After spending lot&#8217;s  of time, i got result in josn format in kohana. I want to fetch all records of last 3 minutes of comment table and it will display on my site after check is record already show on browser or not.</strong></em></p>
<p><strong>As we know kohana is MVC framework so we need to handle controller as well as model.</strong></p>
<p><strong>//Model</strong></p>
<p>In model first i write query to fetch records from database which is under a function get_last_three_minutes_comments.</p>
<p>public function get_last_three_minutes_comments($id){</p>
<p>$query = $this-&gt;db-&gt;query(&#8220;SELECT u.user_name, gs.* from comments as gs INNER JOIN users as u on gs.`user_id`=u.`user_id` WHERE DATE_SUB( now( ) , INTERVAL 3 MINUTE ) &lt; gs.speak_date and gs.group_id=$id ORDER BY id DESC&#8221;);<br />
return $query-&gt;result_array();<br />
}</p>
<p><strong>//Controller</strong></p>
<p>I call this model file from controller by write below code.</p>
<p>public function get_last_three_minutes_comments($page_id){<br />
$data =    $this-&gt;comment-&gt;get_last_three_minutes_comments($page_id); // i have already made a object in constructor for comment table.</p>
<p><strong>// As we know model return data in mixture of array and object and for json we need data only in array format. so here i manipulate data and convert into array format.</strong></p>
<p>$arr = array();<br />
$id=0;<br />
foreach($data as $id =&gt; $rec){<br />
$arr[$id] = array(&#8216;id&#8217;=&gt;$rec-&gt;id,&#8217;user_name&#8217;=&gt;$rec-&gt;user_name,&#8217;speak&#8217;=&gt;$rec-&gt;speak,&#8217;speak_time&#8217;=&gt;$rec-&gt;speak_date);<br />
$id++;<br />
}</p>
<p>//Now we change value in json format using json_encode function and return to JavaScript file by echo command.</p>
<p>echo json_encode(array(&#8216;comment_data&#8217;=&gt;$arr));<br />
}</p>
<p><strong>Javascript file</strong></p>
<p>//In jquery we use $.ajax function to get data from server by using ajax. $.ajax gets some parameter such as dataType which may be get, post, json as our requirement. I use json because i need to handle data in json format. Second parameter is url. url tell to server which page will be called. third one is success. When all server processing is completed then it return data in success which i get in mycomment variable.</p>
<p>$.ajax({<br />
dataType: &#8216;json&#8217;,<br />
url:url_main+&#8221;widgets/get_runtime_comment/&#8221;+tabid[1],<br />
success:function(mycomment){</p>
<p><strong>// In controller i make a array which name is comment_data. Now i access data of json by name of javascript variable in which holds all data then after write array name of php then after call by array index. such as call to user_name field we are below line</strong></p>
<p><strong>mycomment.comment_data[0].user_name;</strong></p>
<p><strong> mycomment is javascript variable name, comment_data is php array and user_name is index name of that array.</strong></p>
<p>len = mycomment.comment_data.length; // here i get total number of records for run a loop.<br />
i=0;<br />
while(i&lt;len){<br />
cmid = mycomment.comment_data[i].id; // here i get id of current record.<br />
comt = $(&#8216;#tabs-&#8217;+tabid[1]).children().find(&#8216;.commentbox&#8217;).find(&#8216;#&#8217;+cmid).html(); // Check whether this record is exist or not in current div.</p>
<p>//if i get blank then it will added into that particular div else ignore it.<br />
if(comt==null){<br />
dt = &#8216;&lt;div id=&#8221;&#8216;+mycomment.comment_data[i].id+&#8217;&gt;&#8217;+mycomment.comment_data[i].speak+&#8217;&lt;br&gt;By: &#8211; &#8216;+mycomment.comment_data[i].user_name+&#8217; on &#8216;+mycomment.comment_data[i].speak_time+&#8217;&lt;/div&gt;&#8217;;<br />
$(&#8216;#tabs-&#8217;+tabid[1]).children().find(&#8216;.commentbox&#8217;).find(&#8216;._group_comments&#8217;).prepend(dt);<br />
}<br />
i++;<br />
}</p>
<p>}<br />
});</p>
<p>I hope you will learn lot&#8217;s of thing from this article.</p>
]]></content:encoded>
			<wfw:commentRss>http://witslog.com/wiki/technical/php/fetch-the-records-through-ajax-and-jquery-in-json-format-using-kohana/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Define variable in Kohana using Config File</title>
		<link>http://witslog.com/wiki/technical/php/kohana/define-variable-in-kohana-using-config-file</link>
		<comments>http://witslog.com/wiki/technical/php/kohana/define-variable-in-kohana-using-config-file#comments</comments>
		<pubDate>Wed, 17 Mar 2010 12:38:48 +0000</pubDate>
		<dc:creator>Pallavi</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP Code]]></category>

		<guid isPermaLink="false">http://witslog.com/wiki/?p=110</guid>
		<description><![CDATA[How to make the global variable in Kohana &#8211; 
To get the value of the variable which is used globally into the website. For it you have to set the variable in config file of Kohana.
For it, you have to open the file -
Kohana -&#62;...]]></description>
			<content:encoded><![CDATA[<p><strong>How to make the global variable in Kohana &#8211; </strong></p>
<p>To get the value of the variable which is used globally into the website. For it you have to set the variable in config file of Kohana.</p>
<p>For it, you have to open the file -</p>
<p>Kohana -&gt; application-&gt;config-&gt; config.php (Path of the config file)</p>
<p>For example -</p>
<p>I have to set the path of the image where it is stored in the folder.</p>
<p>$config['imgpath'] =&#8217;Kohana/images/picture/&#8217;;</p>
<p>To get the config value -</p>
<p>Kohana::config(&#8216;config.imgpath&#8217;);</p>
<p>If you want to print the path -</p>
<p>echo Kohana::config(&#8216;config.imgpath&#8217;);</p>
]]></content:encoded>
			<wfw:commentRss>http://witslog.com/wiki/technical/php/kohana/define-variable-in-kohana-using-config-file/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Read RSS feed XML using CURL in PHP</title>
		<link>http://witslog.com/wiki/technical/php/code-php/read-rss-feed-xml-using-curl-in-php</link>
		<comments>http://witslog.com/wiki/technical/php/code-php/read-rss-feed-xml-using-curl-in-php#comments</comments>
		<pubDate>Wed, 17 Mar 2010 10:55:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Code]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://witslog.com/wiki/?p=106</guid>
		<description><![CDATA[// create a new cURL resource
$ch = curl_init();
$url = &#8220;http://witslog.com/wiki/?feed=rss2&#8243;;
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);  // you have to set the url of the website of which you want to get the rss data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass...]]></description>
			<content:encoded><![CDATA[<p>// create a new cURL resource</p>
<p>$ch = curl_init();<br />
$url = &#8220;http://witslog.com/wiki/?feed=rss2&#8243;;</p>
<p>// set URL and other appropriate options</p>
<p>curl_setopt($ch, CURLOPT_URL, $url);  // you have to set the url of the website of which you want to get the rss data<br />
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br />
curl_setopt($ch, CURLOPT_HEADER, 0);</p>
<p>// grab URL and pass it to the browser<br />
$data = curl_exec($ch);</p>
<p>// close cURL resource, and free up system resources<br />
curl_close($ch);</p>
<p>//Now work with SimpleXmlElement. This is core php xml class. The SimpleXML extension provides a very simple and easily usable    toolset to convert XML to an object that can be processed with    normal property selectors and array iterators.</p>
<p>$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);</p>
<p>//check either it is RSS feeder or ATOM Feeder. Rss feeder have channel node while in Atom feeder you see entry node. Generally each feeder have their defined channel.</p>
<p>if(isset($xml-&gt;channel))<br />
{<br />
parseRSS($xml);<br />
}<br />
if(isset($xml-&gt;entry))<br />
{<br />
parseAtom($xml);<br />
}</p>
<p>//This is for rss Feeder<br />
function parseRSS($xml)<br />
{<br />
echo &#8220;&lt;strong&gt;&#8221;.$xml-&gt;channel-&gt;title.&#8221;&lt;/strong&gt;&#8221;;<br />
$cnt = count($xml-&gt;channel-&gt;item);<br />
for($i=0; $i&lt;$cnt; $i++)<br />
{<br />
$url     = $xml-&gt;channel-&gt;item[$i]-&gt;link;<br />
$title     = $xml-&gt;channel-&gt;item[$i]-&gt;title;<br />
$desc = $xml-&gt;channel-&gt;item[$i]-&gt;description;</p>
<p>echo &#8216;&lt;a href=&#8221;&#8216;.$url.&#8217;&#8221;&gt;&#8217;.$title.&#8217;&lt;/a&gt;&#8217;.$desc.&#8221;;<br />
}<br />
}</p>
<p>//This is for XML Feeder<br />
function parseAtom($xml)<br />
{<br />
echo &#8220;&lt;strong&gt;&#8221;.$xml-&gt;author-&gt;name.&#8221;&lt;/strong&gt;&#8221;;<br />
$cnt = count($xml-&gt;entry);<br />
for($i=0; $i&lt;$cnt; $i++)<br />
{<br />
$urlAtt = $xml-&gt;entry-&gt;link[$i]-&gt;attributes();<br />
$url    = $urlAtt['href'];<br />
$title     = $xml-&gt;entry-&gt;title;<br />
$desc    = strip_tags($xml-&gt;entry-&gt;content);</p>
<p>echo &#8216;&lt;a href=&#8221;&#8216;.$url.&#8217;&#8221;&gt;&#8217;.$title.&#8217;&lt;/a&gt;&#8217;.$desc.&#8221;;<br />
}<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://witslog.com/wiki/technical/php/code-php/read-rss-feed-xml-using-curl-in-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a Basic Web Service Using PHP, MySQL, XML, and JSON</title>
		<link>http://witslog.com/wiki/technical/php/code-php/create-a-basic-web-service-using-php-mysql-xml-and-json</link>
		<comments>http://witslog.com/wiki/technical/php/code-php/create-a-basic-web-service-using-php-mysql-xml-and-json#comments</comments>
		<pubDate>Wed, 17 Mar 2010 07:34:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP Code]]></category>

		<guid isPermaLink="false">http://witslog.com/wiki/?p=103</guid>
		<description><![CDATA[Web services are taking over the world.  I credit Twitter’s epic rise to  the availability of a simple but rich API.  Why not use the same model  for your own sites?  Here’s how to create a basic web service that...]]></description>
			<content:encoded><![CDATA[<p><strong>Web services are taking over the world.  I credit Twitter’s epic rise to  the availability of a simple but rich API.  Why not use the same model  for your own sites?  Here’s how to create a basic web service that  provides an XML or JSON response using some PHP and MySQL.</strong></p>
<p>/* require the user as the parameter */<br />
if(isset($_GET['user']) &amp;&amp; intval($_GET['user'])) {</p>
<p>/* soak in the passed variable or set our own */<br />
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default<br />
$format = strtolower($_GET['format']) == &#8216;json&#8217; ? &#8216;json&#8217; : &#8216;xml&#8217;; //xml is the default<br />
$user_id = intval($_GET['user']); //no default</p>
<p>/* connect to the db */<br />
$link = mysql_connect(&#8216;localhost&#8217;,'username&#8217;,'password&#8217;) or die(&#8216;Cannot connect to the DB&#8217;);<br />
mysql_select_db(&#8216;db_name&#8217;,$link) or die(&#8216;Cannot select the DB&#8217;);</p>
<p>/* grab the posts from the db */<br />
$query = &#8220;SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = &#8216;publish&#8217; ORDER BY ID DESC LIMIT $number_of_posts&#8221;;<br />
$result = mysql_query($query,$link) or die(&#8216;Errant query:  &#8216;.$query);</p>
<p>/* create one master array of the records */<br />
$posts = array();<br />
if(mysql_num_rows($result)) {<br />
while($post = mysql_fetch_assoc($result)) {<br />
$posts[] = array(&#8216;post&#8217;=&gt;$post);<br />
}<br />
}</p>
<p>/* output in necessary format */<br />
if($format == &#8216;json&#8217;) {<br />
header(&#8216;Content-type: application/json&#8217;);<br />
echo json_encode(array(&#8216;posts&#8217;=&gt;$posts));<br />
}<br />
else {<br />
header(&#8216;Content-type: text/xml&#8217;);<br />
echo &#8216;&lt;posts&gt;&#8217;;<br />
foreach($posts as $index =&gt; $post) {<br />
if(is_array($post)) {<br />
foreach($post as $key =&gt; $value) {<br />
echo &#8216;&lt;&#8217;,$key,&#8217;&gt;&#8217;;<br />
if(is_array($value)) {<br />
foreach($value as $tag =&gt; $val) {<br />
echo &#8216;&lt;&#8217;,$tag,&#8217;&gt;&#8217;,htmlentities($val),&#8217;&lt;/&#8217;,$tag,&#8217;&gt;&#8217;;<br />
}<br />
}<br />
echo &#8216;&lt;/&#8217;,$key,&#8217;&gt;&#8217;;<br />
}<br />
}<br />
}<br />
echo &#8216;&lt;/posts&gt;&#8217;;<br />
}</p>
<p>/* disconnect from the db */<br />
@mysql_close($link);<br />
}?</p>
<p><strong><br />
</strong></p>
<p><strong>With the number of persons hitting your web service (hopefully),  you’ll need to do adequate validation before attempting to connect to  the database to avoid injection attacks.  Once we get the desired  results from the database, we cycle through the results to populate our  return results array.  Depending upon the response type desired, we  output the proper header and content in the desired format.</strong></p>
<p><strong>Take  the following sample URL for example:</strong></p>
<p>http://mydomain.com/web-service.php?user=2&amp;num=10</p>
<p>&lt;posts&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;SSLmatic SSL Certificate Giveaway Winners&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2304&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;MooTools FileManager&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2288&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;PHPTVDB: Using PHP to Retrieve TV Show Information&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2266&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;David Walsh: The Lost MooTools Plugins&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2258&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;Create Short URLs Using U.Nu&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2218&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;Create Bit.ly Short URLs Using PHP&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2194&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;Represent Your Repositories Using the GitHub Badge!&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2178&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;ZebraTable&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?page_id=2172&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;MooTools Zebra Table Plugin&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2168&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;post&gt;<br />
&lt;post_title&gt;SSLmatic: Quality, Cheap SSL Certificates and Giveaway!&lt;/post_title&gt;<br />
&lt;guid&gt;http://davidwalsh.name/?p=2158&lt;/guid&gt;<br />
&lt;/post&gt;<br />
&lt;/posts&gt;</p>
<p><strong>Take this next sample URL for example:</strong></p>
<p>http://mydomain.com/web-service.php?user=2&amp;num=10&amp;format=json</p>
<p><strong>Now, we can take a look at the possible results of the URL.</strong></p>
<p>{&#8220;posts&#8221;:[{"post":{"post_title":"SSLmatic SSL Certificate Giveaway Winners","guid":"http:\/\/davidwalsh.name\/?p=2304"}},{"post":{"post_title":"MooTools FileManager","guid":"http:\/\/davidwalsh.name\/?p=2288"}},{"post":{"post_title":"PHPTVDB: Using PHP to Retrieve TV Show Information","guid":"http:\/\/davidwalsh.name\/?p=2266"}},{"post":{"post_title":"David Walsh: The Lost MooTools Plugins","guid":"http:\/\/davidwalsh.name\/?p=2258"}},{"post":{"post_title":"Create Short URLs Using U.Nu","guid":"http:\/\/davidwalsh.name\/?p=2218"}},{"post":{"post_title":"Create Bit.ly Short URLs Using PHP","guid":"http:\/\/davidwalsh.name\/?p=2194"}},{"post":{"post_title":"Represent Your Repositories Using the GitHub Badge!","guid":"http:\/\/davidwalsh.name\/?p=2178"}},{"post":{"post_title":"ZebraTable","guid":"http:\/\/davidwalsh.name\/?page_id=2172"}},{"post":{"post_title":"MooTools Zebra Table Plugin","guid":"http:\/\/davidwalsh.name\/?p=2168"}},{"post":{"post_title":"SSLmatic: Quality, Cheap SSL Certificates and Giveaway!","guid":"http:\/\/davidwalsh.name\/?p=2158"}}]}</p>
<p><strong><br />
</strong></p>
<p><strong>Creating a basic web service is very simple and encourages your users to  spread the word about your website or service.  Want more traffic?   Want your website to grow without you putting in all the effort?  Create  a web service!</strong></p>
<p><strong>Source : </strong>http://davidwalsh.name/web-service-php-mysql-xml-json</p>
]]></content:encoded>
			<wfw:commentRss>http://witslog.com/wiki/technical/php/code-php/create-a-basic-web-service-using-php-mysql-xml-and-json/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

