<?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>The Tutorial Blog &#187; foreach</title>
	<atom:link href="http://www.thetutorialblog.com/tag/foreach/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thetutorialblog.com</link>
	<description></description>
	<lastBuildDate>Tue, 08 Nov 2011 19:21:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<image>
<link>http://www.thetutorialblog.com</link>
<url>http://www.thetutorialblog.com/wp-content/plugins/maxblogpress-favicon/icons/favicon-28.ico</url>
<title>The Tutorial Blog</title>
</image>
		<item>
		<title>A Basic PHP Statistics Script</title>
		<link>http://www.thetutorialblog.com/php/a-basic-php-statistics-script/</link>
		<comments>http://www.thetutorialblog.com/php/a-basic-php-statistics-script/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 23:08:01 +0000</pubDate>
		<dc:creator>Wez Pyke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[$_SERVER['REQUEST_URI'];]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[array_count_values]]></category>
		<category><![CDATA[array_push]]></category>
		<category><![CDATA[explode]]></category>
		<category><![CDATA[fclose]]></category>
		<category><![CDATA[fgets]]></category>
		<category><![CDATA[fopen]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[fwrite]]></category>
		<category><![CDATA[scandir]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[substr]]></category>

		<guid isPermaLink="false">http://www.thetutorialblog.com/?p=61</guid>
		<description><![CDATA[	<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
	In this tutorial you will learn about writing information to a text file and then retrieving the information you wrote to the text file. Retrieving the files that are in a directory and about arrays. This is a quite basic script and there isn't much to write about it so if there is something that [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[	<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
	<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.thetutorialblog.com%2Fphp%2Fa-basic-php-statistics-script%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.thetutorialblog.com%2Fphp%2Fa-basic-php-statistics-script%2F&amp;source=wezpyke&amp;style=normal&amp;service=bit.ly" height="61" width="50" title="A Basic PHP Statistics Script" alt=" A Basic PHP Statistics Script" /><br />
			</a>
		</div>
<p><object width="480" height="392" data="http://flash.revver.com/player/1.0/player.swf?mediaId=1525556&#038;affiliateId=42154" type="application/x-shockwave-flash" id="revvervideoa17743d6aebf486ece24053f35e1aa23"><param name="Movie" value="http://flash.revver.com/player/1.0/player.swf?mediaId=1525556&#038;affiliateId=42154"></param><param name="FlashVars" value="allowFullScreen=true&#038;backColor=#000000&#038;frontColor=#ffffff&#038;gradColor=#000000"></param><param name="AllowFullScreen" value="true"></param><param name="AllowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://flash.revver.com/player/1.0/player.swf?mediaId=1525556&#038;affiliateId=42154" pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always" flashvars="allowFullScreen=true&#038;backColor=#000000&#038;frontColor=#ffffff&#038;gradColor=#000000" allowfullscreen="true" width="480" height="392"></embed></object></p>
<p>In this tutorial you will learn about writing information to a text file and then retrieving the information you wrote to the text file. Retrieving the files that are in a directory and about arrays.</p>
<p>This is a quite basic script and there isn't much to write about it so if there is something that you don't understand look the function up within the PHP manual online.<br />
<span id="more-61"></span></p>
<h2>Step 1</h2>
<p>In the first step we are going to get the current page that we are at and store it within a variable. The information in that variable will then be wrote to a file called <em>stats.txt.</em></p>
<p>Include the following code in the pages that you want the number of visits to be counted for. If you have a file such as a header file than put this code in there.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// write current page to stats file</span>
<span style="color: #000088;">$cur_page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cur_page</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$cur_page</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'index.php'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;stats.txt&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;a+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cur_page</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>Step 2</h2>
<p>The next piece of code that we are going to write is going to retrieve the information in <em>stats.txt</em> and put it into an array. We then use the function <em>array_count_values</em> to see how many times the page url occurs. We also retrieve the files in the directory which this script is in to compare them with the files in <em>stats.txt</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;stats.txt&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num_files</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num_files</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000088;">$num_cv</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_count_values</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num_files</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num_cv</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'file: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$f</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' - '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>If you have any other alternatives or need any help then feel free to post a comment.</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.thetutorialblog.com/php/a-basic-php-statistics-script/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Retrieving an RSS Feed in PHP</title>
		<link>http://www.thetutorialblog.com/php/retrieving-an-rss-feed-in-php/</link>
		<comments>http://www.thetutorialblog.com/php/retrieving-an-rss-feed-in-php/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 00:23:38 +0000</pubDate>
		<dc:creator>Wez Pyke</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Text Tutorials]]></category>
		<category><![CDATA[feed]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.thetutorialblog.com/?p=31</guid>
		<description><![CDATA[	<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
	RSS Feeds are widely used throughout the internet today and on all kinds of websites. From websites like Digg to most blogs. If you want to display an RSS feed on your website its probably easier than you think. For this example I'm going to show you how to display the RSS feed for The [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[	<style>
	#code_div { border: 1px solid #DFF4B5; background-color: #F0FFD2; padding-left: 50px; padding:10px;}
	</style>
	
	<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.thetutorialblog.com%2Fphp%2Fretrieving-an-rss-feed-in-php%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.thetutorialblog.com%2Fphp%2Fretrieving-an-rss-feed-in-php%2F&amp;source=wezpyke&amp;style=normal&amp;service=bit.ly" height="61" width="50" title="Retrieving an RSS Feed in PHP" alt=" Retrieving an RSS Feed in PHP" /><br />
			</a>
		</div>
<p><a href="http://en.wikipedia.org/wiki/RSS_(file_format)">RSS Feeds</a> are widely used throughout the internet today and on all kinds of websites. From websites like <a href="http://www.digg.com">Digg</a> to most blogs. If you want to display an RSS feed on your website its probably easier than you think.</p>
<p>For this example I'm going to show you how to display the RSS feed for The Tutorial Blog using <a href="http://www.php.net">PHP</a>.</p>
<p><span id="more-31"></span></p>
<h2>The Code</h2>
<p>The first two lines of code that we write are going to be what retrieve the RSS feed.<br />
<div id="code_div">$file = <a href="http://uk3.php.net/manual/en/function.file-get-contents.php">file_get_contents</a>("http://www.thetutorialblog.com/feed/");<br />
$xml = new <a href="http://uk3.php.net/SimpleXML">SimpleXMLElement</a>($file);</div><br />
Now the next thing we do is use the foreach function so for each item found do something with it<br />
<div id="code_div"><a href="http://uk3.php.net/manual/en/control-structures.foreach.php">foreach</a>($xml-&gt;channel-&gt;item as $feed){<br />
echo $feed-&gt;title.'&lt;br&gt;';<br />
}</div><br />
What are code will do is simply echo the titles of all the items found within the feeds. If you also want to include a link to the feed you can do this by printing to screen <strong>$feed-&gt;link</strong>.</p>
<p>All the code together should look something like this.<br />
<div id="code_div">&lt;?php</p>
<p>$file = file_get_contents("http://www.thetutorialblog.com/feed/");<br />
$xml = new SimpleXMLElement($file);</p>
<p>foreach($xml-&gt;channel-&gt;item as $feed){<br />
echo $feed-&gt;title.'&lt;br&gt;';<br />
}<br />
?&gt;</div></p>
<p>Thanks for taking the time to read this tutorial. I hope you have learned something.</p>


<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.thetutorialblog.com/php/retrieving-an-rss-feed-in-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

