<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>betaProgrammer</title>
	
	<link>http://betaprogrammer.com/content</link>
	<description />
	<lastBuildDate>Sat, 26 May 2012 23:28:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Betaprogrammer" /><feedburner:info uri="betaprogrammer" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><item>
		<title>Preparing to install WordPress from shell in linux</title>
		<link>http://betaprogrammer.com/content/?p=544</link>
		<comments>http://betaprogrammer.com/content/?p=544#comments</comments>
		<pubDate>Sat, 26 May 2012 23:28:56 +0000</pubDate>
		<dc:creator>jakedimare</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Rackspace Recipes]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[tar]]></category>
		<category><![CDATA[wget]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=544</guid>
		<description><![CDATA[If you&#8217;d like to install WordPress on your cloud server, one of the easiest approaches is to work in shell. In this article we&#8217;ll explain how to: Create a new database in MYSQL Download the latest WordPress  package using WGET and TAR Later, in another post, I&#8217;ll cover actually installing WordPress. The first step is [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;d like to install <a title="Link to the WordPress website" href="http://wordpress.org/" target="_blank">WordPress </a>on your cloud server, one of the easiest approaches is to work in shell. In this article we&#8217;ll explain how to:</p>
<ul>
<li>Create a new database in MYSQL</li>
<li>Download the latest WordPress  package using WGET and TAR</li>
</ul>
<p>Later, in another post, I&#8217;ll cover actually installing WordPress. The first step is to create a new database. To do this, first log into your server via shell. If you are on a remote computer , I recommend using <a title="Link to the Putty download page. " href="http://www.putty.org/" target="_blank">Putty</a>.</p>
<p>Once logged into shell it will be necessary to log into MYSQL. I use the mysql command with the root user as follows:</p>
<div class="codeblock">sudo mysql -u root -p</div>
<p>Breaking this command down, I entered &#8216;sudo&#8217; so the system recognizes me as an administrator for this command, then mysql. -u is to indicate the next string is a user and -p indicates it should prompt me for a password.</p>
<div class="noteblock">Note, because I used sudo the system will first prompt me for my sudo (superuser) password. Once I enter it the system will prompt me for the password for my root database account.</div>
<p>The entire exchange looks like this:</p>
<div class="codeblock">
<p>jake@Poseidon:~$ sudo mysql -u root -p<br />
[sudo] password for jake:<br />
Enter password:<br />
Welcome to the MySQL monitor.  Commands end with ; or \g.<br />
Your MySQL connection id is 37855<br />
Server version: 5.1.37-1ubuntu5 (Ubuntu)</p>
<p>Type &#8216;help;&#8217; or &#8216;\h&#8217; for help. Type &#8216;\c&#8217; to clear the current input s</p>
<p>mysql&gt;</p>
</div>
<p>Once successfully logged into the database there are only two things to do:</p>
<ol>
<li>Create a new database</li>
<li>Create a new user with rights to the database</li>
</ol>
<p>The first step, creating a new user is accomplished using the CREATE DATABASE command. The syntax is as follows:</p>
<div class="codeblock">CREATE DATABASE newdatabase;</div>
<p>For instructions on how to create the new user with rights to the database, please reference our previous post on this topic:<br />
<a title="link to another article" href="http://betaprogrammer.com/content/?p=525" target="_blank"> ADDING DATABASE USERS TO AN EXISTING MYSQL DATABASE</a></p>
<p>The next step is to download and unzip a fresh copy of the WordPress code base. In order to do this we will first create a folder for the new site on the server and then download the files:</p>
<div class="codeblock">cd /var/www/<br />
mkdir newdirectory<br />
cd new directory<br />
wget http://wordpress.org/latest.tar.gz<br />
tar -xzvf latest.tar.gz</div>
<p>The WordPress package will extract into a folder called <em>wordpress </em>in the same directory that you downloaded latest.tar.gz.</p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=544</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding database users to an existing MYSQL database</title>
		<link>http://betaprogrammer.com/content/?p=525</link>
		<comments>http://betaprogrammer.com/content/?p=525#comments</comments>
		<pubDate>Fri, 10 Feb 2012 21:46:29 +0000</pubDate>
		<dc:creator>jakedimare</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[create user]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[grant]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=525</guid>
		<description><![CDATA[If you run your own LAMP stack there will probably come a day when you are ready to run more than one CMS driven website on the server. When the time comes, it becomes necessary to create a new database for each one. It is a very bad idea to provide anyone with root access [...]]]></description>
			<content:encoded><![CDATA[<p>If you run your own LAMP stack there will probably come a day when you are ready to run more than one CMS driven website on the server. When the time comes, it becomes necessary to create a new database for each one. It is a very bad idea to provide anyone with root access to the database so creating unique users for each new database is recommended.</p>
<p>When I first moved from Microsoft to LAMP servers there was nothing more confusing to me than interacting with a database through shell. However, since I&#8217;ve grown used to working this way, I&#8217;ve actually come to prefer it. I also think it is an important skill for any programmer or &#8216;code-curious&#8217; individual to get familiar with so we&#8217;ll approach the task this way.</p>
<p>Once logged into shell it will be necessary to log into MYSQL. I use the mysql command with the root user as follows:</p>
<div class="codeblock">sudo mysql -u root -p</div>
<p>Breaking this command down, I entered &#8216;sudo&#8217; so the system recognizes me as an administrator for this command, then mysql. -u is to indicate the next string is a user and -p indicates it should prompt me for a password.</p>
<div class=noteblock>Note, because I used sudo the system will first prompt me for my sudo (superuser) password. Once I enter it the system will prompt me for the password for my root database account.</div>
<p> The entire exchange looks like this:</p>
<div class="codeblock">
<p>jake@Poseidon:~$ sudo mysql -u root -p<br />
[sudo] password for jake:<br />
Enter password:<br />
Welcome to the MySQL monitor.  Commands end with ; or \g.<br />
Your MySQL connection id is 37855<br />
Server version: 5.1.37-1ubuntu5 (Ubuntu)</p>
<p>Type &#8216;help;&#8217; or &#8216;\h&#8217; for help. Type &#8216;\c&#8217; to clear the current input s</p>
<p>mysql&gt;</p>
</div>
<p>Once successfully logged into the database there are only two things to do:</p>
<ol>
<li>Create a new user</li>
<li>Grant the new user rights to an existing database</li>
</ol>
<p>The first step, creating a new user is accomplished using the CREATE USER command. The syntax is as follows:</p>
<div class=codeblock>
CREATE USER ‘newuser’@’localhost’ IDENTIFIED BY ‘password’;
</div>
<p>
The items in capital letters are parts of the command that don&#8217;t change. The parts within the single quotes are all variables which can be changed. However, if you are working on one machine like I do, &#8216;localhost&#8217; does not change. Give the new user a custom name and password and don&#8217;t forget the trailing semicolon! </p>
<p>The final command will give the new user rights to a specified, existing database using the GRANT command:</p>
<div class=codeblock>
GRANT ALL ON database.* TO ‘user’@’localhost’;
</div>
<p>In this case the word database and the elements within single quotes are all variables. Database should be replaced with the name of the database you want to give the user access to. User should be replaced with the name of the user you just created and the same rules apply for localhost. </p>
<div class=noteblock>
<strong>Some things to keep in mind:</strong></p>
<ol>
<li>In order to confirm the correct spelling of the database name use the command SHOW DATABASES;</li>
<li>Be sure to use sufficiently complex passwords for database users. We recommend at least 8 characters in length using upper and lowercase letters, numbers <em>and </em>special characters. </li>
</ol>
</div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=525</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Configure Apache2 to Allow Drupal to Rewrite Paths</title>
		<link>http://betaprogrammer.com/content/?p=492</link>
		<comments>http://betaprogrammer.com/content/?p=492#comments</comments>
		<pubDate>Sat, 10 Sep 2011 02:26:21 +0000</pubDate>
		<dc:creator>jakedimare</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rackspace Recipes]]></category>
		<category><![CDATA[apache2]]></category>
		<category><![CDATA[drupal]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[rewrite]]></category>
		<category><![CDATA[rules]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=492</guid>
		<description><![CDATA[In this Rackspace Recipe we&#8217;ll configure a &#8216;sites-available&#8217; file in Apache2 in order to allow Drupal (or WordPress) to rewrite paths. After installing a Drupal site on a Rackspace Cloud server for the  first time, chances are you&#8217;ll notice it is not possible to enable the Clean URLs feature. This is most likely because Drupal [...]]]></description>
			<content:encoded><![CDATA[<p>In this <a title="Link to Rackspace Recipe category" href="/content/?category_name=rackspace-recipes">Rackspace Recipe</a> we&#8217;ll configure a &#8216;sites-available&#8217; file in Apache2 in order to allow <a title="Link to the Drupal Website" href="http://drupal.org/" target="_blank">Drupal</a> (or <a title="Link to the WordPress website" href="http://wordpress.org/" target="_blank">WordPress</a>) to rewrite paths. After installing a Drupal site on a <a title="Link to Rackspace Cloud Servers" href="http://www.rackspace.com/cloud/cloud_hosting_products/servers/" target="_blank">Rackspace Cloud</a> server for the  first time, chances are you&#8217;ll notice it is not possible to enable the Clean URLs feature. This is most likely because Drupal doesn&#8217;t have permission to rewrite the path.</p>
<p>Fortunately, this is a very simple issue to correct. In order to gain access to the sites-available file for the site in question, it is necessary to log in via SSH or shell access on a Unix, Linux or a Mac. You can also access the shell by logging into the Rackspace cloud management console for your account. If you are trying to access SSH via a PC I recommend <a title="Link to download putty" href="http://www.chiark.greenend.org.uk/~sgtatham/putty/" target="_blank">PuTTY</a>.</p>
<div class="noteblock">All of the Rackspace Recipes on betaprogrammer.com reference the specific installation we&#8217;re currently using, which is Ubuntu server running apache2. Some differences may exist on other specific configurations.</div>
<p>Each site we publish on our Rackspace server uses a unique sites-available configuration file. These &#8216;files&#8217; are located in a specific location by default: /etc/apache2/sites-available. Once logged into the server, type the following:</p>
<div class="codeblock">CD /etc/apache2/sites-available (enter)<br />
sudo nano yoursitename (enter)</div>
<p>Once you&#8217;ve opened the specific site configuration file add the following:</p>
<div class="codeblock">&lt;Directory /var/www/example&gt;<br />
RewriteEngine on<br />
RewriteBase /<br />
RewriteCond %{REQUEST_FILENAME} !-fsu<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]<br />
&lt;/Directory&gt;</div>
<p>Of course, change the directory from &#8216;example&#8217; to the actual directory for the specific site in question. Once this is complete use control-x to exit nano and be sure to save the file under the same name. Once out of the site it couldn&#8217;t hurt to restart apache with the following command:</p>
<div class="codeblock">sudo /etc/init.d/apache2 reload</div>
<p>In order to determine success, simply log into the Drupal site and check to see if it is now possible to enable clean URL&#8217;s. (/admin/settings/clean-urls)</p>
<div class="noteblock">If you don&#8217;t have a lot of experience working in sites-available configuration files it is a good idea to start by saving a backup of the file contents. This way, if anything goes wrong, you can quickly revert back to the way it was before you started.</div>
<p>This technique can also be used to make it possible for a WordPress site to create simple paths. However, the code for WordPress is slightly different:</p>
<div class="codeblock">&lt;Directory /var/www/example&gt;<br />
RewriteEngine on<br />
RewriteBase /<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteRule . /index.php [L]<br />
&lt;/Directory&gt;</div>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=492</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Launching a fresh Drupal Installation on Linux with Drush</title>
		<link>http://betaprogrammer.com/content/?p=469</link>
		<comments>http://betaprogrammer.com/content/?p=469#comments</comments>
		<pubDate>Tue, 08 Mar 2011 20:51:11 +0000</pubDate>
		<dc:creator>jakedimare</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rackspace Recipes]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=469</guid>
		<description><![CDATA[Care to venture a guess as to how easy it is to launch a fresh installation of the Drupal CMS? Would you guess 50, 25 or 12 steps? How about 8 steps? Believe it or not, that&#8217;s right. In this Rackspace Recipe we will review just how easy it is. This tutorial assumes you&#8217;re working [...]]]></description>
			<content:encoded><![CDATA[<p>Care to venture a guess as to how easy it is to launch a fresh installation of the Drupal CMS? Would you guess 50, 25 or 12 steps? How about 8 steps? Believe it or not, that&#8217;s right. In this <a href="/content/?category_name=rackspace-recipes">Rackspace Recipe</a> we will review just how easy it is. This tutorial assumes you&#8217;re working on a Linux server with the <a href="/content/?p=466">LAMP stack</a> and Drush previously installed. It also assumes you are logged into the shell with sudo permissions. </p>
<ol>
<li>Navigate to the root web folder by typing: &#8216;cd /var/www&#8217;</li>
<li>Download the latest version of Drupal by typing: &#8216;drush dl drupal&#8217;</li>
<li>Rename the Drupal folder by typing: &#8216;mv drupal-XXXX newsitename&#8217;</li>
<li>Type &#8216;cd newsitename&#8217; and open permissions for installation with &#8216;chmod -R 777 . &#8216;</li>
<li>Create a database in mysql with the name of the site</li>
<li>Update sites/default/settings.php with the correct connection string</li>
<li>Run the install.php file</li>
<li>Change permissions back to 755</li>
</ol>
<p>Drush is an incredible tool which I could talk about for days, if I fully understood it. This is literally just the tip of the iceberg. To take this one step further&#8230;what if you wanted to add a bunch of modules to your new Drupal site? That&#8217;s pretty easy too. For instance&#8230;you could download an unlimited number of modules using Drush dl (download). Example:</p>
<div class='codeblock'>
drush dl cck views views_slideshow ctools views_bonus adminrole advanced_help panels admin_menu jquery_update pathauto print nodewords page_title globalredirect path_redirect taxonomy_manager node_import menu_block custom_breadcrumbs ckeditor filefield imageapi imagecache imagefield lightbox2 emfield messaging token webform google_analytics service_links date calendar devel backup_migrate jquery_ui nodequeue context context_reaction_theme views_arg_context cmf content_taxonomy menutrails image
</div>
<p>The trick here is to be within the folder containing the site you want these modules to populate. </p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=469</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Installing LAMP on an Ubuntu Server</title>
		<link>http://betaprogrammer.com/content/?p=466</link>
		<comments>http://betaprogrammer.com/content/?p=466#comments</comments>
		<pubDate>Wed, 02 Feb 2011 02:29:07 +0000</pubDate>
		<dc:creator>Jake DiMare</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rackspace Recipes]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[Rackspace]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=466</guid>
		<description><![CDATA[In this Rackspace Recipe we&#8217;re going to map out the process for installing LAMP (Linux Apache MySQL PHP) on an Ubuntu server. This will all be done from the command prompt or shell, as it is called in Linux and Unix. If you are attempting to connect to a Linux machine from Windows I suggest [...]]]></description>
			<content:encoded><![CDATA[<p>In this <a href="/content/?category_name=rackspace-recipes">Rackspace Recipe</a> we&#8217;re going to map out the process for installing LAMP (Linux Apache MySQL PHP) on an Ubuntu server. This will all be done from the command prompt or shell, as it is called in Linux and Unix. If you are attempting to connect to a Linux machine from Windows I suggest using <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/" target="_blank">PuTTY</a>. On a Mac or Linux machine, simply open the shell utility and log in to the server with SSH using the credentials provided by Rackspace.</p>
<p>Once logged in:</p>
<ol>
<li>Type &#8216;sudo apt-get install apache2&#8242; (yes, it will download and install the package)</li>
<li>Type &#8216;y&#8217; when prompted</li>
<li>Test Apache by opening a web browser and entering the IP address of your machine.</li>
<li>Type &#8216;sudo apt-get install php5 libapache2-mod-php5&#8242;</li>
<li>Type &#8216;y&#8217; when prompted</li>
<li>Type &#8216;sudo /etc/init.d/apache2 restart&#8217;</li>
<li>Create a file called using &#8216;sudo nano /var/www/testphp.php&#8217; enter the following line: &lt;?php phpinfo(); ?&gt; and then click control x to close and save the file.</li>
<li>Open a browser and navigate to http://yourserverip/testphp.php. If you see the PHP diagnostic page you&#8217;ve successfully installed PHP.</li>
<li>Type &#8216;sudo apt-get install mysql-server&#8217;</li>
<li>During the install you&#8217;ll be prompted to enter a password.</li>
<li>Test mysql by typing &#8216;mysql -u yourusername -p&#8217;</li>
<li>Enter your password when prompted. If you get a mysql prompt you&#8217;re done!</li>
</ol>
<p>As you can see, without testing this process is complete in less than 12 steps and it is free. I began my career on working at a Windows shop and I was amazed at the speed and ease of using a command prompt once I learned a few simple concepts.</p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=466</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Floating in the Rackspace Cloud</title>
		<link>http://betaprogrammer.com/content/?p=455</link>
		<comments>http://betaprogrammer.com/content/?p=455#comments</comments>
		<pubDate>Wed, 02 Feb 2011 01:07:08 +0000</pubDate>
		<dc:creator>Jake DiMare</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Rackspace Recipes]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Rackspace]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=455</guid>
		<description><![CDATA[I recently began migrating my own websites from a typical managed hosting service to the Rackspace Cloud in order to save money on hosting. I was surprised to discover unparalleled customer service, efficient administrative interface and a completely reliable, fast server. As a result I have decided I am going to use the cloud for [...]]]></description>
			<content:encoded><![CDATA[<p>I recently began migrating my own websites from a typical managed hosting service to the <a href="http://www.rackspacecloud.com/index.php" target="_blank">Rackspace Cloud</a> in order to save money on hosting. I was surprised to discover unparalleled customer service, efficient administrative interface and a completely reliable, fast server. As a result I have decided I am going to use the cloud for my client&#8217;s needs whenever possible.</p>
<p>Before you start migrating all your sites, it is important to mention that a cloud server is no different from setting up a server in your own home or office (other than the location of the machine). You will need to administer every aspect of the machine, including security. Although Rackspace customer service is incredibly helpful, it is important to understand the fundamentals of server administration. I thought it would be fun to explain some of things I am learning along the way.</p>
<div class="noteblock">
<p>Everything I will share in this series I plan to tag as &#8220;Rackspace Recipes&#8221; will be based on my own experiences setting up and running LAMP on an <a href="http://www.ubuntu.com/" target="_blank">Ubuntu </a>Linux server. I&#8217;d like to point out they offer a variety of choices which might be better suited for your own specific needs.</p>
</div>
<p>Today we&#8217;ll take a real quick look at how easy it is to set up a new Ubuntu Linux server in the Rackspace cloud. It couldn&#8217;t possibly be any easier:</p>
<ol>
<li>Set up a new account with <a href="https://signup.rackspacecloud.com/signup" target="_blank">Rackspace Cloud</a></li>
<li><a href="https://manage.rackspacecloud.com/" target="_blank">Login</a></li>
<li>Click Hosting</li>
<li>Click Cloud Servers</li>
<li>Click Add Server</li>
<li>On the first tab you will see options for Linux servers. I use Ubuntu 9.1</li>
<li>Give your server a name</li>
<li>Select a server size. It&#8217;s OK not to be perfect here because you can adjust it on the fly later. I started with 256 but 512 is probably the minimum size for a server running multiple Drupal web sites.</li>
<li>Click Create Server</li>
</ol>
<p>That&#8217;s it! You now have a Linux server. In our next Rackspace Recipe we&#8217;ll explore installing the LAMP environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=455</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uploading files using PHP and HTML</title>
		<link>http://betaprogrammer.com/content/?p=432</link>
		<comments>http://betaprogrammer.com/content/?p=432#comments</comments>
		<pubDate>Thu, 09 Sep 2010 16:49:18 +0000</pubDate>
		<dc:creator>Jake DiMare</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[filesystem]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[styles]]></category>
		<category><![CDATA[upload]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=432</guid>
		<description><![CDATA[This week we&#8217;ll take a look at manipulating files and folders in PHP by creating an upload form component. Our example will apply PHP filesystem and directory functions and is 100% functional. Feel free to use it on your own project or modify as you see fit. The files are located here. Check out the [...]]]></description>
			<content:encoded><![CDATA[<p>This week we&#8217;ll take a look at manipulating files and folders in PHP by creating an upload form component. Our example will  apply  PHP filesystem and directory functions and is 100% functional. Feel free to use it on your own project or modify as you see fit. The files are located <a href="/related/file-upload.zip" target="_blank">here.</a><br />
  <span id="more-432"></span> </p>
<p>Check out the example below:</p>
<p>&nbsp;<iframe class="iframes" src="http://betaprogrammer.com/related/file-upload/form.php">If you can see this, your browser doesn&#8217;t render IFRAME so here&#8217;s a <A HREF="http://betaprogrammer.com/related/file-upload/form.php" mce_HREF="http://betaprogrammer.com/related/file-upload/form.php">link</A> to the example.</iframe></p>
<div class="noteblock">
<p>This above program has three distinct components:</p>
<ul>
<li>form.php</li>
<li>process.php</li>
<li>uploads (folder)</li>
</ul>
</div>
<p>The form.php file is what actually displays the form fields on the page. However, behind the scenes in our example form.php is also used to set establishing variables and handle dynamic feedback to report error conditions. This is why it is a php file instead of simple html. For this reason, we&#8217;ll break it down into two separate parts below. The first part will be the PHP components.</p>
<div class="codeblock">
<p>&lt;?php</p>
<p>//variables<br />
    $message = &#8221;;</p>
<p>//if the count exists in the url it means a document was uploaded. The count will be used to build a link to the document<br />
    if (isset($_GET['count'])){<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$count = $_GET['count'];<br />
    } else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$count = 0;<br />
    }</p>
<p>//if the name exists in the url it means a document was uploaded. The name will be used to build a link to the document<br />
    if (isset($_GET['name'])){<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$name = $_GET['name'];<br />
    } else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$name = &#8221;;<br />
    }</p>
<p>//if the code exists in the url it means the form was submitted. It is used to determine the feedback message in the switch below <br />
    if (isset($_GET['code'])){<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$code = $_GET['code'];<br />
    } else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$code = 0;<br />
    }</p>
<p>if (isset($_GET['status'])){<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$status = $_GET['status'];<br />
    } else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$status = 0;<br />
    }</p>
<p>if ($status == 0) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$style = &#8216;message&#8217;;<br />
    } else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$style = &#8216;error&#8217;;<br />
    }</p>
<p>    switch ($code) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;100&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;USER ERROR CODE 100: Illegitimate file type submitted. The following file types are accepted: .odt, .doc, .docx, .pdf&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;101&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;USER ERROR CODE 101: Oversize file submitted. The maximum file size accepted is 2 Meg.&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;102&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;SYSTEM ERROR CODE 102: Non-specific directory error.&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;103&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;SYSTEM ERROR CODE 103: Directory already exists.&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;104&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;SYSTEM ERROR CODE 104: File already exists.&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;201&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;Successful upload. &lt;a href=uploads/&#8217;.$count.&#8217;/&#8217;.$name.&#8217;&gt;View File&lt;/a&gt;&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;105&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;SYSTEM ERROR CODE 105: Upload failed, please try again.&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;case &quot;0&quot;:<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &#8216;Ready&#8217;;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;break;<br />
    }<br />
    ?&gt;</p>
</div>
<p>The above code can be further broken down into two parts. The top half is a series of if/then statements that check for the presence of some key variables. The bottom half is a switch used to determine what the current status of the form is. There are a handful of error conditions, a ready condition and a success condition. The condition is determined by evaluating the variable $code which is either set to default on this page of gets its value from the query string parameter set in the process.php file. </p>
<p>The next block of code includes the HTML elements and some PHP making up the remainder of the form.php file. </p>
<div class="codeblock">
<p>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />
    &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />
    &lt;head&gt;<br />
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br />
    &lt;title&gt;File Uploader&lt;/title&gt;<br />
    &lt;style&gt;<br />
    body {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;margin:20px;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;background-color:#CCC;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;font-size:12px;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;font-family:Verdana, Geneva, sans-serif;<br />
    }</p>
<p>.message {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;font-weight:bold;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;color:#060;<br />
    }</p>
<p>.error {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;font-weight:bold;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;color:#F00;<br />
    }</p>
<p>.form {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;padding: 20px 0px 0px 0px;<br />
    }</p>
<p>a 			{color:#060; text-decoration:none;}<br />
    a:hover 	{color:#00F; text-decoration:none;}<br />
    &lt;/style&gt;<br />
    &lt;/head&gt;</p>
<p>&lt;body&gt;<br />
    &lt;div class=&quot;standard&quot;&gt;Form Status: &lt;span class=&quot;&lt;?php echo $style; ?&gt;&quot;&gt;&lt;?php echo $message; ?&gt;&lt;/span&gt;&lt;/div&gt;</p>
<p>&lt;div class=&quot;form&quot;&gt;<br />
    &lt;form enctype=&quot;multipart/form-data&quot; action=&quot;process.php&quot; method=&quot;POST&quot;&gt;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;200000&quot; /&gt;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;Choose a file to upload: &lt;input name=&quot;file&quot; id=&quot;file&quot; type=&quot;file&quot; /&gt;&lt;br /&gt;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&lt;input type=&quot;submit&quot; value=&quot;Upload File&quot; /&gt;<br />
    &lt;/form&gt;</p>
<p>&lt;/div&gt;<br />
    &lt;div class=&quot;form&quot;&gt;&lt;a href=&quot;form.php&quot;&gt;Reset Form&lt;/a&gt;&lt;/div&gt;<br />
    &lt;/body&gt;<br />
    &lt;/html&gt;</p>
</div>
<p>The above code starts with the standard HTML head information and includes some basic inline styles. Within the body there is a DIV to display the status message, a DIV with the form and a DIV with a link to &#8216;reset&#8217; the form. This last piece wouldn&#8217;t typically be necessary but it is really handy for the example. As you can see, when the form is submitted it runs the other file, process.php </p>
<p>Process.php can essentially be broken down into three components. The first group checks the file being uploaded to ensure it is an acceptable file type and size. The second group determines if the file already exists and then creates a subfolder for it.  If any of the checks in the first two functions catch an issue the status is set to 1 and a code is saved so the form page will know what the problem is.</p>
<p>The final function of process.php is to upload the file if there are no issues and return to the form page with a status code.</p>
<div class="codeblock">
<p>&lt;?php</p>
<p>//establishing variables<br />
    $status = 0;<br />
    $code = 0;<br />
    $name = $_FILES[&quot;file&quot;][&quot;name&quot;];<br />
    $name = str_replace(&quot; &quot;,&quot;&quot;,$name);<br />
    $thisSize = $_FILES[&quot;file&quot;][&quot;size&quot;];</p>
<p>//validate file type<br />
    $allowedExtensions = array(&quot;txt&quot;,&quot;doc&quot;,&quot;pdf&quot;,&quot;odt&quot;,&quot;docx&quot;); <br />
    if (!in_array(end(explode(&quot;.&quot;,$name)),$allowedExtensions)) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$status = 1; //disallowed file type<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$code = 100; //illegitimate file type <br />
    }</p>
<p>//validate file size<br />
    if ($thisSize == 0) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$status = 1;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$code = 101;	//file too large  (It returns 0 when it is over the limit)<br />
    }</p>
<p>//each document is put in a unique folder so determine the name of the next folder by counting the existing ones and adding 1.<br />
    $count = 0;<br />
    $directory=&#8217;../file-upload/uploads&#8217;;</p>
<p>if (is_dir($directory)) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;if ($dh = opendir($directory)) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (($item = readdir($dh)) !== false) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($item != &#8216;.&#8217; &amp;&amp; $item != &#8216;..&#8217;) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$count++; <br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;closedir($dh);<br />
    &nbsp;&nbsp;&nbsp;&nbsp;}<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$count++;<br />
    }else{<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$status = 1;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$code = 102;<br />
    }</p>
<p>//check to see if the client folder already exists<br />
    $path = &quot;uploads/&quot;.$count.&quot;/&quot;;<br />
    //if not, create a new client folder<br />
    if (!file_exists($path)) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;mkdir(&quot;uploads/$count/&quot;, 0777);<br />
    } else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$status = 1;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$code = 103;<br />
    }</p>
<p>//check to see if the file already exists<br />
    $fullpath = &quot;uploads/&quot;.$count.&quot;/&quot; . $name;<br />
    if (file_exists($fullpath)) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$status = 1;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$code = 104; //this file already exists<br />
    } </p>
<p>if ($status == 0) {<br />
    //upload the file<br />
    &nbsp;&nbsp;&nbsp;&nbsp;if(move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;],$fullpath)) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//set status<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$code = 201; //successful upload<br />
    &nbsp;&nbsp;&nbsp;&nbsp;} else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$status = 1;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$code = 105; //unknown fail<br />
    &nbsp;&nbsp;&nbsp;&nbsp;}<br />
    }</p>
<p>header(&quot;Location: form.php?status=$status&amp;code=$code&amp;count=$count&amp;name=$name&quot;); <br />
    ?&gt; </p>
</div>
<p>To avoid file naming conflicts we decided to organize every uploaded file in a unique directory within the directory named &#8216;uploads&#8217;. This was done by counting the number of subdirectories using the directory functions &#8216;is_dir&#8217;, &#8216;opendir&#8217; and &#8216;readdir&#8217;. Notice the if statement within the while loop that reads if($item != ‘.’ &#038;&#038; $item != ‘..’). This is so the system does not count the current and parent folders. </p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=432</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Super Clean Base WordPress Template</title>
		<link>http://betaprogrammer.com/content/?p=401</link>
		<comments>http://betaprogrammer.com/content/?p=401#comments</comments>
		<pubDate>Thu, 02 Sep 2010 19:43:37 +0000</pubDate>
		<dc:creator>Jake DiMare</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[base]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[clean]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=401</guid>
		<description><![CDATA[Logically, most designers want to pack their template with lots of cool features in order to makes them more marketable. My idea is to give you something really basic and let you take it from there. The first one is a cleaned up version of the template I created for this very blog. I plan [...]]]></description>
			<content:encoded><![CDATA[<p>Logically, most designers want to pack their template with lots of cool features in order to makes them more marketable. My idea is to give you something really basic and let you take it from there.<span id="more-401"></span><a title="link to the zip file of this theme" href="http://betaprogrammer.com/related/clean.zip" target="_blank"><img style="border: 1px solid black; margin: 2px 5px;" title="Screenshot of the betaProgrammer WordPress template" src="http://www.betaprogrammer.com/content/wp-content/themes/clean/screenshot.png" alt="Screenshot of the betaProgrammer WordPress template" width="365" height="210" align="right" /></a></p>
<p>The first one is a cleaned up version of the template I created for this very blog. I plan to create something even more basic in the future. Enjoy!</p>
<p><strong><a title="link to the zip file of this theme" href="http://betaprogrammer.com/related/clean.zip" target="_blank">Download the templates</a></strong></p>
<p><strong>Notes:</strong></p>
<ul>
<li>The navigation in the header and main navigation are so simple they  are static. You can replace them with nav code from the <a title="Link to wordpress codex" href="http://codex.wordpress.org/Main_Page" target="_blank">WordPress Codex</a> or just edit the links by hand. The secondary navigation in the sidebar  is dynamic and built by the WordPress engine.</li>
<li>In order for the bio picture on the author’s page to display,  simply upload a .jpeg to images/biopics and name it with the ID number  of the author. For instance, if you are author 1 then the image must be  named 1.jpg.</li>
<li>The ‘ads’ are just images with links back to the same page. You can  replace them with embed code from the ad server of your choice. I use  Google Adsense.</li>
</ul>
<p><strong>Installation:</strong></p>
<ol>
<li>Unzip the folder</li>
<li>Put it in the wp-content/themes/ folder</li>
<li>Enjoy!</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=401</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Basic use of recursion, arrays and loops</title>
		<link>http://betaprogrammer.com/content/?p=354</link>
		<comments>http://betaprogrammer.com/content/?p=354#comments</comments>
		<pubDate>Wed, 25 Aug 2010 17:50:01 +0000</pubDate>
		<dc:creator>Jake DiMare</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[calculation]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[modulus]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[preg_split]]></category>
		<category><![CDATA[prime]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[rtrim]]></category>
		<category><![CDATA[tables]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=354</guid>
		<description><![CDATA[In this article we&#8217;ll explore using PHP to calculate whether a given number is prime and then calculate &#038; display all the primes between 0 and that number. For those who slept through math (like me) a prime number is a natural number only divisible by 1 and itself. This problem is excellent for computers [...]]]></description>
			<content:encoded><![CDATA[<p>In this article we&#8217;ll explore using PHP to calculate whether a given number is prime and then calculate &#038; display all the primes between 0 and that number. For those who slept through math (like me) a prime number is a natural number only divisible by 1 and itself. This problem is excellent for computers because the easiest solution is to check every number between 2 and the number in question. The resulting program is a great example to demonstrate how to use recursive loops and build an array. <span id="more-354"></span></p>
<p>The complete source code for the example is available for <a target="_blank" href="http://betaprogrammer.com/primes/index.zip">download here</a>.</p>
<p>&nbsp;<iframe class="modiframes" src="http://betaprogrammer.com/primes/index.php">If you can see this, your browser doesn&#8217;t render IFRAME so here&#8217;s a <A HREF="http://betaprogrammer.com/primes/index.php" mce_HREF="http://betaprogrammer.com/primes/index.php">link</A> to the example.</iframe></p>
<p>The source code starts with the essential head components and then sets up the form.</p>
<div class="codeblock">
<p>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;<br />
    &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;<br />
    &lt;html&gt;<br />
    &lt;head&gt;<br />
    &lt;style type=&quot;text/css&quot;&gt;<br />
    body {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;font-family:Verdana, Geneva, sans-serif;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;font-size: 11px;<br />
    }</p>
<p>.standard {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;padding:5px 5px 0px 5px;<br />
    }</p>
<p>  &lt;/style&gt;<br />
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;&gt;<br />
  &lt;title&gt;Prime Numbers&lt;/title&gt;<br />
  &lt;/head&gt;</p>
<p>&lt;body&gt;<br />
    &lt;div class=&quot;standard&quot;&gt;<br />
    &lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;<br />
    &lt;input type=&quot;text&quot; name=&quot;number&quot; id=&quot;number&quot; value=&quot;7&quot;&gt;<br />
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;&gt;<br />
    &lt;/form&gt;<br />
    &lt;/div&gt;</p>
</div>
<p>The next section of the code, which starts right under the form, includes a handful of establishing variables. Beneath the variables the code uses a simple if/then statement to test for the presence of the post variable, indicating the form was submitted. If the post variable is set the program transfers the value to a variable entitled &#8216;$number&#8217; for further processing. Otherwise a default value is set, which I decided in this case would be 7. Notice this matches the default value for the input field?</p>
<div class="codeblock">
<p>&lt;?php </p>
<p>$number  = &quot;&quot;;<br />
    $i       = 2;<br />
    $status  = &quot;&quot;;<br />
    $message = &quot;&quot;;<br />
    $list    = &quot;1,&quot;;<br />
    $ii      = 2;<br />
    $istatus = &quot;&quot;;
  </p>
<p>if (isset($_POST['number'])) {<br />
    $number = $_POST['number'];<br />
    }else{<br />
    $number = 7;<br />
    }</p>
</div>
<p>The next block of code contains the loop. However, before the loop can run it is important to ensure the value passed is less than 100,000. Without this check it would be easy for a user to enter a number so large the server would not be able to process it before the page timed out. The limit could probably be closer to a million and still work but it is unnecessary to take any chances or over-burden the server for the purposes of this lesson. </p>
<div class="noteblock">
<p>For the next couple of steps the code uses something called a &#8216;for&#8217; loop. A for loop runs until the index ($i) reaches a certain value. It also increments the index on each pass. The proper syntax is:</p>
<p>    for($i;$i&lt;$number;$i++){</p>
<p>enter your code</p>
<p>}</p>
</div>
<p>There are actually two loops in the example, an inner and an outer. The outer loop&#8217;s first job is to determine if the number submitted through the form is prime or not. This is done by dividing it by every number between 2 and one digit less than the submitted number. If the result is a natural number, meaning a positive whole number without any decimals, the submitted number is therefore not prime. This is tested in PHP by using the function is_int which returns true if the number is natural. If this is the case the program adds a 1 to the status variable. Later the status variable is checked as equal to zero (prime) or anything other than zero (not prime). </p>
<p>The inner loop repeats the same process recursively over the current index number to determine if it is prime. If the answer is yes the prime number is appended to a comma separated list, which is creatively entitled &#8216;$list&#8217; in the example. Later the program will convert $list to an array and output it as a table. </p>
<div class="codeblock">
<p>if($number &gt; 100000) { <br />
&nbsp;&nbsp;&nbsp;&nbsp;$message = &quot;That number is too large&quot;;<br />
} else {<br />
&nbsp;&nbsp;&nbsp;&nbsp;echo(&#8216;&lt;div class=&quot;standard&quot;&gt;The number you submitted is:&#8217;.$number.&#8217;&lt;/div&gt;&#8217;);</p>
<p>for($i;$i&lt;$number;$i++){<br />
&nbsp;&nbsp;&nbsp;&nbsp;$temp = ($number/$i);<br />
&nbsp;&nbsp;&nbsp;&nbsp;if(is_int($temp)){<br />
&nbsp;&nbsp;&nbsp;&nbsp;$status .= 1;<br />
}</p>
<p>$ii = 2;<br />
$istatus = &quot;&quot;;</p>
<p>for($ii;$ii&lt;$i;$ii++){<br />
&nbsp;&nbsp;&nbsp;&nbsp;$itemp = ($i/$ii);<br />
&nbsp;&nbsp;&nbsp;&nbsp;if(is_int($itemp)){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$istatus .= 1;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;if($istatus == 0) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$list .= $i.&quot;,&quot;; <br />
&nbsp;&nbsp;&nbsp;&nbsp;} <br />
}</p>
<p> $list = rtrim($list, &quot;,&quot;);<br />
    $result = count($list);</p>
<p>    if($status == 0) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &quot;&lt;div class=standard&gt;This number is prime&lt;/div&gt;&quot;;<br />
    } else {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$message = &quot;&lt;div class=standard&gt;This number is not prime&lt;/div&gt;&quot;;<br />
    }</p>
<p>}</p>
</div>
<p>Notice the PHP function rtrim? Once the loop is complete it is necessary to remove the final trailing &#8220;,&#8221; from $list. Otherwise it will create an empty value later when we split the list up into an array. The final steps are to output our results. The next block of code shows how this is done. </p>
<div class="codeblock">
<p>echo($message.&quot;&lt;div class=standard&gt;The prime numbers between 0 and &quot;.$number.&quot; are: &lt;/div&gt;&quot;);</p>
<p>$splits = preg_split(&quot;[,]&quot;, $list);<br />
    $split_length = count($splits);<br />
    $ai = 1;</p>
<p>echo(&quot;&lt;div&gt;&lt;br&gt;&lt;table cellpadding=5 border=1&gt;&lt;tr&gt;&quot;);</p>
<p>for($ai;$ai&lt;$split_length;$ai++) {<br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo(&quot;&lt;td&gt;&quot;.$splits[$ai]);<br />
    &nbsp;&nbsp;&nbsp;&nbsp;if(($ai&gt;1) &amp;&amp; ($ai%4) == 0) {echo(&quot;&lt;/td&gt;&lt;/tr&gt;&quot;);}else{echo(&quot;&lt;/td&gt;&quot;);}<br />
    }</p>
<p>echo(&quot;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&quot;);</p>
<p>?&gt;
  </p>
<p>&lt;/body&gt;<br />
    &lt;/html&gt;</p>
</div>
<p>After outputting the status of the submitted value the program uses a PHP array function called preg_split to convert the comma separated list of prime numbers to an array named $splits. Preg_split takes two arguments. The first one is a regular expression identifying where to split the string. In this case we want it split at each comma in the list so the first argument is [,]. The second argument is the the actual string to split.</p>
<p>Once the array is created there is one last &#8216;for&#8217; loop over the prime numbers array that outputs each value to a table. The table employs the same modulus operator referenced in the <a href="http://betaprogrammer.com/content/?p=336" target="_blank">last post</a> to know when to create a new row. </p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=354</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Modulus to Build Dynamic Tables in PHP</title>
		<link>http://betaprogrammer.com/content/?p=336</link>
		<comments>http://betaprogrammer.com/content/?p=336#comments</comments>
		<pubDate>Thu, 19 Aug 2010 21:36:24 +0000</pubDate>
		<dc:creator>Jake DiMare</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cell]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[modulus]]></category>
		<category><![CDATA[row]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://betaprogrammer.com/content/?p=336</guid>
		<description><![CDATA[Yesterday, after wrapping up the piece on calculating the Fibonacci sequence I wasn&#8217;t happy about the long vertical presentation. It was a missed opportunity to talk about a cool trick someone taught me years ago for generating tables with a variable number of columns using the modulus operator. Check out the example below. &#160;If you [...]]]></description>
			<content:encoded><![CDATA[<p><P>Yesterday, after wrapping up the piece on calculating the Fibonacci sequence I wasn&#8217;t happy about the long vertical presentation. It was a missed opportunity to talk about a cool trick someone taught me years ago for generating tables with a variable number of columns using the modulus operator. <span id="more-336"></span><br />
</P></p>
<p>
Check out the example below.
</p>
<p>&nbsp;<iframe class="modiframes" src="http://betaprogrammer.com/mod/index.php">If you can see this, your browser doesn&#8217;t render IFRAME so here&#8217;s a <A HREF="http://betaprogrammer.com/mod/index.php" mce_HREF="http://betaprogrammer.com/mod/index.php">link</A> to the example.</iframe></p>
<p>This is accomplished by adding an if/then statement to the loop where the table is generated. Essentially, as the loop begins a table and a row are created. Each iteration of the loop opens a cell, generates the next Fibonacci number and then increments the index. Next, before the loop closes it uses an if/then statement to check if the index, or number of times the loop has happened, divided by the selected number of columns is zero. If the answer is yes then it will close the cell, close the row and open a new row. Otherwise, it will just close the cell. Once the maximum loops have happened it exits the loop and does the reverse. This way it doesn&#8217;t end up with an extra close row tag at the end. See below for a specific example of how this looks using the modulus operator in PHP.</p>
<div class="codeblock">
<p>&lt;?php</p>
<p>echo(&quot;&lt;table border=1&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&quot;);</p>
<p>while($index &lt; $max) { <br />
    &nbsp;&nbsp;&nbsp;&nbsp;$sum = $add1 + $add2; <br />
    &nbsp;&nbsp;&nbsp;&nbsp;echo(&quot;&lt;td&gt;&quot;.$sum); <br />
    &nbsp;&nbsp;&nbsp;&nbsp;if(($index%$mod)==0) {echo(&quot;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&quot;);} else {echo(&quot;&lt;/td&gt;&quot;);} <br />
    &nbsp;&nbsp;&nbsp;&nbsp;$add1 = $add2;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$add2 = $sum;<br />
    &nbsp;&nbsp;&nbsp;&nbsp;$index++;<br />
    }</p>
<p>if((($index-1)%$mod)==0) {echo(&quot;&lt;/table&gt;&quot;);} else {echo(&quot;&lt;/tr&gt;&lt;/table&gt;&quot;);}</p>
<p>&nbsp;</p>
<p>?&gt;</p>
</div>
<p>The complete source code including the form component for this exercise can be <a href="http://betaprogrammer.com/mod/mod_code.zip">downloaded here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://betaprogrammer.com/content/?feed=rss2&amp;p=336</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

