Archive for the 'PHP' Category

New Tutorial — Create an AJAX Calendar with PHP

Saturday, July 26th, 2008

In my newest tutorial, you learn how to quickly render a calendar using PHP's date() and mktime() functions. Then provide some AJAX functionality to move between months without having to reload the page. This tutorial walks you through a great way to build the foundation in PHP for any calendar needs you might have on your site.

Check out my new tutorial, Create an AJAX Calendar with PHP, to learn how to render a calendar with PHP and use AJAX request to move from month to month.

PHP Remove Item from an Array

Thursday, July 17th, 2008

When working with arrays in PHP, you will encounter times when you need to easily remove known values from that array. There are a couple of quick techniques I use to accomplish this.

The first, uses the PHP function array_diff(). When using array_diff to remove an item from an array, you pass your current array as the first parameter and an array containing only that value you want to remove as the second.

We'll start with our intial array, a list of dog breeds:

$dogs = array('poodle','beagle','great dane','dachshund');

Now, we'll use array_diff to get rid of the poodle to form a list of hounds

$hounds = array_diff($dogs,array('poodle')); // --> array(3) { [1]=>  string(6) "beagle" [2]=>  string(10) "great dane" [3]=>  string(9) "dachshund" }

The second method utilizes the handy array_filter() function for more complex removal. When using array_filter to remove an item from an array, we pass our current array as the first parameter and the filtering callback function as a string as the second parameter.

For this example, we will keep it simple and stick to checking whether or not the type of dog is a poodle. If it is not a poodle, our filtering function will return TRUE and be added to the resulting array.

function gethounds($dog){
  if($dog != 'poodle')
    return TRUE;
}
$hounds = array_filter($dogs,'gethounds'); // --> array(3) { [1]=>  string(6) "beagle" [2]=>  string(10) "great dane" [3]=>  string(9) "dachshund" }

Using PHP Dynamic Variables

Wednesday, July 9th, 2008

PHP dynamic variables have a lot of usefulness. As you're programming for the web, you'll come across many cases where you have a block of code that you will want to use for a number of different cases. This is very common when you have output or validation helpers.

In this quick howto, I will show you how to use PHP's dynamic variables to create a basic little text output helper function. The example is simple, but the ability to do this comes in extremely handy as you start working on bigger projects.

First, let's define two arrays of information, one about my car and one about my favorite guitar:

'Saturn','model'=>'Vue');
$guitardata = array('make'=>'Heritage','model'=>'H-535');

Now that this data is set, we can create a function to put together our text output.

function buildAboutMe($data,$verb){
  global ${$data.'data'}; // Bring the array within scope
  $info = ${$data.'data'}; // Store the array as $info
  return "I $verb a {$info['make']} {$info['model']}.";
}

In this function, we are passing in the $data parameter as a string. The function then uses a dynamic variable to bring the corresponding array within scope of the function. If we pass in 'myinfo', this would be the same as declaring

global $myinfodata

Then we store the $myinfodata array as that same value and access it when putting the string together. Simple enough!

The final code is the actual function calls, passing in a verb argument to make our output make sense based on what we're describing.

$car = buildAboutMe('car','drive');
echo $car; // I drive a Saturn Vue.
$guitar = buildAboutMe('guitar','play');
echo $guitar; // I play a Heritage H-535.

And that's all there is to it! This is a pretty basic example but you can imagine what kind of door using dynamic variables can open as you are writing your next web application!

Ryboe Tag Cloud 1.02 Released

Tuesday, February 26th, 2008

With the release of WordPress v 2.3, there were some structural changes to post categories that broke functionality of version 1.01 of the Ryboe Tag Cloud. I have finally had a chance to sit down and make the couple of quick changes necessary to restore functionality. In this newest version of the Ryboe Tag Cloud, I have also built in backwards compatibility– so if you have not yet upgraded your WordPress install to 2.3, you should still experience a flawless experience.

Without further ado, here is the new plugin.

Download Ryboe Tag Cloud WordPress Plugin

Past Releases:
Release 1.01
Original Release

Ryboe Tag Cloud V.1.01 Released

Thursday, March 8th, 2007

We received such a positive reaction after the release of the Ryboe Tag Cloud for WordPress Plugin. It seems that there are many people out there with WordPress blogs who are looking for a simple and elegant tag cloud to show off their category tags. Well, we are pleased to announce that we have added a nice little enhancement to the plugin in the form of an update — version 1.01.

We received a few emails from individuals who wished to use the tag cloud on their blog, however they were not using the default WordPress structure for category links. The original version of the plugin relied on the fact that the user was able to link to their categories in the following format:

<a href="/category/php">PHP</a>

However, it became apparent that many users could not use these nice "slug" links due to limitations of their web server (mod_rewrite issues, etc) or general favor in defining the links using the category ID in the following format:

<a href="/?cat=1">PHP</a>

We have included an option in the Ryboe Tag Cloud widget so that you may choose to "Use Nice Category Links". This option is a simple checkbox that you can select to use the SEO-favored slug method. If you leave the box unchecked, it will simply link using the category ID.

WordPress Tag Cloud Options

Download the newest version of the Ryboe Tag Cloud Plugin for WordPress

Enjoy!

New Tutorial - PHP Headers to Force Download

Thursday, February 22nd, 2007

In the new tutorial, Using PHP Headers to Force Download, we briefly go over how to use the header() function in PHP to set HTTP raw headers to define application type. This is useful when providing downloads on your website and you don't want to risk the user's browser trying to handle the file with default plug-ins and applications. Check it out!

Ryboe Tag Cloud Plugin for WordPress

Thursday, January 11th, 2007

Familiar with seeing tag clouds around the net?  Would you like to display the content on your WordPress blog as tags and tag clouds respectively.  Then look no further.  We have released the first version of the Ryboe Tag Cloud Plugin for WordPress. This plugin uses the WordPress widget system to place a tag cloud in your sidebar.  With a fast and easy install, you can quickly convert your categories to "tags" and have a tag cloud in your sidebar in no time!

Download the Ryboe Tag Cloud Plugin

Here's how to install it:

  1. Make sure you have WordPress Widgets installed on your .blog. If you don't, go ahead and head here for official instructions on how to install WordPress widgets. Note** The download link is on the left side of that page.
  2. After verifiying that you have WordPress Widgets installed, download the Ryboe Tag Cloud Plugin for WordPress.
  3. Unzip the file and copy it to your /wp-content/plugins/widgets directory with all of your other widgets.
  4. Log into your WordPress Dashboard and head to the "Plugins" menu. Find the plug-in entitled "Ryboe Tag Cloud" and select "activate"
  5. Navigate to Presentation -> Sidebar Widgets and drag the "Ryboe Tag Cloud" onto your sidebar layout. (Be sure to change your title here if you wish to have one).
  6. Visit your blog to see your new tag cloud!

It's that quick and easy.  Now you have a slick little tag cloud of all your post categories on your sidebar!

Hiding Subpages (Children Pages) in WordPress

Saturday, January 6th, 2007

As I was getting my template refined for the implementation of this site, I came across an interesting little code snippet.

What was I trying to do? I was working on adding what WordPress calls "Pages" to my site to use as static content for my project examples. Once I had added my pages, I needed to display these pages on my header.php. In order to do so, WordPress provides a function. Here's what the code looks like:

<?php wp_list_pages(); ?>

The issue with this function is it provides an entire list of links to each of your pages that you have added to WordPress. In my case, I had set up a number of Pages to be children [pages linked off] of the "Project Examples" page. It turns out that you can provide to the wp_list_pages() function an argument defining the depth of pages shown. If you wanted to only show one level of pages, you would use the value '1'.

I was able to choose the levels deep I wished to display pages from the parents. In doing this, the function would no longer display all my pages but rather just the top level. It's now up to me to manually link to the other existing pages in my content management system.

The final code:

<?php wp_list_pages('depth=1'); ?>

Note, you can also exclude individual pages from being displayed using this function. In order to do so, provide the id's of each page you would like to hide and format as such:

<?php wp_list_pages('exclude=12,43'); ?>

It turns out there is quite a bit you can do with this function. Check out the official WordPress docs for more