<?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"
	>

<channel>
	<title>CodeGrrl.com &#187; Tutorials</title>
	<atom:link href="http://www.codegrrl.com/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://codegrrl.com</link>
	<description>Taking the grr out of coding!</description>
	<pubDate>Tue, 22 Jul 2008 10:47:45 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>Part 12: Paginating your entries</title>
		<link>http://codegrrl.com/tutorials/part-12-paginating-your-entries</link>
		<comments>http://codegrrl.com/tutorials/part-12-paginating-your-entries#comments</comments>
		<pubDate>Sat, 28 Apr 2007 21:59:06 +0000</pubDate>
		<dc:creator>Amelie</dc:creator>
		
		<category><![CDATA[Build A Blog]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=8</guid>
		<description><![CDATA[Many people have asked how to paginate their blog&#8217;s front page so as to show the previous/next 5 entries, for example. This tutorial was originally posted on the forums by Amanda.


Add this code to the blog page that displays your entries after you have your MySQL connection information:
$blog_postnumber = 5;
The number after the = is [...]]]></description>
			<content:encoded><![CDATA[<p>Many people have asked how to paginate their blog&#8217;s front page so as to show the previous/next 5 entries, for example. This tutorial was originally posted on the <a href="/forums/">forums</a> by Amanda.</p>
<p><span id="more-8"></span></p>
<ol>
<li>Add this code to the blog page that displays your entries after you have your MySQL connection information:
<pre>$blog_postnumber = 5;</pre>
<p>The number after the = is the number of posts you want displayed on your blog.</p>
</li>
<li>Next, add this underneath the above code:
<pre>if(!isset($_GET['page'])) {
	$page = 1;
}
else {
	$page = (int)$_GET['page'];
}
$from = (($page * $blog_postnumber) - $blog_postnumber);</pre>
</li>
<li>Assuming you have kept the code of your blog index page as it is in the past tutorials, you should have this line near the top of the page:
<pre>$sql = &quot;SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT 5&quot;;</pre>
<p>You should change that code to the following:</p>
<pre>$sql = &quot;SELECT * FROM php_blog ORDER BY timestamp DESC LIMIT $from, $blog_postnumber&quot;;</pre>
</li>
<li>The next step is to link the pages. To do this, add the following code to the bottom of your index page. It will calculate how many pages are needed based on how many posts you have, and how many you want to display on each page. It will then link the pages for you.
<pre>$total_results = mysql_fetch_array(mysql_query(&quot;SELECT COUNT(*) as num FROM php_blog&quot;));
$total_pages = ceil($total_results['num'] / $blog_postnumber);
if ($page &gt; 1) {
    $prev = ($page - 1);
    echo &quot;&lt;a href=\&quot;index.php?page=$prev\&quot;&gt;&amp;lt;&amp;lt;  Newer&lt;/a&gt;&nbsp;&quot;;
}
for($i = 1; $i &lt;= $total_pages; $i++) {
    if ($page == $i) {
        echo &quot;$i&nbsp;&quot;;
        }
		else {
           echo &quot;&lt;a href=\&quot;index.php?page=$i\&quot;&gt;$i&lt;/a&gt;&nbsp;&quot;;
        }
}
if ($page &lt; $total_pages) {
   $next = ($page + 1);
   echo &quot;&lt;a href=\&quot;index.php?page=$next\&quot;&gt;Older &amp;gt;&amp;gt;&lt;/a&gt;&quot;;
}</pre>
<p>For this last part to work, it&#8217;s assuming the page with your blog entry list is named index.php. If it&#8217;s not, you can change the index.php part in the last code. (It&#8217;s listed 3 times.)</p>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/part-12-paginating-your-entries/feed</wfw:commentRss>
		</item>
		<item>
		<title>Part 11: Categories</title>
		<link>http://codegrrl.com/tutorials/part-11-categories</link>
		<comments>http://codegrrl.com/tutorials/part-11-categories#comments</comments>
		<pubDate>Fri, 27 Apr 2007 22:12:34 +0000</pubDate>
		<dc:creator>Amelie</dc:creator>
		
		<category><![CDATA[Build A Blog]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=65</guid>
		<description><![CDATA[This tutorial will teach you how to add simple categories to your blog.



The first thing we need to do is to add a category table to our database. We could just list the categories in the entries table, but this can cause problems if you need to rename a category. Doing it this way means [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial will teach you how to add simple categories to your blog.</p>
<p><span id="more-65"></span></p>
<ol>
<li>
<p>The first thing we need to do is to add a category table to our database. We could just list the categories in the entries table, but this can cause problems if you need to rename a category. Doing it this way means that the category will change everywhere if you edit it once.</p>
<p>First, we need to connect to MySQL.</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');</pre>
<p>(Remember to add your own details)</p>
<p>Next we select the database to use:</p>
<pre>mysql_select_db('db_name');</pre>
<p>Second, we tell MySQL to add our table to the database. We will need two columns, one which will be the category ID number, and the other which will be the category name. We will give our category field a limit of 255 characters, since anything longer than that is unlikely. Our ID numbers will be automatically incremented and unique, so we must tell MySQL to do this for us. We will also tell it that the ID will be an unsigned number (i.e., it won&#8217;t be negative) and that the number won&#8217;t go over 9999 (4 digits, in other words).</p>
<p>This is the code we will use:</p>
<pre>$result = mysql_query(&quot;CREATE TABLE php_blog_categories
(`category_id` smallint(4) UNSIGNED AUTO_INCREMENT NOT NULL,
`category_name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`category_id`))&quot;);</pre>
<p>Because each category ID number relates to the category name, we will assign it as our primary key. This helps MySQL to associate the different IDs to the category names, and this will help us to match entries to categories. It also makes sure that we never have a duplicate ID number.</p>
<p>We will run that command through MySQL. To check if it worked or not, we will add a snippet to the script, which will check whether the query was successful (true) or not. If it wasn&#8217;t, we&#8217;d like MySQL to tell us why:</p>
<pre>if ($result == true) echo 'Table created successfully.';
else 'Table could not be created. ' . mysql_error();</pre>
<p>Then we will close MySQL and PHP.</p>
<pre>mysql_close();
?&gt;</pre>
<p>The complete table script should look like this:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

$result = mysql_query(&quot;CREATE TABLE php_blog_categories
(`category_id` smallint(4) UNSIGNED AUTO_INCREMENT NOT NULL,
`category_name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`category_id`))&quot;);

if ($result == true) echo 'Table created successfully.';
else 'Table could not be created. ' . mysql_error();

mysql_close();
?&gt;</pre>
<p>Run this just like you did when creating the initial table in part 1. If you get a success message, you are ready to move onto step 2.</p>
</li>
<li>
<p>The next thing to do is to add a column to our entries table to show which category a post is in. We will be doing this using the category&#8217;s ID number, not the category name. Let&#8217;s say we have a category called &quot;work&quot;, which has an ID of 1. All entries that are categorised under &quot;work&quot; will have the number 1 in their category column, and MySQL will translate that ID, using the table we just created, to its proper name. It sounds complicated, but it really isn&#8217;t!</p>
<p>We will be altering the entries table just like we did for the password in part 4. We will run a quick snippet, just like the one above, to change the table. We need to tell the table that we would like a new column called category, and that it will have the same details as the category ID table (except that it does not need to be unique - one category can contain many entries). This is the code we will use:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

$result = mysql_query(&quot;ALTER TABLE php_blog ADD `category` smallint(4) UNSIGNED NOT NULL&quot;);

if ($result == true) echo 'Table altered successfully.';
else 'Table could not be altered. ' . mysql_error();

mysql_close();
?&gt;</pre>
<p>Run that just like above, and once again if you got a success message you are ready for the next step.</p>
</li>
<li>
<p>Now we must add a page that will manage categories for us. We want to be able to add, rename and delete categories, and this part will teach you how to do this.</p>
<p>Here is the code that we will use:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

if (isset($_POST['add_category'])) {

}
elseif (isset($_POST['edit_category'])) {

}
elseif (isset($_POST['delete_category'])) {

}
else {

}

mysql_close();
?&gt;</pre>
<p>Obviously this is not the complete code, it is the outline for what we want to do. First we are checking whether any of the form buttons were pressed, and which one it was. If no button was pressed, we will get the page to show us all our existing categories.</p>
<p>This is the first thing we will do, even though it comes last in the code structure, as it will help you to see how the categories are processed. It&#8217;s also the easiest part <img src='http://codegrrl.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>As I said, the categories will be shown, along with options of what to do with them, by default on the page.</p>
<p>Find this part in the code structure I posted above:</p>
<pre>else {

}</pre>
<p>Between the { and the }, we will get our categories from the database. This is how we will do it:</p>
<p>First, let&#8217;s get the details from the database.</p>
<pre>$result = mysql_query(&quot;SELECT * FROM php_blog_categories&quot;);</pre>
<p>Second, let&#8217;s close PHP so we can add some HTML code to format the categories before we show them. We also need to create the form that will tell PHP what we want doing with a category.</p>
<pre>?&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;&lt;p&gt;</pre>
<p>Then we will reopen PHP, and start showing our results in a loop (so that we don&#8217;t have to keep repeating the database selection every time). This is the same principle we used when showing the blog entries.</p>
<pre>&lt;?php
while($row = mysql_fetch_array($result)) {
?&gt;</pre>
<p>Here I have closed PHP again, as it will make it easier to see what we are doing. Now we are going to create a list of all the categories, like so:</p>
<pre>&lt;input type=&quot;radio&quot; name=&quot;category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt; &lt;?php echo $row['category_name']; ?&gt;&lt;br /&gt;</pre>
<p>When run by PHP, the HTML will be &#8216;filled in&#8217;, and will look something like this:</p>
<pre>&lt;input type=&quot;radio&quot; name=&quot;category&quot; value=&quot;1&quot; /&gt; Work</pre>
<p>That code will repeat for every category you have.</p>
<p>Now we go back into PHP and and close the while loop:</p>
<pre>&lt;?php
}
?&gt;</pre>
<p>Then we come out of it again, and add our HTML for the edit and delete buttons and the end of the form. Underneath, we will also add a new form to add a new category.</p>
<pre>&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;edit_category&quot; id=&quot;edit_category&quot; value=&quot;Edit selected category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;delete_category&quot; id=&quot;delete_category&quot; value=&quot;Delete selected category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;
&lt;p&gt;Add new category: &lt;input type=&quot;text&quot; name=&quot;new_category&quot; id=&quot;new_category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;add_category&quot; id=&quot;add_category&quot; value=&quot;Add category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;</pre>
<p>We then need to reopen PHP to continue the script.</p>
<pre>&lt;?php</pre>
<p>The complete code for categories.php should look like this now:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

if (isset($_POST['add_category'])) {

}
elseif (isset($_POST['edit_category'])) {

}
elseif (isset($_POST['delete_category'])) {

}
else {

$result = mysql_query(&quot;SELECT * FROM php_blog_categories&quot;);
?&gt;
&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;&lt;p&gt;

&lt;?php
while($row = mysql_fetch_array($result)) {
?&gt;

&lt;input type=&quot;radio&quot; name=&quot;category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt; &lt;?php echo $row['category_name']; ?&gt;&lt;br /&gt;

&lt;?php
}
?&gt;

&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;edit_category&quot; id=&quot;edit_category&quot; value=&quot;Edit selected category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;delete_category&quot; id=&quot;delete_category&quot; value=&quot;Delete selected category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;
&lt;p&gt;Add new category: &lt;input type=&quot;text&quot; name=&quot;new_category&quot; id=&quot;new_category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;add_category&quot; id=&quot;add_category&quot; value=&quot;Add category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;?php
}

mysql_close();
?&gt;</pre>
<p>At the moment, you will most likely not have any categories, as you have just created the tables, so running the categories.php as it is will probably result in a blank page with just the buttons. Don&#8217;t worry! That is normal.</p>
</li>
<li>
<p>Next we will process category addition.</p>
<p>Find the following part in your code:</p>
<pre>if (isset($_POST['add_category'])) {

}</pre>
<p>Between those {}s, we will add our processing code.</p>
<p>First we will strip HTML and convert special characters as both a security precaution and to prevent problems with category names.</p>
<pre>$newcat = htmlspecialchars(strip_tags($_POST['new_category']));</pre>
<p>Next, we need to check if a category was submitted. If the text box was left blank, we will display an error and stop the script from going any further. You might ask why we are doing this after stripping invalid characters from the category - we do it afterwards in case the category was made up only of invalid characters. If the entire category name is stripped out, it will be as if the person didn&#8217;t enter anything at all.</p>
<pre>if (empty($newcat)) {
die(&quot;No category submitted! Please go back and enter one.&quot;);
}</pre>
<p>If the category is not empty, the script will be allowed to continue (it will stop and display an error if the category is empty), so we don&#8217;t need an else statement for the next part, which is to add the category to the database.</p>
<p>Before we add the code to the database, we need to escape quotes. On most servers this is done automatically, but if it isn&#8217;t, you&#8217;ll have problems adding new categories containing quotes.</p>
<p>First we need to check whether the server will escape the quotes for us. Because we only need to do something if the server doesn&#8217;t escape the quotes, that is what we will check for, hence the ! in the if statement.</p>
<pre>if (!get_magic_quotes_gpc()) {
$newcat = mysql_real_escape_string($newcat);
}</pre>
<p>mysql_real_escape_string() will escape the quotes for us. It is similar to addslashes() which is used in previous parts of the Build A Blog tutorials, but mysql_real_escape_string() is especially suited for MySQL databases so is better to use than addslashes().</p>
<p>Here is the code we will use to submit the new category to the database:</p>
<pre>$insert = mysql_query(&quot;INSERT INTO php_blog_categories (`category_name`) VALUES ('$newcat')&quot;);</pre>
<p>We then check to see if the category was successfully submitted:</p>
<pre>if ($insert == true) echo 'Category successfully added.';
else echo 'The category could not be added to the database. ' . mysql_error();</pre>
<p>Now our complete code should look like this:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

if (isset($_POST['add_category'])) {

$newcat = htmlspecialchars(strip_tags($_POST['new_category']));

if (empty($newcat)) {
die( &quot;No category submitted! Please go back and enter one.&quot;);
}

if (!get_magic_quotes_gpc()) {
$newcat = mysql_real_escape_string($newcat);
}

$insert = mysql_query(&quot;INSERT INTO php_blog_categories (`category_name`) VALUES ('$newcat')&quot;);

if ($insert == true) echo 'Category successfully added.';
else echo 'The category could not be added to the database. ' . mysql_error();

}
elseif (isset($_POST['edit_category'])) {

}
elseif (isset($_POST['delete_category'])) {

}
else {

$result = mysql_query(&quot;SELECT * FROM php_blog_categories&quot;);
?&gt;
&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;&lt;p&gt;

&lt;?php
while($row = mysql_fetch_array($result)) {
?&gt;

&lt;input type=&quot;radio&quot; name=&quot;category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt; &lt;?php echo $row['category_name']; ?&gt;&lt;br /&gt;

&lt;?php
}
?&gt;

&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;edit_category&quot; id=&quot;edit_category&quot; value=&quot;Edit selected category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;delete_category&quot; id=&quot;delete_category&quot; value=&quot;Delete selected category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;
&lt;p&gt;Add new category: &lt;input type=&quot;text&quot; name=&quot;new_category&quot; id=&quot;new_category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;add_category&quot; id=&quot;add_category&quot; value=&quot;Add category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;?php
}

mysql_close();
?&gt;</pre>
</li>
<li>The next part we will do is category deletion, as editing is a little bit harder. We will do that part last.
<p>The code to delete entries is very similar to the code to add entries - we will check to see if a value was submitted, and if that value was a number (the category ID). Then we will take that value, and delete the category to which that ID number corresponds.</p>
<p>So, find this part in your code:</p>
<pre>elseif (isset($_POST['delete_category'])) {

}</pre>
<p>As usual, we will add our new code between the braces ( { and } ).</p>
<p>Let&#8217;s make sure the category that was submitted is valid (i.e. a number):</p>
<pre>$category = (int)$_POST['category'];</pre>
<p>Next, let&#8217;s see if a category was actually chosen for deletion (again, we do this after checking that the category is a number, as if it isn&#8217;t it will have been stripped out by the above code and will be equivalent to not having chosen a category to delete).</p>
<pre>if (empty($category)) {
die(&quot;No category chosen! Please go back and choose a category to delete.&quot;);
}</pre>
<p>If the above did not fail, we are sure we have an ID number. Let&#8217;s delete its corresponding category from the database:</p>
<pre>$delete = mysql_query(&quot;DELETE FROM php_blog_categories WHERE `category_id` = $category LIMIT 1&quot;);</pre>
<p>I&#8217;ve added a LIMIT 1 to the end, because although we are meant to have unique category ID numbers, if, for whatever reason you end up with two categories with the same ID number, we only want to delete one of the entries. LIMIT 1 will make sure that this happens.</p>
<p>Now we will test to see if the category was deleted successfully:</p>
<pre>if ($delete == true) echo 'Category successfully deleted.';
else echo 'The category could not be deleted. ' . mysql_error();</pre>
<p>The complete code should now look like this:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

if (isset($_POST['add_category'])) {

$newcat = htmlspecialchars(strip_tags($_POST['new_category']));

if (empty($newcat)) {
die( &quot;No category submitted! Please go back and enter one.&quot;);
}

if (!get_magic_quotes_gpc()) {
$newcat = mysql_real_escape_string($newcat);
}

$insert = mysql_query(&quot;INSERT INTO php_blog_categories (`category_name`) VALUES ('$newcat')&quot;);

if ($insert == true) echo 'Category successfully added.';
else echo 'The category could not be added to the database. ' . mysql_error();

}
elseif (isset($_POST['edit_category'])) {

}
elseif (isset($_POST['delete_category'])) {

$category = (int)$_POST['category'];

if (empty($category)) {
die(&quot;No category chosen! Please go back and choose a category to delete.&quot;);
}

$delete = mysql_query(&quot;DELETE FROM php_blog_categories WHERE `category_id` = $category LIMIT 1&quot;);

if ($delete == true) echo 'Category successfully deleted.';
else echo 'The category could not be deleted. ' . mysql_error();

}
else {

$result = mysql_query(&quot;SELECT * FROM php_blog_categories&quot;);
?&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;&lt;p&gt;

&lt;?php
while($row = mysql_fetch_array($result)) {
?&gt;

&lt;input type=&quot;radio&quot; name=&quot;category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt; &lt;?php echo $row['category_name']; ?&gt;&lt;br /&gt;

&lt;?php
}
?&gt;

&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;edit_category&quot; id=&quot;edit_category&quot; value=&quot;Edit selected category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;delete_category&quot; id=&quot;delete_category&quot; value=&quot;Delete selected category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;
&lt;p&gt;Add new category: &lt;input type=&quot;text&quot; name=&quot;new_category&quot; id=&quot;new_category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;add_category&quot; id=&quot;add_category&quot; value=&quot;Add category&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;?php
}

mysql_close();
?&gt;</pre>
</li>
<li>The next stage is to edit our categories. This is slightly more complicated than the other steps, as we first need to select the category we want to edit, show a form to edit it, then submit the changes to the database.
<p>First, find the only remaining &quot;blank&quot; part of your code (i.e. a part which doesn&#8217;t do anything), which is this part:</p>
<pre>elseif (isset($_POST['edit_category'])) {

}</pre>
<p>Between the {}s we will start our new code.</p>
<p>Let&#8217;s make sure the category is a number and that it is not empty (this is the exact same process we used for the deleting part):</p>
<pre>$category = (int)$_POST['category'];

if (empty($category)) {
die (&quot;No category chosen! Please go back and choose a category to edit.&quot;);
}
</pre>
<p>Next we will get the current category name from the database:</p>
<pre>$result = mysql_query(&quot;SELECT * FROM php_blog_categories WHERE `category_id` = $category LIMIT 1&quot;);
$row = mysql_fetch_array($result);</pre>
<p>Let&#8217;s check that the category actually exists. If it doesn&#8217;t, we will get errors everywhere or a blank page (depends on server settings), so let&#8217;s tell the user and exit the script before this has a chance to happen.</p>
<pre>if (!$row) {
die(&quot;There doesn't seem to be a category with the ID number submitted. Please go back and try again.&quot;);
}</pre>
<p>The !$row part is short for if ($row == false), which would be the case if no results were found in the database corresponding to the ID number we want.</p>
<p>We will close PHP now, so we can use HTML properly to display a form for a new category name.</p>
<pre>?&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;</pre>
<p>We will add a hidden field now. The value of the field will be the ID number of the category, so that the database knows which category this is when the form is submitted; the name of the field will be the section of the script we are in, which is edit_category. This will enable the script to know where we are once the form has submitted.</p>
<pre>&lt;p&gt;&lt;input type=&quot;hidden&quot; name=&quot;edit_category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt;</pre>
<p>Now we will show a field for entering a new category name, along with some text which tells us what the category is currently called.</p>
<pre>The current category name is &lt;?php echo $row['category_name']; ?&gt;. To change it, enter a new name in the box below.&lt;br /&gt;
Rename category to: &lt;input type=&quot;text&quot; name=&quot;new_name&quot; id=&quot;new_name&quot; /&gt;&lt;br /&gt;

&lt;input type=&quot;submit&quot; name=&quot;submit_category_edit&quot; id=&quot;submit_category_edit&quot; value=&quot;Submit new name&quot; /&gt;&lt;/p&gt;</pre>
<p>Finally for this part, we need to reopen PHP for the script to continue:</p>
<pre>&lt;?php</pre>
<p>We have now created our category editing form, but it is of no use without the processing part which we will do next.</p>
<p>Your edit category part should look like this:</p>
<pre>elseif (isset($_POST['edit_category'])) {

$category = (int)$_POST['category'];

if (empty($category)) {
die (&quot;No category chosen! Please go back and choose a category to edit.&quot;);
}

$result = mysql_query(&quot;SELECT * FROM php_blog_categories WHERE `category_id` = $category LIMIT 1&quot;);
$row = mysql_fetch_array($result);

if (!$row) {
die(&quot;There doesn't seem to be a category with the ID number submitted. Please go back and try again.&quot;);
}
?&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;

&lt;p&gt;&lt;input type=&quot;hidden&quot; name=&quot;edit_category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt;

The current category name is &lt;?php echo $row['category_name']; ?&gt;. To change it, enter a new name in the box below.&lt;br /&gt;

Rename category to: &lt;input type=&quot;text&quot; name=&quot;new_name&quot; id=&quot;new_name&quot; /&gt;&lt;br /&gt;
&lt;input type=&quot;submit&quot; name=&quot;submit_category_edit&quot; id=&quot;submit_category_edit&quot; value=&quot;Submit new name&quot; /&gt;&lt;/p&gt;

&lt;?php

}</pre>
<p>The next part will go ABOVE what we have just done. In other words, it will go underneath this line:</p>
<pre>elseif (isset($_POST['edit_category'])) {</pre>
<p>But above this one:</p>
<pre>$category = (int)$_POST['category'];</pre>
<p>What we will do now is check to see if the category has been renamed, and if it has, we will change it in the database. This is almost exactly the same process as the category addition section.</p>
<p>First we will check whether we have pressed to rename our category, or whether want to display the form to rename it (note that there is no closing brace on the end of this line, we will add this once we have finished processing the category).</p>
<pre>if (isset($_POST['submit_category_edit'])) {</pre>
<p>Now we check that the category name is appropriate, by stripping HTML and converting entities:</p>
<pre>$newcat = htmlspecialchars(strip_tags($_POST['new_name']));</pre>
<p>Next, we check to see if a category name was submitted:</p>
<pre>if (empty($newcat)) {
die(&quot;No new category name entered, please go back and try again.&quot;);
}</pre>
<p>Now we will check to see if a category ID was submitted, making sure it is numeric first.</p>
<pre>$id = (int)$_POST['edit_category'];

if (empty($id)) {
die(&quot;Invalid category!&quot;);
}</pre>
<p>Once we are sure we have all the parts we need, we need to update the category details in the database. However, we should escape any quotes first, as we did for the category addition part:</p>
<pre>if (!get_magic_quotes_gpc()) {
$newcat = mysql_real_escape_string($newcat);
}</pre>
<p>This is the code we shall use update the database:</p>
<pre>$edit = mysql_query(&quot;UPDATE php_blog_categories SET `category_name` = '$newcat' WHERE `category_id` = $id LIMIT 1&quot;);

if ($edit == true) echo 'Category successfully edited.';
else echo 'Category could not be edited. ' . mysql_error();</pre>
<p>The last thing we will do is to close the if statement we began earlier, to test whether we were updating the category or not. We do this by adding a closing brace.</p>
<pre>}</pre>
<p>We also need to separate the processing part from the editing form, otherwise we will have both happening at once. So we will add an else statement after the closing brace mentioned above, like so:</p>
<pre>else {</pre>
<p>And then a closing brace just before this line:</p>
<pre>elseif (isset($_POST['delete_category'])) {</pre>
<p>The complete edit block should look like this:</p>
<pre>elseif (isset($_POST['edit_category'])) {

if (isset($_POST['submit_category_edit'])) {

$newcat = htmlspecialchars(strip_tags($_POST['new_name']));

if (empty($newcat)) {
die(&quot;No new category name entered, please go back and try again.&quot;);
}

$id = (int)$_POST['edit_category'];

if (empty($id)) {
die(&quot;Invalid category!&quot;);
}

if (!get_magic_quotes_gpc()) {
$newcat = mysql_real_escape_string($newcat);
}

$edit = mysql_query(&quot;UPDATE php_blog_categories SET `category_name` = '$newcat' WHERE `category_id` = $id LIMIT 1&quot;);

if ($edit == true) echo 'Category successfully edited.';
else echo 'Category could not be edited. ' . mysql_error();

}
else {

$category = (int)$_POST['category'];

if (empty($category)) {
die (&quot;No category chosen! Please go back and choose a category to edit.&quot;);
}

$result = mysql_query(&quot;SELECT * FROM php_blog_categories WHERE `category_id` = $category LIMIT 1&quot;);
$row = mysql_fetch_array($result);

if (!$row) {
die(&quot;There doesn't seem to be a category with the ID number submitted. Please go back and try again.&quot;);
}
?&gt;

&lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;
&lt;p&gt;&lt;input type=&quot;hidden&quot; name=&quot;edit_category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt;

The current category name is &lt;?php echo $row['category_name']; ?&gt;. To change it, enter a new name in the box below.&lt;br /&gt;
Rename category to: &lt;input type=&quot;text&quot; name=&quot;new_name&quot; id=&quot;new_name&quot; /&gt;&lt;br /&gt;

&lt;input type=&quot;submit&quot; name=&quot;submit_category_edit&quot; id=&quot;submit_category_edit&quot; value=&quot;Submit new name&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;

&lt;?php

}

}</pre>
<p>And that&#8217;s the category management part done!</p>
<p>categories.php should look like this when completed:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');

if (isset($_POST['add_category'])) {

    $newcat = htmlspecialchars(strip_tags($_POST['new_category']));

    if (empty($newcat)) {
        die( &quot;No category submitted! Please go back and enter one.&quot;);
    }

    if (!get_magic_quotes_gpc()) {
        $newcat = mysql_real_escape_string($newcat);
    }

    $insert = mysql_query(&quot;INSERT INTO php_blog_categories (`category_name`) VALUES ('$newcat')&quot;);

    if ($insert == true) echo 'Category successfully added.';
    else echo 'The category could not be added to the database. ' . mysql_error();

}
elseif (isset($_POST['edit_category'])) {

    if (isset($_POST['submit_category_edit'])) {

        $newcat = htmlspecialchars(strip_tags($_POST['new_name']));

        if (empty($newcat)) {
            die(&quot;No new category name entered, please go back and try again.&quot;);
        }

        $id = (int)$_POST['edit_category'];

        if (empty($id)) {
            die(&quot;Invalid category!&quot;);
        }

        if (!get_magic_quotes_gpc()) {
            $newcat = mysql_real_escape_string($newcat);
        }

        $edit = mysql_query(&quot;UPDATE php_blog_categories SET `category_name` = '$newcat' WHERE `category_id` = $id LIMIT 1&quot;);

        if ($edit == true) echo 'Category successfully edited.';
        else echo 'Category could not be edited. ' . mysql_error();

    }
    else {

        $category = (int)$_POST['category'];

        if (empty($category)) {
            die (&quot;No category chosen! Please go back and choose a category to edit.&quot;);
        }

        $result = mysql_query(&quot;SELECT * FROM php_blog_categories WHERE `category_id` = $category LIMIT 1&quot;);
        $row = mysql_fetch_array($result);

        if (!$row) {
            die(&quot;There doesn't seem to be a category with the ID number submitted. Please go back and try again.&quot;);
        }
        ?&gt;

        &lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;
        &lt;p&gt;&lt;input type=&quot;hidden&quot; name=&quot;edit_category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt;

        The current category name is &lt;?php echo $row['category_name']; ?&gt;. To change it, enter a new name in the box below.&lt;br /&gt;
        Rename category to: &lt;input type=&quot;text&quot; name=&quot;new_name&quot; id=&quot;new_name&quot; /&gt;&lt;br /&gt;

        &lt;input type=&quot;submit&quot; name=&quot;submit_category_edit&quot; id=&quot;submit_category_edit&quot; value=&quot;Submit new name&quot; /&gt;&lt;/p&gt;
        &lt;/form&gt;

        &lt;?php

    }

}
elseif (isset($_POST['delete_category'])) {

    $category = (int)$_POST['category'];

    if (empty($category)) {
        die(&quot;No category chosen! Please go back and choose a category to delete.&quot;);
    }

    $delete = mysql_query(&quot;DELETE FROM php_blog_categories WHERE `category_id` = $category LIMIT 1&quot;);

    if ($delete == true) echo 'Category successfully deleted.';
    else echo 'The category could not be deleted. ' . mysql_error();

}
else {

    $result = mysql_query(&quot;SELECT * FROM php_blog_categories&quot;);
    ?&gt;
    &lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;&lt;p&gt;

    &lt;?php
    while($row = mysql_fetch_array($result)) {
        ?&gt;

        &lt;input type=&quot;radio&quot; name=&quot;category&quot; value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot; /&gt; &lt;?php echo $row['category_name']; ?&gt;&lt;br /&gt;

        &lt;?php
    }
    ?&gt;

    &lt;/p&gt;
    &lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;edit_category&quot; id=&quot;edit_category&quot; value=&quot;Edit selected category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;delete_category&quot; id=&quot;delete_category&quot; value=&quot;Delete selected category&quot; /&gt;&lt;/p&gt;

    &lt;/form&gt;

    &lt;form action=&quot;categories.php&quot; method=&quot;post&quot;&gt;
    &lt;p&gt;Add new category: &lt;input type=&quot;text&quot; name=&quot;new_category&quot; id=&quot;new_category&quot; /&gt; &lt;input type=&quot;submit&quot; name=&quot;add_category&quot; id=&quot;add_category&quot; value=&quot;Add category&quot; /&gt;&lt;/p&gt;

    &lt;/form&gt;

    &lt;?php
}

mysql_close();
?&gt;</pre>
</li>
<li>
<p>Now we need to implement categories into our new post and edit post pages. We will start with the new post page.</p>
<p>Find the following part in your entry posting page:</p>
<pre>&lt;p&gt;&lt;strong&gt;&lt;label for=&quot;title&quot;&gt;Title:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;title&quot; id=&quot;title&quot; size=&quot;40&quot; /&gt;&lt;/p&gt;</pre>
<p>Above that, we will add our category selection box. By default this may look slightly odd on your page, especially if you have a long category name, but you can easily change this using CSS.</p>
<p>We will start by getting our categories from the database. We don&#8217;t have a database connection on this page yet, so we will insert one here:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password');
mysql_select_db('db_name');</pre>
<p>Let&#8217;s select the categories and display them in a loop:</p>
<pre>$result = mysql_query(&quot;SELECT * FROM php_blog_categories&quot;);

echo '&lt;p&gt;&lt;strong&gt;&lt;label for=&quot;category&quot;&gt;Category:&lt;/label&gt;&lt;/strong&gt; &lt;select name=&quot;category&quot; id=&quot;category&quot;&gt;';

while($row = mysql_fetch_array($result)) { ?&gt;

&lt;option value=&quot;&lt;?php echo $row['category_id']; ?&gt;&quot;&gt;&lt;?php echo $row['category_name']; ?&gt;&lt;/option&gt;

&lt;?php
}
?&gt;

&lt;/select&gt;&lt;/p&gt;</pre>
<p>Now find this part in your post page:</p>
<pre>$entry = nl2br($entry);</pre>
<p>Above that, we will add our category processing, and we will make sure it&#8217;s a number (category ID):</p>
<pre>$category = (int)$_POST['category'];</pre>
<p>Next find this:</p>
<pre>$sql = &quot;INSERT INTO php_blog (timestamp,title,entry) VALUES ('$timestamp','$title','$entry')&quot;;</pre>
<p>(This may look slightly different if you have password protection or custom fields enabled)</p>
<p>You need to add the category field to this query.</p>
<pre>$sql = &quot;INSERT INTO php_blog (timestamp,title,entry,category) VALUES ('$timestamp','$title','$entry','$category')&quot;;</pre>
</li>
<li>
<p>Now that we have added the category to new posts, we need to be able to add it to our edit posts page as well, so we can change the category if needs be.</p>
<p><strong>Please note that you can only have ONE category per entry. This tutorial will not teach you how to give your entries multiple categories (or &quot;tags&quot;).</strong></p>
<p>In your update entry page, find the following code:</p>
<pre>$old_timestamp = $row['timestamp'];
$old_title = stripslashes($row['title']);
$old_entry = stripslashes($row['entry']);
$old_password = $row['password'];</pre>
<p>Add this part to the end:</p>
<pre>$old_category = $row['category'];</pre>
<p>Next, find this part:</p>
<pre>&lt;p&gt;&lt;strong&gt;&lt;label for=&quot;title&quot;&gt;Title:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;title&quot; id=&quot;title&quot; value=&quot;&lt;?php echo $old_title; ?&gt;&quot; size=&quot;40&quot; /&gt;&lt;/p&gt;</pre>
<p>Above that, we will add our categories just like we did to the post entry form. We don&#8217;t need to open a database connection since we already have one, so we will get straight to it. However, we need to make sure the current category of the entry is selected; we will do this by testing each category to see if it matches the category ID stored for that post. If it matches, we will insert the proper HTML to select the current category by default.</p>
<p>Here is the code for the categories:</p>
<pre>&lt;?php
$result2 = mysql_query(&quot;SELECT * FROM php_blog_categories&quot;);

echo '&lt;p&gt;&lt;strong&gt;&lt;label for=&quot;category&quot;&gt;Category:&lt;/label&gt;&lt;/strong&gt; &lt;select name=&quot;category&quot; id=&quot;category&quot;&gt;';

while($row2 = mysql_fetch_array($result2)) { ?&gt;

    &lt;option value=&quot;&lt;?php echo $row2['category_id']; ?&gt;&quot; &lt;?php if ($old_category == $row2['category_id']) echo ' selected=&quot;selected&quot;'; ?&gt;&gt;&lt;?php echo $row2['category_name']; ?&gt;&lt;/option&gt;
    &lt;?php
}
?&gt;
&lt;/select&gt;&lt;/p&gt;</pre>
<p>We need to call this $row2 and $result2 rather than our normal $result and $row, since we already have a $result and $row - those are related to the entry (not the categories), and we don&#8217;t want to overwrite them.</p>
<p>Now we must process the category update. This is identical to what we did to process the category in the new post page - first we look for this part in the code:</p>
<pre>$entry = nl2br($entry);</pre>
<p>Underneath it, we add:</p>
<pre>$category = (int)$_POST['category'];</pre>
<p>Now is where it differs slightly. Find this part:</p>
<pre>$result = mysql_query(&quot;UPDATE php_blog SET timestamp='$timestamp', title='$title', entry='$entry', password='$password' WHERE id='$id' LIMIT 1&quot;) or print (&quot;Can't update entry.&lt;br /&gt;&quot; . mysql_error());</pre>
<p>(Again, this will differ depending on which fields you have in your database, and whether you are using password protection or not)</p>
<p>Let&#8217;s add the category update to that query:</p>
<pre>$result = mysql_query(&quot;UPDATE php_blog SET timestamp='$timestamp', title='$title', entry='$entry', password='$password', category='$category' WHERE id='$id' LIMIT 1&quot;) or print (&quot;Can't update entry.&lt;br /&gt;&quot; . mysql_error());</pre>
<p>And we&#8217;re done!</p>
</li>
<li>But of course we&#8217;re not completely done yet - we want to show the category of a post on our blog front page, and on an individual entry.
<p>To do this, find this part in both your main blog page and your individual entry page (it will appear several times in your individual entry page, and again will look different depending on the fields you have):</p>
<pre>    $title = stripslashes($row['title']);
    $entry = stripslashes($row['entry']);
    $password = $row['password'];</pre>
<p>Underneath this, we need to get our categories. We have them stored by ID in the entries table, so we need to convert them to their names.</p>
<p>To do this, we need to get the name from the categories table, using the ID stored in the entries table, like so:</p>
<pre>$get_categories = mysql_query(&quot;SELECT * FROM php_blog_categories WHERE `category_id` = $row[category]&quot;);
$category = mysql_fetch_array($get_categories);</pre>
<p>Now we are ready to display the category. Find these parts in your blog and individual entry pages (again, this may appear several times:</p>
<pre>&lt;p&gt;&lt;strong&gt;&lt;?php echo $title; ?&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;
        &lt;?php echo $entry; ?&gt;&lt;br /&gt;&lt;br /&gt;

        Posted on &lt;?php echo $date; ?&gt;&lt;/p&gt;</pre>
<p>Just add the category where you want it to show up. For example:</p>
<pre>&lt;p&gt;&lt;strong&gt;&lt;?php echo $title; ?&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;
        &lt;?php echo $entry; ?&gt;&lt;br /&gt;&lt;br /&gt;

        Posted in &lt;?php echo $category['category_name']; ?&gt; on &lt;?php echo $date; ?&gt;&lt;/p&gt;</pre>
</li>
<li>&#8230;You probably want to show all the posts in one category now, don&#8217;t you? Well if you do, you can change the code above to link the category to its archives, like so:
<pre>Posted in &lt;a href=&quot;category.php?category=&lt;?php echo $row['category']; ?&gt;&quot;&gt;&lt;?php echo $category['category_name']; ?&gt;&lt;/a&gt; on &lt;?php echo $date; ?&gt;</pre>
<p>We will now make a category archive page, which will look pretty much exactly like the yearly archive page. We will call this page category.php (note that this is different from your category admin page, which is called <strong>categories.php</strong>. You can of course rename these files to anything you like, but in this example I will use category.php as the category archive page.</p>
<p>Let&#8217;s start with the MySQL connection:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password') ;
mysql_select_db('db_name');</pre>
<p>We now need to check what category we are displaying, which is decided by the ?category= part in the URL. If someone has gone directly to category.php without specifying a category to display, we need to display an error just like we do in the single entry page and the archive page, when the person hasn&#8217;t specified a year or entry ID to look at.</p>
<pre>if (!isset($_GET['category'])) {
    die(&quot;Invalid category specified.&quot;);
}
else {
    $category = (int)$_GET['category'];
}</pre>
<p>Now that we have the category we want to display entries from, let&#8217;s collect the posts from the entries table:</p>
<pre>$result = mysql_query(&quot;SELECT timestamp, id, title FROM php_blog WHERE category = $category ORDER BY id DESC&quot;);</pre>
<p>We will now add up how many posts were found:</p>
<pre>$num = mysql_num_rows($result);</pre>
<p>&#8230;and get the category name from the categories table:</p>
<pre>$get_category = mysql_query(&quot;SELECT * FROM php_blog_categories WHERE category_id = $category&quot;);
$get_category2 = mysql_fetch_array($get_category);</pre>
<p>Now we can use those details as a header, for example you might want to say something like this:</p>
<pre>echo &quot;&lt;h1&gt;There are $num posts in the &amp;quot;$get_category2[category_name]&amp;quot;category&lt;/h1&gt;&quot;</pre>
<p>Let&#8217;s now show the posts. This is identical to the archive page entry code.</p>
<pre>while ($row = mysql_fetch_array($result)) {
    $date = date(&quot;l F d Y&quot;, $row['timestamp']);
    $id = $row['id'];
    $title = stripslashes($row['title']);

    ?&gt;

    &lt;p&gt;&lt;?php echo $date; ?&gt;&lt;br /&gt;&lt;a href=&quot;journal.php?id=&lt;?php echo $id; ?&gt;&quot;&gt;&lt;?php echo $title; ?&gt;&lt;/a&gt;&lt;/p&gt;

    &lt;?php
}

?&gt;</pre>
<p>That&#8217;s the category archive done. The complete code should look like this:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password') ;
mysql_select_db('db_name');

if (!isset($_GET['category'])) {
    die(&quot;Invalid category specified.&quot;);
}
else {
    $category = (int)$_GET['category'];
}

$result = mysql_query(&quot;SELECT timestamp, id, title FROM php_blog WHERE category = $category ORDER BY id DESC&quot;);

$num = mysql_num_rows($result);

$get_category = mysql_query(&quot;SELECT * FROM php_blog_categories WHERE category_id = $category&quot;);
$get_category2 = mysql_fetch_array($get_category);

echo &quot;&lt;h1&gt;There are $num posts in the &amp;quot;$get_category2[category_name]&amp;quot;category&lt;/h1&gt;&quot;;

while($row = mysql_fetch_array($result)) {
    $date = date(&quot;l F d Y&quot;, $row['timestamp']);
    $id = $row['id'];
    $title = stripslashes($row['title']);

    ?&gt;

    &lt;p&gt;&lt;?php echo $date; ?&gt;&lt;br /&gt;&lt;a href=&quot;journal.php?id=&lt;?php echo $id; ?&gt;&quot;&gt;&lt;?php echo $title; ?&gt;&lt;/a&gt;&lt;/p&gt;
    &lt;?php
}

?&gt;</pre>
</li>
<li>
<p>You may want a little block to display all your categories and the number of posts in them, just like for the yearly archives. If you&#8217;d like to do this, here is how.</p>
<p>First, let&#8217;s connect to the database:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password') ;
mysql_select_db('db_name');</pre>
<p>Next, let&#8217;s get our categories from the database:</p>
<pre>$result1 = mysql_query(&quot;SELECT * FROM php_blog_categories ORDER BY category_name ASC&quot;);</pre>
<p>Now let&#8217;s display the categories and link them to the archives:</p>
<pre>while($row = mysql_fetch_array($result1)) {</pre>
<p>Ah, before we can do that, we need our posts! We need to get them while inside the loop, as we need a different post count for each category.</p>
<pre>$result2 = mysql_query(&quot;SELECT COUNT(`id`) AS entries FROM php_blog WHERE category = $row[category_id]&quot;);
$num_entries = mysql_fetch_array($result2);</pre>
<p><em>Now</em> we can proceed with showing the posts!</p>
<pre>    echo '&lt;a href=&quot;category.php?category=' . $row['category_id'] . '&quot;&gt;' . $row['category_name'] . '&lt;/a&gt; (' . $num_entries['entries'] . ')&lt;br /&gt;';</pre>
<p>Let&#8217;s close that while loop and PHP in general:</p>
<pre>}
?&gt;</pre>
<p>Your category archive block should look like this:</p>
<pre>&lt;?php
mysql_connect('localhost', 'db_username', 'db_password') ;
mysql_select_db('db_name');

$result1 = mysql_query(&quot;SELECT * FROM php_blog_categories ORDER BY category_name ASC&quot;);

while($row = mysql_fetch_array($result1)) {

    $result2 = mysql_query(&quot;SELECT COUNT(`id`) AS entries FROM php_blog WHERE category = $row[category_id]&quot;);
    $num_entries = mysql_fetch_array($result2);

    echo '&lt;a href=&quot;category.php?category=' . $row['category_id'] . '&quot;&gt;' . $row['category_name'] . '&lt;/a&gt; (' . $num_entries['entries'] . ')&lt;br /&gt;';

}
?&gt;</pre>
</li>
</ol>
<p>And that&#8217;s it! Now you should have categories fully enabled in your blog!</p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/part-11-categories/feed</wfw:commentRss>
		</item>
		<item>
		<title>Randomize&#8230; Pretty much anything!</title>
		<link>http://codegrrl.com/tutorials/randomize-pretty-much-anything</link>
		<comments>http://codegrrl.com/tutorials/randomize-pretty-much-anything#comments</comments>
		<pubDate>Tue, 11 Apr 2006 12:42:30 +0000</pubDate>
		<dc:creator>Valerie</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=66</guid>
		<description><![CDATA[So&#8230; you want a random background image? Or a random stylesheet, even? Try this out - it&#8217;s quick, easy - and will use PHP and CSS.
First you, of course, should be able to use PHP on your site. You should also know a little bit about CSS. Those are the two things this technique will [...]]]></description>
			<content:encoded><![CDATA[<p>So&#8230; you want a random background image? Or a random stylesheet, even? Try this out - it&#8217;s quick, easy - and will use PHP and CSS.</p>
<p><span id="more-66"></span>First you, of course, should be able to use PHP on your site. You should also know a little bit about CSS. Those are the two things this technique will use. There are numerous ways this can be accomplished but this is the way I am currently using it on my blog site.</p>
<p>First, I have the area where I want the images to be randomized. This is my header image. I have a div for that and it has both an ID and a Class. This is what it looks like before I randomize it:</p>
<pre>&lt;div id="random" class="random"&gt; &lt;/div&gt;</pre>
<p>I use the ID to define all the basics and styles other than the image to get that out of the way and free the Class up for the random background image.</p>
<p>Next, add in your random PHP snippet that will essentially change the Class with each page load:</p>
<pre>&lt;?php echo(rand(1,X)); ?&gt;</pre>
<p>So now the whole thing looks like this:</p>
<pre>&lt;div id="random" class="random&lt;?php echo(rand(1,X)); ?&gt;"&gt; &lt;/div&gt;</pre>
<p>Just change that &#8220;X&#8221; to however many images you plan to have be it 5 or 20, it is your maximum value that you want outputted. When you load the page, that random PHP snippet will pick a number between 1 and that number, so you will end up with class=&#8221;random2&#8243; or class=&#8221;random5&#8243; and so on. Of course, if you want to get technical, you can also change that &#8220;1&#8243; to something, so as to randomize between, say, 3 and 7, etc.</p>
<p>Now you need to tell it what to do with those classes. In your CSS, define what each and every one of those classes will do. If you&#8217;ve got three images you want to rotate between, you might do something like this:</p>
<pre>.random1 {background: url(images/image1.jpg);}
.random2 {background: url(images/image2.jpg);}
.random3 {background: url(images/image3.jpg);}</pre>
<p>See? Easy!</p>
<p>That&#8217;s just the bare minimum, you could even use the CSS to randomize anything: different colored text, different margins, padding, borders, etc. If you really wanted to waste a lot of time, you could use that PHP snippet in your call for a stylesheet and load a completely different stylesheet each time, you would just start your stylesheet code and put the PHP code somewhere inside the href call for the stylesheet and have your stylesheets appropriately named. Of course, that might make your visitors want to pluck out their eyes, but you get the idea!</p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/randomize-pretty-much-anything/feed</wfw:commentRss>
		</item>
		<item>
		<title>Skinning: Customising your skins page</title>
		<link>http://codegrrl.com/tutorials/skinning-customising-your-skins-page</link>
		<comments>http://codegrrl.com/tutorials/skinning-customising-your-skins-page#comments</comments>
		<pubDate>Fri, 20 May 2005 12:48:46 +0000</pubDate>
		<dc:creator>Vixx</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=67</guid>
		<description><![CDATA[If you have skinned your site and offer visitors several different themes to pick from, this tutorial will show you how to customise your skins.php page so that it changes the information about the current layout according to what skin your visitor is using.
Although this means we have to use cookies and a little PHP, [...]]]></description>
			<content:encoded><![CDATA[<p>If you have <a href="/tutorials/skinning-your-site">skinned</a> your site and offer visitors several different themes to pick from, this tutorial will show you how to customise your skins.php page so that it changes the information about the current layout according to what skin your visitor is using.</p>
<p>Although this means we have to use cookies and a little PHP, you don&#8217;t need to know much about them to implement this . . . although it should go without saying that you should have already implemented my skinning tutorial successfully for this to work!</p>
<p><span id="more-67"></span>Please note that I have not tested this method with any skinning tutorial other than the one I wrote myself here at CodeGrrl, but there isn&#8217;t a reason why this shouldn&#8217;t work with any other skinning method as they&#8217;re all essentially the same.</p>
<h3>Step One:</h3>
<p>On my own version of skins.php I have a small introduction about skins and some screencaps to show visitors what the layouts are like before you reach the heading &#8220;About This Layout&#8221;, but you can actually place this anywhere you like on the page. So wherever you want your layout information to display, add this:</p>
<pre>&lt;?php
if ($_COOKIE['skin'] == 1) {
echo &quot;&lt;h2&gt;About This Layout&lt;/h2&gt;
&lt;p&gt;You're currently viewing theme 1 entitled \&quot;NAME OF SKIN\&quot; which was uploaded on DATE OF UPLOAD.&lt;/p&gt;";
}
?&gt;</pre>
<p>You can add as much information as you like - as you can see on my site, I talk about what resolutions and browsers the skin is optimised for, credits for images, validation details etc. etc. You can also use <acronym title="HyperText Mark-up Language">HTML</acronym> as normal, although you will need to remember that any &quot;s (speechmarks) need to be prefixed with a backslash ( \ ) in order for the page to display properly. For instance, if you have a link like:</p>
<pre>&lt;a href=&quot;http://furious-angel.com&quot;&gt;Click here!&lt;/a&gt;</pre>
<p>You will need to change it to:</p>
<pre>&lt;a href=\&quot;http://furious-angel.com\&quot;&gt;Click here!&lt;/a&gt;</pre>
<h3>Step Two:</h3>
<p>Add the snippet in Step One for every skin/layout you have, changing</p>
<pre>if ($_COOKIE['skin'] == 1)</pre>
<p>to:</p>
<pre>elseif ($_COOKIE['skin'] == 2)</pre>
<p>and so on, according to whichever skin number you&#8217;re referring to.</p>
<h3>Step Three:</h3>
<p>That&#8217;s it - all done! <img src='http://codegrrl.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> You should now have a skins.php that displays only one &quot;About This Layout&quot; section that changes according to what skin number your visitor has selected!</p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/skinning-customising-your-skins-page/feed</wfw:commentRss>
		</item>
		<item>
		<title>Part 10: Deleting entries</title>
		<link>http://codegrrl.com/tutorials/part-10-deleting-entries</link>
		<comments>http://codegrrl.com/tutorials/part-10-deleting-entries#comments</comments>
		<pubDate>Tue, 04 Jan 2005 12:53:12 +0000</pubDate>
		<dc:creator>Valerie</dc:creator>
		
		<category><![CDATA[Build A Blog]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=68</guid>
		<description><![CDATA[My turn!  Okay, this is my attempt at writing my first tutorial.
A lot of people have been asking how to delete the entries out of build-a-blog.  So that is what this tutorial will cover.  You should have followed all of the parts of the tutorial so far up to this point, this [...]]]></description>
			<content:encoded><![CDATA[<p>My turn!  Okay, this is my attempt at writing my first tutorial.</p>
<p>A lot of people have been asking how to delete the entries out of build-a-blog.  So that is what this tutorial will cover.  You should have followed all of the parts of the tutorial so far up to this point, this tutorial will assume that you have.</p>
<p><span id="more-68"></span>Let&#8217;s open our update.php, or whatever file you are using to change entries after they&#8217;ve been posted.</p>
<p>Since we&#8217;ve already got the rest of the code for this file, let&#8217;s find the part that looks something like this, towards the end of your file:</p>
<pre>&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;update&quot; id=&quot;update&quot; value=&quot;Update&quot; /&gt;&lt;/p&gt;

&lt;/form&gt;</pre>
<p>Optional: right after that, let&#8217;s put a little warning message&#8230;</p>
<pre>&lt;p&gt;&lt;strong&gt;Before deleting, be absolutely sure - there is no confirmation nor is there any way to reverse deletion!&lt;/strong&gt;&lt;br /&gt;
&lt;small&gt;(You may be shown your entry again after deleting - do not worry, it HAS been deleted.  Check the main page of the blog if you are still unsure.&lt;/small&gt;&lt;/p&gt;</pre>
<p>This is just a little note to remind you that once you will click the button, your entry will be gone.  There will be no confirmation, no box asking you &quot;are you sure?&quot;  That can be written in later.</p>
<p>Now we&#8217;ve got to write the actual code to delete the entry.<br />
This is a new form, and like the update form, it will use the same page to process what you want it to do.</p>
<pre>&lt;form action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot; method=&quot;post&quot;&gt;</pre>
<p>We need to pass the variable of the id (if you remember, the id is passed to the update form from the list of entries you clicked on to get there, so we&#8217;ll need to do the same thing here):</p>
<pre>&lt;input type=&quot;hidden&quot; name=&quot;id&quot; id=&quot;id&quot; value=&quot;&lt;?php echo $id; ?&gt;&quot; /&gt;</pre>
<p>And we want to submit our decision, aka delete it.</p>
<pre>&lt;input type=&quot;submit&quot; name=&quot;delete&quot; id=&quot;delete&quot; value=&quot;Yes, I am absolutely and positively sure I want to delete this entry.&quot; /&gt;

&lt;/form&gt;</pre>
<p>Of course, your button can say whatever you want it to, just change the part for the value - just don&#8217;t use apostrophes or quotes.</p>
<p>Now, to process the deletion.<br />
Let&#8217;s skip over the part from the original update form that starts as follows:</p>
<pre>    if (isset($_POST['update'])) {</pre>
<p>And closes like so:</p>
<pre>    header("Location: journal.php?id=" . $id);

}</pre>
<p>After that, skip a line and start your process.  Notice that we gave our above submit button a name of &quot;delete&quot; this is where that comes into play.</p>
<pre>if (isset($_POST['delete'])) {</pre>
<p>Let&#8217;s get the id from the form:</p>
<pre>    $id = (int)$_POST['id'];</pre>
<p>Next, what should PHP do with the it?</p>
<pre>    $result = mysql_query(&quot;DELETE FROM php_blog WHERE id='$id'&quot;) or print (&quot;Can't delete entry.&lt;br /&gt;&quot; . mysql_error());</pre>
<p>The above will also give you the output of an error if deletion is unsuccessful for any reason.</p>
<p>Now what do we do if deletion is successful?  If the result of deletion is != (not equal to) false, then we say so. We&#8217;re also telling the code to &quot;exit&quot; here - without this, an error would occur after deletion. You can include a link back to your main admin page here if you like.</p>
<pre>    if ($result != false) {
        print &quot;The entry has been successfully deleted from the database.&quot;;
        exit;
    }
}</pre>
<p>The end!</p>
<p>Okay, just kidding, let&#8217;s wrap it up.  What does it look like now?<br />
You should have two parts: part 1 is after the update form, before processing that form, and will contain your delete form; part 2 is after processing the update and it will process the delete form.</p>
<p>Your complete update.php should look more or less like this:</p>
<pre>&lt;?php
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');

if (isset($_POST['update'])) {

    $id = htmlspecialchars(strip_tags($_POST['id']));
    $month = htmlspecialchars(strip_tags($_POST['month']));
    $date = htmlspecialchars(strip_tags($_POST['date']));
    $year = htmlspecialchars(strip_tags($_POST['year']));
    $time = htmlspecialchars(strip_tags($_POST['time']));
    $entry = $_POST['entry'];
    $title = htmlspecialchars(strip_tags($_POST['title']));
    if (isset($_POST['password'])) $password = htmlspecialchars(strip_tags($_POST['password']));
    else $password = "";

    $entry = nl2br($entry);

    if (!get_magic_quotes_gpc()) {
        $title = addslashes($title);
        $entry = addslashes($entry);
    }

    $timestamp = strtotime ($month . " " . $date . " " . $year . " " . $time);

    $result = mysql_query(&quot;UPDATE php_blog SET timestamp='$timestamp', title='$title', entry='$entry', password='$password' WHERE id='$id' LIMIT 1&quot;&#041; or print (&quot;Can't update entry.&lt;br /&gt;&quot; . mysql_error());

    header(&quot;Location: journal.php?id=&quot; . $id);

}

if (isset($_POST['delete'])) {
    $id = (int)$_POST['id'];
    $result = mysql_query(&quot;DELETE FROM php_blog WHERE id='$id'&quot;) or print (&quot;Can't delete entry.&lt;br /&gt;&quot; . mysql_error());
    if ($result != false) {
        print &quot;The entry has been successfully deleted from the database.&quot;;
        exit;
    }
}

if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) {
    die(&quot;Invalid entry ID.&quot;&#041;;
}
else {
    $id = (int)$_GET['id'];
}

$result = mysql_query (&quot;SELECT * FROM php_blog WHERE id='$id'&quot;&#041; or print (&quot;Can't select entry.&lt;br /&gt;&quot; . $sql . &quot;&lt;br /&gt;&quot; . mysql_error());

while ($row = mysql_fetch_array($result)) {
    $old_timestamp = $row['timestamp'];
    $old_title = stripslashes($row['title']);
    $old_entry = stripslashes($row['entry']);
    $old_password = $row['password'];

    $old_title = str_replace('&quot;','\'',$old_title);
    $old_entry = str_replace('&lt;br /&gt;', '', $old_entry);

    $old_month = date(&quot;F&quot;,$old_timestamp);
    $old_date = date(&quot;d&quot;,$old_timestamp);
    $old_year = date(&quot;Y&quot;,$old_timestamp);
    $old_time = date(&quot;H:i&quot;,$old_timestamp);
}
?&gt;

&lt;form method=&quot;post&quot; action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot;&gt;

&lt;p&gt;&lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;&lt;?php echo $id; ?&gt;&quot; /&gt;

&lt;strong&gt;&lt;label for=&quot;month&quot;&gt;Date (month, day, year):&lt;/label&gt;&lt;/strong&gt; 

&lt;select name=&quot;month&quot; id=&quot;month&quot;&gt;
&lt;option value=&quot;&lt;?php echo $old_month; ?&gt;&quot;&gt;&lt;?php echo $old_month; ?&gt;&lt;/option&gt;

&lt;option value=&quot;January&quot;&gt;January&lt;/option&gt;
&lt;option value=&quot;February&quot;&gt;February&lt;/option&gt;
&lt;option value=&quot;March&quot;&gt;March&lt;/option&gt;
&lt;option value=&quot;April&quot;&gt;April&lt;/option&gt;

&lt;option value=&quot;May&quot;&gt;May&lt;/option&gt;
&lt;option value=&quot;June&quot;&gt;June&lt;/option&gt;
&lt;option value=&quot;July&quot;&gt;July&lt;/option&gt;
&lt;option value=&quot;August&quot;&gt;August&lt;/option&gt;

&lt;option value=&quot;September&quot;&gt;September&lt;/option&gt;
&lt;option value=&quot;October&quot;&gt;October&lt;/option&gt;
&lt;option value=&quot;November&quot;&gt;November&lt;/option&gt;
&lt;option value=&quot;December&quot;&gt;December&lt;/option&gt;

&lt;/select&gt;

&lt;input type=&quot;text&quot; name=&quot;date&quot; id=&quot;date&quot; size=&quot;2&quot; value=&quot;&lt;?php echo $old_date; ?&gt;&quot; /&gt;

&lt;select name=&quot;year&quot; id=&quot;year&quot;&gt;
&lt;option value=&quot;&lt;?php echo $old_year; ?&gt;&quot;&gt;&lt;?php echo $old_year; ?&gt;&lt;/option&gt;
&lt;option value=&quot;2004&quot;&gt;2004&lt;/option&gt;

&lt;option value=&quot;2005&quot;&gt;2005&lt;/option&gt;
&lt;option value=&quot;2006&quot;&gt;2006&lt;/option&gt;
&lt;option value=&quot;2007&quot;&gt;2007&lt;/option&gt;
&lt;option value=&quot;2008&quot;&gt;2008&lt;/option&gt;

&lt;option value=&quot;2009&quot;&gt;2009&lt;/option&gt;
&lt;option value=&quot;2010&quot;&gt;2010&lt;/option&gt;
&lt;/select&gt;

&lt;strong&gt;&lt;label for=&quot;time&quot;&gt;Time:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;time&quot; id=&quot;time&quot; size=&quot;5&quot; value=&quot;&lt;?php echo $old_time; ?&gt;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;label for=&quot;title&quot;&gt;Title:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;title&quot; id=&quot;title&quot; value=&quot;&lt;?php echo $old_title; ?&gt;&quot; size=&quot;40&quot; /&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;label for=&quot;password&quot;&gt;Password protect?&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;checkbox&quot; name=&quot;password&quot; id=&quot;password&quot; value=&quot;1&quot;&lt;?php if($old_password == 1) echo &quot; checked=\&quot;checked\&quot;&quot;; ?&gt; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;textarea cols=&quot;80&quot; rows=&quot;20&quot; name=&quot;entry&quot; id=&quot;entry&quot;&gt;&lt;?php echo $old_entry; ?&gt;&lt;/textarea&gt;&lt;/p&gt;

&lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;update&quot; id=&quot;update&quot; value=&quot;Update&quot;&gt;&lt;/p&gt;

&lt;/form&gt;

&lt;p&gt;&lt;strong&gt;Before deleting, be absolutely sure - there is no confirmation nor is there any way to reverse deletion!&lt;/strong&gt;&lt;br /&gt;
&lt;small&gt;(You may be shown your entry again after deleting - do not worry, it HAS been deleted.  Check the main page of the blog if you are still unsure.&lt;/small&gt;&lt;/p&gt;

&lt;form action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot; method=&quot;post&quot;&gt;

&lt;input type=&quot;hidden&quot; name=&quot;id&quot; id=&quot;id&quot; value=&quot;&lt;?php echo $id; ?&gt;&quot; /&gt;
&lt;input type=&quot;submit&quot; name=&quot;delete&quot; id=&quot;delete&quot; value=&quot;Yes, I am absolutely and positively sure I want to delete this entry.&quot; /&gt;

&lt;/form&gt;

&lt;?php

mysql_close();
?&gt;</pre>
<p>Now we should be all done.  <img src='http://codegrrl.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/part-10-deleting-entries/feed</wfw:commentRss>
		</item>
		<item>
		<title>Part 9: Creating a form to edit and delete comments</title>
		<link>http://codegrrl.com/tutorials/part-9-creating-a-form-to-edit-and-delete-comments</link>
		<comments>http://codegrrl.com/tutorials/part-9-creating-a-form-to-edit-and-delete-comments#comments</comments>
		<pubDate>Thu, 30 Dec 2004 12:58:52 +0000</pubDate>
		<dc:creator>Julie</dc:creator>
		
		<category><![CDATA[Build A Blog]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=69</guid>
		<description><![CDATA[As you may or may not know, Michelle is currently unable to add to the Build A Blog series of tutorials, so I thought I&#8217;d try my hand at writing a new instalment.  I can&#8217;t promise it will be as good as her tutorials, but I&#8217;ll do my best!  This tutorial covers the [...]]]></description>
			<content:encoded><![CDATA[<p>As you may or may not know, Michelle is currently unable to add to the Build A Blog series of tutorials, so I thought I&#8217;d try my hand at writing a new instalment.  I can&#8217;t promise it will be as good as her tutorials, but I&#8217;ll do my best!  This tutorial covers the creation of an admin page for editing and deleting user comments.</p>
<p><span id="more-69"></span>This code is based on Michelle&#8217;s tutorial for <a href="/tutorials/part-6-creating-a-form-to-edit-entries">creating a form to edit entries</a>.  I&#8217;ve also followed her style of coding to keep this tutorial similar to the previous ones.</p>
<p>Before following this tutorial, you need to set up comments by following <a href="/tutorials/part-7-allowing-reader-comments">Part 7: Allowing Reader Comments</a>.</p>
<p>Let&#8217;s start by creating a page called editcomments.php.  This page needs to go in the same password protected directory as your blog entry and edit pages to keep it secure.</p>
<p>First, open PHP:</p>
<pre>&lt;?php</pre>
<p>Connect to your database (change the values as applicable):</p>
<pre>mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');</pre>
<p>Now, we will write an if statement to check if the edit form (which we&#8217;ll create further down the page) has been submitted, and if so, to process it by updating the database with the new posted info. Here we will also strip out HTML as a security measure:</p>
<pre>if (isset($_POST['edit'])) {
    $name = htmlspecialchars(strip_tags($_POST['name']));
    $email = htmlspecialchars(strip_tags($_POST['email']));
    $url = htmlspecialchars(strip_tags($_POST['url']));
    $comment = htmlspecialchars(strip_tags($_POST['comment']));
    $comment = nl2br($comment);
    $id = (int)$_POST['id'];

    if (!get_magic_quotes_gpc()) {
        $name = addslashes($name);
        $url = addslashes($url);
        $comment = addslashes($comment);
    }

    $result = mysql_query(&quot;UPDATE php_blog_comments SET name='$name', email='$email', url='$url', comment='$comment' WHERE id='$id' LIMIT 1&quot;) or print (&quot;Can't update comment.&lt;br /&gt;&quot; . $result . &quot;&lt;br /&gt;&quot; . mysql_error());
    if ($result != false) {
        print &quot;&lt;p&gt;The comment has successfully been edited!&lt;/p&gt;&quot;;
    }
}</pre>
<p>Next, another if statement to check if the delete form (again, we&#8217;ll create it further down the page) has been submitted, and if so, to process it by deleting the comment from the database and posting a success message:</p>
<pre>if(isset($_POST['delete'])) {
    $id = (int)$_POST['id'];
    $result = mysql_query(&quot;DELETE FROM php_blog_comments WHERE id='$id' LIMIT 1&quot;) or print (&quot;Can't delete comment.&lt;br /&gt;&quot; . $result . &quot;&lt;br /&gt;&quot; . mysql_error());
    if ($result != false) {
        print &quot;&lt;p&gt;The comment has successfully been deleted!&lt;/p&gt;&quot;;
    }
}</pre>
<p>Just like the edit entries page, the form to edit a comment only shows up if the url is http://yourdomain.com/dir/editcomments.php?id=xx where xx is the id number of the comment.  So, now we will write an if statement to check if that variable $id has been passed through the URL, and whether it&#8217;s a valid ID (i.e. it&#8217;s not zero or a letter):</p>
<pre>if (isset($_GET['id']) &#038;&#038; !empty($_GET['id']) &#038;&#038; is_numeric($_GET['id'])) {</pre>
<p>Take note that it&#8217;s necessary here to use $&#095;GET['id'] instead of just $id, even if you have register&#095;globals on.  $&#095;GET holds the variables passed through the URL and not those from forms, cookies, or other methods.  For this particular statement, we only want to check for a query string (URL) variable here, so it&#8217;s important to make the distinction.</p>
<p>So, if $_GET['id'] is set, that means we can show the edit form for the comment with that id number.  First we select the comment from the database:</p>
<pre>$result = mysql_query (&quot;SELECT * FROM php_blog_comments WHERE id='$_GET[id]'&quot;) or print (&quot;Can't select comment.&lt;br /&gt;&quot; . mysql_error());</pre>
<p>Then use a while loop to set variables for the existing comment data:</p>
<pre>while ($row = mysql_fetch_array($result)) {
      $old_name = stripslashes($row['name']);
      $old_email = $row['email'];
      $old_url = stripslashes($row['url']);
      $old_comment = stripslashes($row['comment']);
      $old_comment = str_replace('&lt;br /&gt;', '', $old_comment);
}</pre>
<p>And now the form, which we will fill with the values we just set.  Note that there are three submit buttons: one to save changes, one to delete the comment, and one to cancel with no changes made (it just takes you back to the edit comments menu, which we&#8217;ll create further down in the tutorial). Here we close PHP for a bit so that we can add some normal <acronym title="HyperText Mark-up Language">HTML</acronym>, but we will reopen it as and when needed.:</p>
<pre>?&gt;
&lt;form method=&quot;post&quot; action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot;&gt;
    &lt;p&gt;&lt;input type=&quot;hidden&quot; name=&quot;id&quot; id=&quot;id&quot; value=&quot;&lt;?php echo $_GET['id']; ?&gt;&quot;&gt;

    &lt;strong&gt;&lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; size=&quot;40&quot; value=&quot;&lt;?php echo $old_name; ?&gt;&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;&lt;label for=&quot;email&quot;&gt;E-mail:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; size=&quot;40&quot; value=&quot;&lt;?php echo $old_email; ?&gt;&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;&lt;label for=&quot;url&quot;&gt;URL:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;url&quot; id=&quot;url&quot; size=&quot;40&quot; value=&quot;&lt;?php echo $old_url; ?&gt;&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;&lt;label for=&quot;comment&quot;&gt;Comment:&lt;label&gt;&lt;/strong&gt;&lt;br /&gt;
    &lt;textarea cols=&quot;80&quot; rows=&quot;20&quot; name=&quot;comment&quot; id=&quot;comment&quot;&gt;&lt;?php echo $old_comment; ?&gt;&lt;/textarea&gt;&lt;/p&gt;

    &lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;edit&quot; id=&quot;edit&quot; value=&quot;Save Changes&quot;&gt; &lt;input type=&quot;submit&quot; name=&quot;delete&quot; id=&quot;delete&quot; value=&quot;Delete Comment&quot;&gt; &lt;input type=&quot;submit&quot; value=&quot;Never Mind&quot;&gt;&lt;/p&gt;

&lt;/form&gt;
&lt;?php</pre>
<p>Now for the else that goes with the if $&#095;GET['id'] above.  So everything below this is now if $&#095;GET['id'] is <em>not</em> set:</p>
<pre>}
else {</pre>
<p>What we are creating now is the edit comments menu that shows up by default if you just go to editcomments.php with no ?id=xx.  It involves three database queries - the second and third being nested inside the first.  The first query selects from the comments table the id of each blog entry that has comments.  The second query then gets the date and title of the entry from the blog table.  The third query gets the comments from that entry.  Here it goes&#8230;</p>
<p>The first query.  The limit is set to 10 entries worth of comments to keep the page from being too long, but you can change it to whatever number you like, or just remove the limit completely:</p>
<pre>$result = mysql_query(&quot;SELECT entry AS get_group FROM php_blog_comments GROUP BY get_group DESC LIMIT 10&quot;) or print (&quot;Can't select comments.&lt;br /&gt;&quot; . $result . &quot;&lt;br /&gt;&quot; . mysql_error());</pre>
<p>Now start the while loop and set the value of $get_group, which in this case is the id number of an entry.  Then open a paragraph to hold each entry&#8217;s comment links (to keep things neat).  We don&#8217;t close the while loop yet because the next two are nested inside this one&#8230; we&#8217;ll close it later on:</p>
<pre>while($row = mysql_fetch_array($result)) {
     $get_group = $row['get_group'];

     print &quot;&lt;p&gt;&quot;;</pre>
<p>The second query, to get the timestamp and title from the blog table.  Inside the while loop, we format the date and then print the date and title.  This while loop can be closed here, because the third query doesn&#8217;t depend on it.</p>
<pre>    $result2 = mysql_query(&quot;SELECT timestamp, title FROM php_blog WHERE id='$get_group'&quot;);
    while($row2 = mysql_fetch_array($result2)) {
        $date = date(&quot;l F d Y&quot;,$row2['timestamp']);
        $title = stripslashes($row2['title']);
        print &quot;&lt;strong&gt;&quot; . $date . &quot; - &quot; . $title . &quot;&lt;/strong&gt;&lt;br /&gt;&quot;;
    }</pre>
<p>Now the final query to get the comment info so we can format it to be linked to the edit page.  Don&#8217;t close this while loop just yet, there&#8217;s more:</p>
<pre>    $result3 = mysql_query(&quot;SELECT * FROM php_blog_comments WHERE entry='$get_group' ORDER BY timestamp DESC&quot;);
    while($row3 = mysql_fetch_array($result3)) {
        $id = $row3['id'];
        $name = stripslashes($row3['name']);
        $comment = stripslashes($row3['comment']);
        $date = date(&quot;l F d Y&quot;,$row3['timestamp']);</pre>
<p>We want to know which comment we are clicking on to edit, but if we displayed each comment in full, the page could get quite lengthy.  So, we can check to see if the comment is longer than 75 characters, and if it is, trim it and put an ellipsis on the end. We&#8217;re also stripping line breaks in order to keep the size down as well:</p>
<pre>        if (strlen($comment) > 75 || strstr($comment, &quot;&lt;br /&gt;&quot;) || strstr($comment, &quot;\n&quot;)) {
            $comment = substr($comment,0,75) . &quot;...&quot;;
            $comment = str_replace(&quot;&lt;br /&gt;&quot;, &quot;&quot;, $comment);
            $comment = str_replace(&quot;\n&quot;, &quot; &quot;, $comment);
        }</pre>
<p>Now print the comment (or comment snippet), linked to the edit form for that comment:</p>
<pre>        print "&lt;a href=\&quot;editcomments.php?id=&quot; . $id . &quot;\&quot;>&quot; . $comment . &quot;&lt;/a&gt;&lt;br /&gt;Comment by &quot; . $name . &quot; @ &quot; . $date;</pre>
<p>Close the paragraph, the third query&#8217;s while loop, the first query&#8217;s while loop and then the else part we opened earlier:</p>
<pre>        print &quot;&lt;/p&gt;&quot;;

    }
}
}</pre>
<p>Then close the database connection:</p>
<pre>mysql_close();</pre>
<p>And finally, close PHP:</p>
<pre>?&gt;</pre>
<p>Here&#8217;s the whole code for the editcomments.php page:</p>
<pre>&lt;?php
mysql_connect ('localhost', 'db_username', 'db_password') ;
mysql_select_db ('db_name');

if (isset($_POST['edit'])) {
    $name = htmlspecialchars(strip_tags($_POST['name']));
    $email = htmlspecialchars(strip_tags($_POST['email']));
    $url = htmlspecialchars(strip_tags($_POST['url']));
    $comment = htmlspecialchars(strip_tags($_POST['comment']));
    $comment = nl2br($comment);
    $id = (int)$_POST['id'];

    if (!get_magic_quotes_gpc()) {
        $name = addslashes($name);
        $url = addslashes($url);
        $comment = addslashes($comment);
    }

    $result = mysql_query(&quot;UPDATE php_blog_comments SET name='$name', email='$email', url='$url', comment='$comment' WHERE id='$id' LIMIT 1&quot;) or print (&quot;Can't update comment.&lt;br /&gt;&quot; . $result . &quot;&lt;br /&gt;&quot; . mysql_error());
    if ($result != false) {
        print &quot;&lt;p&gt;The comment has successfully been edited!&lt;/p&gt;&quot;;
    }
}

if(isset($_POST['delete'])) {
$id = (int)$_POST['id'];
     $result = mysql_query(&quot;DELETE FROM php_blog_comments WHERE id='$id' LIMIT 1&quot;) or print (&quot;Can't delete comment.&lt;br /&gt;&quot; . $result . &quot;&lt;br /&gt;&quot; . mysql_error());
     if ($result != false) {
         print &quot;&lt;p&gt;The comment has successfully been deleted!&lt;/p&gt;&quot;;
     }
}

if (isset($_GET['id']) &#038;&#038; !empty($_GET['id']) &#038;&#038; is_numeric($_GET['id'])) {

$result = mysql_query (&quot;SELECT * FROM php_blog_comments WHERE id='$_GET[id]'&quot;) or print (&quot;Can't select comment.&lt;br /&gt;&quot; . mysql_error());

while ($row = mysql_fetch_array($result)) {
      $old_name = stripslashes($row['name']);
      $old_email = $row['email'];
      $old_url = stripslashes($row['url']);
      $old_comment = stripslashes($row['comment']);
      $old_comment = str_replace('&lt;br /&gt;', '', $old_comment);
}

?&gt;

&lt;form method=&quot;post&quot; action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot;&gt;
    &lt;p&gt;&lt;input type=&quot;hidden&quot; name=&quot;id&quot; id=&quot;id&quot; value=&quot;&lt;?php echo $_GET['id']; ?&gt;&quot;&gt;

    &lt;strong&gt;&lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot; size=&quot;40&quot; value=&quot;&lt;?php echo $old_name; ?&gt;&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;&lt;label for=&quot;email&quot;&gt;E-mail:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;email&quot; id=&quot;email&quot; size=&quot;40&quot; value=&quot;&lt;?php echo $old_email; ?&gt;&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;&lt;label for=&quot;url&quot;&gt;URL:&lt;/label&gt;&lt;/strong&gt; &lt;input type=&quot;text&quot; name=&quot;url&quot; id=&quot;url&quot; size=&quot;40&quot; value=&quot;&lt;?php echo $old_url; ?&gt;&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;strong&gt;&lt;label for=&quot;comment&quot;&gt;Comment:&lt;label&gt;&lt;/strong&gt;&lt;br /&gt;
    &lt;textarea cols=&quot;80&quot; rows=&quot;20&quot; name=&quot;comment&quot; id=&quot;comment&quot;&gt;&lt;?php echo $old_comment; ?&gt;&lt;/textarea&gt;&lt;/p&gt;

    &lt;p&gt;&lt;input type=&quot;submit&quot; name=&quot;edit&quot; id=&quot;edit&quot; value=&quot;Save Changes&quot;&gt; &lt;input type=&quot;submit&quot; name=&quot;delete&quot; id=&quot;delete&quot; value=&quot;Delete Comment&quot;&gt; &lt;input type=&quot;submit&quot; value=&quot;Never Mind&quot;&gt;&lt;/p&gt;

&lt;/form&gt;
&lt;?php

}
else {

$result = mysql_query(&quot;SELECT entry AS get_group FROM php_blog_comments GROUP BY get_group DESC LIMIT 10&quot;) or print (&quot;Can't select comments.&lt;br /&gt;&quot; . $result . &quot;&lt;br /&gt;&quot; . mysql_error());

while($row = mysql_fetch_array($result)) {
     $get_group = $row['get_group'];

     print &quot;&lt;p&gt;&quot;;

    $result2 = mysql_query(&quot;SELECT timestamp, title FROM php_blog WHERE id='$get_group'&quot;);
    while($row2 = mysql_fetch_array($result2)) {
        $date = date(&quot;l F d Y&quot;,$row2['timestamp']);
        $title = stripslashes($row2['title']);
        print &quot;&lt;strong&gt;&quot; . $date . &quot; - &quot; . $title . &quot;&lt;/strong&gt;&lt;br /&gt;&quot;;
    }

    $result3 = mysql_query(&quot;SELECT * FROM php_blog_comments WHERE entry='$get_group' ORDER BY timestamp DESC&quot;);
    while($row3 = mysql_fetch_array($result3)) {
        $id = $row3['id'];
        $name = stripslashes($row3['name']);
        $comment = stripslashes($row3['comment']);
        $date = date(&quot;l F d Y&quot;,$row3['timestamp']);

        if (strlen($comment) > 75 || strstr($comment, &quot;&lt;br /&gt;&quot;) || strstr($comment, &quot;\n&quot;)) {
            $comment = substr($comment,0,75) . &quot;...&quot;;
            $comment = str_replace(&quot;&lt;br /&gt;&quot;, &quot;&quot;, $comment);
            $comment = str_replace(&quot;\n&quot;, &quot; &quot;, $comment);
        }

        print &quot;&lt;a href=\&quot;editcomments.php?id=&quot; . $id . &quot;\&quot;&gt;&quot; . $comment . &quot;&lt;/a&gt;&lt;br /&gt;Comment by &quot; . $name . &quot; @ &quot; . $date;
        print &quot;&lt;/p&gt;&quot;;

    }
}
}
mysql_close();

?&gt;</pre>
<p>That&#8217;s it!  Remember to insert your database connection info and to change the table names if yours aren&#8217;t php&#095;blog and php&#095;blog&#095;comments.  You should now have an admin panel to edit and delete comments.  If you have any problems, please post in the tutorials forum!</p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/part-9-creating-a-form-to-edit-and-delete-comments/feed</wfw:commentRss>
		</item>
		<item>
		<title>Allow duplicate emails</title>
		<link>http://codegrrl.com/tutorials/allow-duplicate-emails-2</link>
		<comments>http://codegrrl.com/tutorials/allow-duplicate-emails-2#comments</comments>
		<pubDate>Mon, 29 Nov 2004 13:13:20 +0000</pubDate>
		<dc:creator>Julie</dc:creator>
		
		<category><![CDATA[Scripts: PHPFanBase]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=70</guid>
		<description><![CDATA[This brief tutorial will show you how to allow members to join with duplicate emails.
Open join.php and find the following lines of code (if you haven&#8217;t edited anything, they should start at line 316):
    if ($num == &#34;0&#34;){
        if ($emailnotify == 'Y'){

    [...]]]></description>
			<content:encoded><![CDATA[<p>This brief tutorial will show you how to allow members to join with duplicate emails.</p>
<p><span id="more-70"></span>Open <strong>join.php</strong> and find the following lines of code (if you haven&#8217;t edited anything, they should start at line 316):</p>
<pre>    if ($num == &quot;0&quot;){
        if ($emailnotify == 'Y'){

    $msg = &quot;Name:\t$_POST[name]\n&quot;;
    $msg .= &quot;E-Mail:\t$_POST[email]\n&quot;;
    $msg .= &quot;Hide E-Mail:\t$_POST[hideemail]\n&quot;;
    $msg .= &quot;URL:\t$_POST[url]\n&quot;;
    $msg .= &quot;Country:\t$_POST[country]\n&quot;;
       if ($enablefave == 'Y'){
    $msg .= &quot;Fave:\t$_POST[fave]\n&quot;; }
    $msg .= &quot;Comments:\t$_POST[comments]\n&quot;;
    $msg .= &quot;Sender IP:\t$REMOTE_ADDR\n&quot;;
    $msg .= &quot;\nLogin to your Admin Panel: $siteurl/admin.php\n&quot;;

    $recipient = &quot;$adminemail&quot;;

    $subject = &quot;Member - $sitename&quot;;

    $mailheaders = &quot;From: $_POST[email]\n&quot;;
    $mailheaders .= &quot;Reply-To: $_POST[email]\n\n&quot;;

    mail($recipient, $subject, $msg, $mailheaders); }

       if ($enablefave == 'Y'){
    $query = &quot;INSERT INTO $table VALUES ('','$name','$email','$url','$country','$comments','$hideemail','$apr','$fave')&quot;;
    mysql_query($query);
    } else {
    $query = &quot;INSERT INTO $table VALUES ('','$name','$email','$url','$country','$comments','$hideemail','$apr')&quot;;
    mysql_query($query);
    }

    echo &quot;&lt;p&gt;Thank you, $_POST[name]! You will be added to the members list with the next update. :)&lt;/p&gt;&quot;;

    } else { ?&gt;

    &lt;p&gt;Sorry, but this email address is already listed in the database. This means that you're already listed as a member. If you wish to update your member     information, please use the &lt;a href=&quot;join.php?action=update&quot;&gt;Update Form&lt;/a&gt;. If you want to see your listing as a member, please &lt;a href=&quot;members.php&quot;&gt;click here&lt;/a&gt;.&lt;/p&gt;
    &lt;? } }</pre>
<p>Delete those lines and replace them with this code:</p>
<pre>    if ($emailnotify == 'Y'){

    $msg = &quot;Name:\t$_POST[name]\n&quot;;
    $msg .= &quot;E-Mail:\t$_POST[email]\n&quot;;
    $msg .= &quot;Hide E-Mail:\t$_POST[hideemail]\n&quot;;
    $msg .= &quot;URL:\t$_POST[url]\n&quot;;
    $msg .= &quot;Country:\t$_POST[country]\n&quot;;
       if ($enablefave == 'Y'){
    $msg .= &quot;Fave:\t$_POST[fave]\n&quot;; }
    $msg .= &quot;Comments:\t$_POST[comments]\n&quot;;
    $msg .= &quot;Sender IP:\t$REMOTE_ADDR\n&quot;;
    $msg .= &quot;\nLogin to your Admin Panel: $siteurl/admin.php\n&quot;;

    $recipient = $adminemail;

    $subject = &quot;Member - $sitename&quot;;

    $mailheaders = &quot;From: $_POST[email]\n&quot;;
    $mailheaders .= &quot;Reply-To: $_POST[email]\n\n&quot;;

    mail($recipient, $subject, $msg, $mailheaders); }

       if ($enablefave == 'Y'){
    $query = &quot;INSERT INTO $table VALUES ('','$name','$email','$url','$country','$comments','$hideemail','$apr','$fave')&quot;;
    mysql_query($query);
    } else {
    $query = &quot;INSERT INTO $table VALUES ('','$name','$email','$url','$country','$comments','$hideemail','$apr')&quot;;
    mysql_query($query);
    }

    echo &quot;&lt;p&gt;Thank you, $_POST[name]! You will be added to the members list with the next update. :)&lt;/p&gt;&quot;;
    }</pre>
<p>That&#8217;s it!  Just save and re-upload the file.  Now members are able to join more than once with the same email, which can come in handy if you are using the script for something other than a fanlisting.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/allow-duplicate-emails-2/feed</wfw:commentRss>
		</item>
		<item>
		<title>Part 8: Next &#038; previous links</title>
		<link>http://codegrrl.com/tutorials/part-8-next-previous-links</link>
		<comments>http://codegrrl.com/tutorials/part-8-next-previous-links#comments</comments>
		<pubDate>Sat, 04 Sep 2004 13:21:39 +0000</pubDate>
		<dc:creator>Michelle</dc:creator>
		
		<category><![CDATA[Build A Blog]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=71</guid>
		<description><![CDATA[In this instalment we will learn how to create next and previous links for easy navigation through our single entry pages.  The following code should be placed on your single entry page (probably called journal.php or entry.php), just after your blog entry (and before any commenting functions). In this tutorial, the individual entry page [...]]]></description>
			<content:encoded><![CDATA[<p>In this instalment we will learn how to create next and previous links for easy navigation through our single entry pages.  The following code should be placed on your single entry page (probably called journal.php or entry.php), just after your blog entry (and before any commenting functions). In this tutorial, the individual entry page will be referred to as journal.php.</p>
<p><span id="more-71"></span>MySQL should already be open on this page.  Ensure that you have not closed MySQL previous to this code.</p>
<p>Now, the SQL query to find the id of the entry BEFORE the one you are viewing (ie, the previous id) should look like so:</p>
<pre>$sql_prev = &quot;SELECT * FROM php_blog WHERE id &lt; '$id' ORDER BY id DESC LIMIT 1&quot;;</pre>
<p>Remember that the $id variable comes from your url, and that any time you view journal.php, you should view it as journal.php?id=xx (where xx is an id number).  The above code takes your $id variable, and find the first existing smaller id number &#8212; the id for the previous entry.</p>
<p>Next we&#8217;ll execute that SQL:</p>
<pre>$result_prev = mysql_query ($sql_prev) or print (&quot;Can't select previous entry id table php_blog.&lt;br /&gt;&quot; . $sql_prev . &quot;&lt;br /&gt;&quot; . mysql_error());</pre>
<p>And while it is true, define $prev as the previous id:</p>
<pre>while ($row = mysql_fetch_array($result_prev)) {
    $prev = $row['id'];
}</pre>
<p>Now, in order to have a previous link if there is a previous entry, but not have one if no previous entry exists, we will use the following if statement:</p>
<pre>if (isset($prev)) {
    // print a previous link
    printf(&quot;&lt;a href=\&quot;entry.php?id=%s\&quot;&gt;Previous&lt;/a&gt; -- &quot;, $prev);
}
else {
    // just print the word &quot;previous&quot;
    print &quot;Previous -- &quot;;
}</pre>
<p>So, the entire code for coming up with a previous link will look something like this:</p>
<pre>$sql_prev = &quot;SELECT * FROM php_blog WHERE id &lt; '$id' ORDER BY id DESC LIMIT 1&quot;;
$result_prev = mysql_query ($sql_prev) or print (&quot;Can't select previous entry id table php_blog.&lt;br /&gt;&quot; . $sql_prev . &quot;&lt;br /&gt;&quot; . mysql_error());

while ($row = mysql_fetch_array($result_prev)) {
    $prev = $row['id'];
}

if (isset($prev)) {
    // print a previous link
    printf(&quot;&lt;a href=\&quot;journal.php?id=%s\&quot;&gt;Previous&lt;/a&gt; -- &quot;, $prev);
}
else {
    // just print the word &quot;previous&quot;

    print &quot;Previous -- &quot;;
}</pre>
<p>Our next link will be almost identical, except for finding the first existing LARGER id number, ie, the next entry id:</p>
<pre>$sql_next = &quot;SELECT * FROM php_blog WHERE id &gt; '$id' ORDER BY id LIMIT 1&quot;;
$result_next = mysql_query ($sql_next) or print (&quot;Can't select next entry id table php_blog.&lt;br /&gt;&quot; . $sql_next . &quot;&lt;br /&gt;&quot; . mysql_error());

while ($row = mysql_fetch_array($result_next)) {
    $next = $row['id'];
}

if (isset($next)) {
    // print a next link
    printf(&quot;&lt;a href=\&quot;journal.php?id=%s\&quot;&gt;Next&lt;/a&gt;&quot;, $next);
}
else {
    // just print the word &quot;next&quot;

    print &quot;Next&quot;;
}</pre>
<p>And that&#8217;s all there is to it.  If you have any trouble, post your entire journal.php code in the Tutorial Help forum.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/part-8-next-previous-links/feed</wfw:commentRss>
		</item>
		<item>
		<title>Collective: show all</title>
		<link>http://codegrrl.com/tutorials/collective-show-all</link>
		<comments>http://codegrrl.com/tutorials/collective-show-all#comments</comments>
		<pubDate>Sun, 22 Aug 2004 13:24:07 +0000</pubDate>
		<dc:creator>Sasha</dc:creator>
		
		<category><![CDATA[Scripts: Flinx]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=72</guid>
		<description><![CDATA[Do you want to have a page that shows every fanlist in your collective, rather than having to click on each category? This tutorial will show you how.
Note: This tutorial is for Flinx Collective, not the normal version!
First, open your editor (even something as simple as Notepad would work for this) - save the new [...]]]></description>
			<content:encoded><![CDATA[<p>Do you want to have a page that shows every fanlist in your collective, rather than having to click on each category? This tutorial will show you how.<br />
Note: This tutorial is for <strong>Flinx Collective</strong>, not the normal version!</p>
<p><span id="more-72"></span>First, open your editor (even something as simple as Notepad would work for this) - save the new file as <strong>showall.php</strong>.</p>
<p>Paste all of the following code into it:</p>
<pre>&lt;?php
include(&quot;config.php&quot;);
include($header);
$query = &quot;SELECT * FROM $table_link&quot;;
$result = mysql_query($query);
$num = mysql_num_rows($result);
?&gt;

&lt;p&gt;Below you can view all &lt;strong&gt;&lt;?php echo $num; ?&gt;&lt;/strong&gt; listings in this Collective.&lt;/p&gt;

&lt;?php
while ($row = mysql_fetch_assoc($result)) {
    $title = $row['title'];
    $subject = $row['subject'];
    $url = $row['url'];
    $description = $row['description'];
    $date = $row['date'];
    $image = $row['image'];
    $width = $row['width'];
    $height = $row['height'];
    if ($width &lt; 10) {
        $insert_width = &quot;&quot;;
    }
    else {
        $insert_width=&quot; width=\&quot;&quot; . $width . &quot;\&quot;&quot;;
    }
    if ($height &lt; 10) {
        $insert_height = &quot;&quot;;
    }
    else {
        $insert_height=&quot; height=\&quot;&quot; . $height . &quot;\&quot;&quot;;
    }
    ?&gt;

&lt;table align=&quot;center&quot; width=&quot;&lt;?php echo $desc_width; ?&gt;&quot;&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href=&quot;&lt;?php echo $url; ?&gt;&quot; class=&quot;pic&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;&lt;?php echo $base_url; ?&gt;&lt;?php echo $image; ?&gt;&quot; alt=&quot;&lt;?php echo $name; ?&gt;&quot;&lt;?php echo $insert_width; ?&gt;&lt;?php echo $insert_height; ?&gt;&gt;&lt;/a&gt;

&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Title:&lt;/strong&gt; &lt;?php echo $title; ?&gt;&lt;br /&gt;
&lt;strong&gt;Subject:&lt;/strong&gt; &lt;?php echo $subject; ?&gt;&lt;br /&gt;

&lt;strong&gt;Opened:&lt;/strong&gt; &lt;?php echo $date; ?&gt;&lt;br /&gt;
&lt;em&gt;&lt;?php echo $description; ?&gt;&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;?php
}
include($footer); ?&gt;</pre>
<p>Edit any of the table attributes to your own liking, to fit in with your layout.</p>
<p>Save the file in your collective website (make sure it&#8217;s in the same folder as your FlinxCollective config.php!), upload it, and voila! A page that shows every list in your collective, alphabetically.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/collective-show-all/feed</wfw:commentRss>
		</item>
		<item>
		<title>Reviews section (with MTAmazon)</title>
		<link>http://codegrrl.com/tutorials/reviews-section-with-mtamazon</link>
		<comments>http://codegrrl.com/tutorials/reviews-section-with-mtamazon#comments</comments>
		<pubDate>Tue, 17 Aug 2004 13:26:54 +0000</pubDate>
		<dc:creator>Sasha</dc:creator>
		
		<category><![CDATA[Movable Type]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=73</guid>
		<description><![CDATA[I have been asked loads of times how I created my own Book Reviews section on my personal site, so here&#8217;s a tutorial explaining how.
For this tutorial you&#8217;ll need Movable Type 2.x. It might work in MT3 as well, but I haven&#8217;t tested it so I&#8217;m not sure. You&#8217;ll also need to have MTAmazon installed [...]]]></description>
			<content:encoded><![CDATA[<p>I have been asked loads of times how I created my own Book Reviews section on my personal site, so here&#8217;s a tutorial explaining how.</p>
<p><span id="more-73"></span>For this tutorial you&#8217;ll need <a href="http://movabletype.org">Movable Type 2.x</a>. It might work in MT3 as well, but I haven&#8217;t tested it so I&#8217;m not sure. You&#8217;ll also need to have <a href="http://mtamazon.sourceforge.net/">MTAmazon</a> installed and working properly. This tutorial will <strong>not</strong> tell you how to install MTAmazon, however.</p>
<p>Please note that you can use this tutorial for more than just book reviews. Using <a href="http://www.amazon.com/">Amazon</a>, you could also review CDs, DVDs, games, etc. The categories in this tutorial are used as the Book Categories (Fiction, Non-Fiction, Childrens, etc), but obviously they can be used for other things as well. For example, if you&#8217;re reviewing games, you might want to create a category for each game platform (PC, PS2, XBox) that you&#8217;re reviewing.<br />
This tutorial is just to give you an idea of how to set something like this up properly. <img src='http://codegrrl.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>And as always, this tutorial assumes you&#8217;re using PHP headers and footers for your site. If you&#8217;re not, just replace the PHP includes with whatever HTML coding you&#8217;d like there.</p>
<p>Create a new blog in MT. My Core Setup settings look like this, where &quot;books&quot; is the folder I&#8217;ll be putting the reviews in (so replace books with something else if you don&#8217;t want it all in a &quot;books&quot; folder):</p>
<p><strong>Weblog Name:</strong> Books<br />
<strong>Local Site Path:</strong> /your/absolute/path/to/books<br />
<strong>Site URL:</strong> http://domain.com/books/<br />
<strong>Local Archive Path:</strong> same as Local Site Path</p>
<p><strong>Archive URL:</strong> same as Site URL</p>
<p>Under <strong>Preferences</strong>, make sure that you have selected Individual as Preferred Archive Type, and php as the file extension for archive files.</p>
<p>Under <strong>Archiving</strong>, make sure you have the boxes ticked next to Individual and Category. Fill in the following under Archive File Template:</p>
<ul>
<li>
<p>Individual:</p>
<pre>&lt;MTEntryDate format=&quot;%Y&quot;&gt;/&lt;MTEntryDate format=&quot;%m&quot;&gt;/&lt;MTEntryTitle dirify=&quot;1&quot;&gt;.php</pre>
</li>
<li>
<p>Category:</p>
<pre>&lt;MTCategoryLabel dirify=&quot;1&quot;&gt;.php</pre>
</li>
</ul>
<p>Now go into the <strong>Templates</strong> section. The only templates you&#8217;ll really need for this tutorial are Main Index, Category Archive, and Individual Entry Archive, so feel free to delete the rest if you&#8217;re not using them.</p>
<p>Click on Main Index, and copy and paste the following coding into it:</p>
<pre>
&lt;?php include(&quot;/path/to/your/header.php&quot;); ?&gt;

&lt;h2&gt;Recent Reads&lt;/h2&gt;

&lt;MTEntries lastn=&quot;3&quot;&gt;
&lt;MTAmazon method=&quot;Asin&quot; search=&quot;[MTEntryMore]&quot;&gt;
&lt;a href=&quot;&lt;MTAmazonLink&gt;&quot;&gt;&lt;img src=&quot;&lt;MTAmazonMediumImage&gt;&quot; border=&quot;0&quot; class=&quot;floatleft&quot; alt=&quot;&lt;MTAmazonTitle&gt;&quot; /&gt;&lt;/a&gt;

&lt;h3&gt;&lt;$MTEntryDate format=&quot;%x&quot;$&gt; ::&lt;a name=&quot;&lt;$MTEntryID pad=&quot;1&quot;$&gt;&quot;&gt;&lt;/a&gt; &lt;strong&gt;&lt;$MTEntryTitle$&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;&lt;MTAmazonTitle&gt;&lt;br /&gt;
By &lt;MTAmazonAuthor&gt;&lt;br /&gt;
Released &lt;MTAmazonReleaseDate format=&quot;%B %e, %Y&quot;&gt;&lt;br /&gt;
&lt;MTAmazonSalePrice&gt;&lt;/p&gt;&lt;/MTAmazon&gt;

&lt;p&gt;&lt;$MTEntryExcerpt$&gt;
&nbsp;&nbsp;&nbsp;&lt;a href=&quot;&lt;MTEntryLink&gt;&quot;&gt;&lt;b&gt;[more...]&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;
&nbsp;&lt;/p&gt;
&lt;/MTEntries&gt;

&lt;h2&gt;Genres&lt;/h2&gt;
&lt;ul&gt;&lt;MTCategories&gt;
&lt;li&gt;&lt;a href=&quot;&lt;$MTCategoryArchiveLink$&gt;&quot;&gt;&lt;$MTCategoryLabel$&gt;&lt;/a&gt; [ &lt;MTCategoryCount&gt; ]&lt;/li&gt;

&lt;/MTCategories&gt;&lt;/ul&gt;

&lt;h2&gt;Older Reads&lt;/h2&gt;
&lt;ul&gt;
&lt;MTArchiveList archive_type=&quot;Monthly&quot;&gt;
&lt;li&gt;In  &lt;b&gt;&lt;$MTArchiveTitle$&gt;&lt;/b&gt; I read the following book(s):&lt;br/&gt;

  &lt;MTEntries&gt;- &nbsp;&nbsp;&nbsp;&lt;a href=&quot;&lt;$MTEntryLink  archive_type=&quot;Individual&quot;$&gt;&quot;&gt;&lt;$MTEntryTitle$&gt;&lt;/a&gt;&nbsp;&nbsp;&nbsp;&lt;/MTEntries&gt;  - &lt;br /&gt;&nbsp;&lt;br /&gt;&lt;/li&gt;
&lt;/MTArchiveList&gt;&lt;/ul&gt;

&lt;?php include(&quot;/path/to/your/footer.php&quot;); ?&gt;</pre>
<p>Hit save, and go to the Category Template. Copy the following code into the Category template:</p>
<pre>&lt;?php include(&quot;/path/to/your/header.php&quot;); ?&gt;
&lt;h1&gt;Book List&lt;/h1&gt;

&lt;h2&gt;&lt;$MTArchiveCategory$&gt;&lt;/h2&gt;

&lt;ul&gt;&lt;MTEntries&gt;
&lt;li&gt;&lt;a href=&quot;&lt;MTEntryLink&gt;&quot;&gt;&lt;MTEntryTitle&gt;&lt;/a&gt; (&lt;MTEntryDate format=&quot;%x&quot;&gt;)&lt;/li&gt;

&lt;/MTEntries&gt;
&lt;/ul&gt;
&lt;?php include(&quot;/path/to/your/footer.php&quot;); ?&gt;</pre>
<p>Hit save again, and now go to the Individual Entry Archive. Copy the following code into there:</p>
<pre>&lt;?php include(&quot;/path/to/your/header.php&quot;); ?&gt;
&lt;h2&gt;&lt;$MTEntryDate format=&quot;%x&quot;$&gt; - &lt;$MTEntryTitle$&gt;&lt;/h2&gt;

&lt;div align=&quot;center&quot;&gt;
&lt;MTEntryPrevious&gt;
&lt;a href=&quot;&lt;$MTEntryLink$&gt;&quot;&gt;&laquo; &lt;$MTEntryTitle$&gt;&lt;/a&gt; |
&lt;/MTEntryPrevious&gt;
&lt;a href=&quot;/&quot;&gt;Main&lt;/a&gt;

&lt;MTEntryNext&gt;
| &lt;a href=&quot;&lt;$MTEntryLink$&gt;&quot;&gt;&lt;$MTEntryTitle$&gt; &raquo;&lt;/a&gt;
&lt;/MTEntryNext&gt;
&lt;/div&gt;

&lt;$MTEntryTrackbackData$&gt;
&lt;div class=&quot;blog&quot;&gt;

&lt;MTAmazon method=&quot;Asin&quot; search=&quot;[MTEntryMore]&quot;&gt;
&lt;a href=&quot;&lt;MTAmazonLink&gt;&quot;&gt;&lt;img src=&quot;&lt;MTAmazonMediumImage&gt;&quot; border=&quot;0&quot; alt=&quot;&lt;MTAmazonTitle&gt;&quot; /&gt;&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Title: &lt;b&gt;&lt;MTAmazonTitle&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Author:  &lt;b&gt;&lt;MTAmazonAuthor&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Genre:  &lt;b&gt;&lt;MTEntryCategories glue=&quot;, &quot;&gt;&lt;a href=&quot;&lt;$MTCategoryArchiveLink$&gt;&quot;&gt;&lt;$MTCategoryLabel$&gt;&lt;/a&gt;&lt;/MTEntryCategories&gt;&lt;/b&gt;&lt;/li&gt;

&lt;li&gt;Published By:  &lt;b&gt;&lt;MTAmazonManufacturer&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Published On: &lt;b&gt;&lt;MTAmazonReleaseDate format=&quot;%B %e, %Y&quot;&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Type: &lt;b&gt;&lt;MTAmazonField name=&quot;Media&quot;&gt;&lt;/b&gt;&lt;/li&gt;

&lt;li&gt;Price: &lt;b&gt;&lt;MTAmazonSalePrice&gt;&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Avg. Customer Rating: &lt;b&gt;&lt;MTAmazonField name=&quot;Reviews/AvgCustomerRating&quot;&gt;&lt;/b&gt; stars&lt;/li&gt;

&lt;li&gt;[ &lt;a href=&quot;&lt;MTAmazonLink&gt;&quot;&gt;Buy Now&lt;/a&gt; ]&lt;/li&gt;
&lt;/ul&gt;
&lt;/MTAmazon&gt;

&lt;$MTEntryBody convert_breaks=&quot;1&quot;$&gt; 

&lt;div class=&quot;by&quot;&gt;&lt;b&gt;Agree? Disagree?&lt;/b&gt; &lt;MTEntryIfAllowComments&gt;&lt;a href=&quot;&lt;$MTCGIPath$&gt;&lt;$MTCommentScript$&gt;?entry_id=&lt;$MTEntryID$&gt;&quot; onclick=&quot;OpenComments(this.href); return false&quot;&gt;Speak up!&lt;/a&gt; (&lt;$MTEntryCommentCount$&gt;)&lt;/MTEntryIfAllowComments&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;? include(&quot;/path/to/your/footer.php&quot;); ?&gt;</pre>
<p>Obviously, if you&#8217;re not reviewing books, you&#8217;ll want to edit the fields mentioned above. A CD won&#8217;t have an Author, so you&#8217;d need to take that out. <a href="http://mtamazon.sourceforge.net/use.html">Here is a list of tags that you can use in MTAmazon</a>.<br />
When you&#8217;re done, hit Save again.</p>
<p>Now you&#8217;re ready to publish your first review. Click on New Entry in the navigation bar, and put the following into the entry fields:</p>
<p><strong>Entry Title</strong>: Book Title</p>
<p><strong>Category</strong>: Book&#8217;s category<br />
<strong>Entry Body</strong>: Your review of the book<br />
<strong>Extended Entry</strong>: The book&#8217;s ISBN or ASIN number (look it up on Amazon)</p>
<p>And that&#8217;s all. Now when you hit Save Entry (and make sure that Post Status is set to Publish), your first book review will show up!</p>
<p>If you get any errors, keep in mind that they&#8217;re usually caused by MTAmazon. Double check that you have the right ASIN number, and that there are no spaces around the number in the Extended Entry field. If you&#8217;re reviewing something else than books, make sure that the tags you used in the templates are actually available for whatever it is that you&#8217;re reviewing.</p>
<p>And lastly, keep in mind that this is just the way I do it. There are many other ways to achieve a similar result, please don&#8217;t feel like you have to copy me exactly. This tutorial is more meant to give you an idea of how to use MT&#8217;s versatality than a step-by-step-this-is-the-only-way-to-do-it guide. <img src='http://codegrrl.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> Have fun with it!</p>
<p><strong>PLEASE NOTE:</strong> If you&#8217;re getting MTAmazon errors with this tutorial, please take a look at the following <a href="http://www.codegrrl.com/forums/index.php?showtopic=5646">forum thread</a>! <img src='http://codegrrl.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codegrrl.com/tutorials/reviews-section-with-mtamazon/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
