WordPress Development: WPDB Selections

Published on Feb 25, 2009   //  Development
Off

WordPress Development

Last week we began covering the WPDB class available in WordPress. This week we will continue along that topic. WordPress provides multiple functions for doing SELECT queries on the database.

Generic Select

With the WPDB class, there are functions available to select specific variables, rows or columns, but there is also a “generic” SELECT. get_results() allows you to perform a SELECT query on the database, and have the results returned to you in an object, associative array or numerical array. This syntax of get_results is simple:

[code='php']$wpdb->get_results('query', output_type);[/code]

Where query is your SELECT query and output_type is either:

  • OBJECT – result will be an object (default)
  • ARRAY_A – result will be an associative array
  • ARRAY_N – result will be a numerical array

Selecting a Row

If you want to retrieve an entire, single row, the WPDB class has a function for this. get_row() will output one row and cache the rest of the result.

[code='php']$wpdb->get_row('query', output_type, row_offset);[/code]

query is your SELECT query, output_type is the same as get_results() and row_offset is the row you want (starting from 0, which is the default).

Selecting a Column

Similar to get_row(), get_col() will return a single column from your SELECT query.

[code='php']$wpdb->get_col('query', column_offset);[/code]

query is your SELECT query and column_offset is the column you would like return (starting from 0, which is the default).

Flushing the Cache

Whenever you run a query using a function in the WPDB class, related information is cached in the variables $wpdb->last_result, $wpdb->last_query and $wpdb->col_info. You can empty this cache (and you should after you’re finished with it) using the flush() function.

[code='php']$wpdb->flush();[/code]

Next week

Next week, we’ll be covering how to use the WPDB class to insert and update data.

WordPress Wednesday: Simple PayPal Donate

Published on Feb 25, 2009   //  WordPress
Off

Perhaps you use your WordPress-powered blog to share some of your creations with the Internet community. This could be in the form of useful images, scripts, simple games, or even WordPress plugins. By offering these services to the community for free, it’s only fair that you give them the opportunity to give you something back. That’s why so many of these kinds of sites have some sort of “donation” button on them.

You could certainly log into your PayPal account and go through the process of creating the donation button using their tool, but some have noted that the generated script may not be XHTML compliant. Further still, you may only want to place the PayPal Donation button in certain posts, avoiding the integration with your template files. What happens if you want to change something after the fact? Are you going to edit all of those individual posts?

Overcoming both of these concerns is the recently developed Simple PayPal Donate WordPress plug-in. This plug-in does exactly what you’d expect it to do. Moreover, you can edit it from a single location and have the effects reach everywhere where the code is implemented all at once. This functions in a similar way to something like AdSense-Deluxe, for example.

Unfortunately, the Simple PayPal Donate plug-in does not have an options screen within the WordPress dashboard. Instead, you’ll need to configure it through the raw code and, optionally, by modifying the plugin itself. For more information, check out the official page at Artiss.co.uk.

Community Poll

Published on Feb 25, 2009   //  Polls

Weekly Community Poll

Last week we asked are you now where you thought you would be 10 years ago and 58% said not even close. This weeks question is…

 

{democracy:70}

Twitter 101: Understanding the Basics of Twitter

Published on Feb 24, 2009   //  Events
Off

Everyday more and more businesses and people are getting on board with Twitter. This course is for anyone who already has a twitter account, but is still trying to find their away around and not really sure what Twitter is all about. Or it could be that you’ve heard of Twitter, but you’re not sure how it could help your business.

In this 1 hour fast paced Twitter training you will learn to cut through the noise on Twitter and you will receive the basic understanding of the different Twitter components.

When is the course happening?
Wednesday, February 25, 2009 5:30 PM - 6:30 PM

Where is it happening?
Online Course

Registration
To register for this course you need to register here.

Course Fee
This course costs $49.00.

Everything PHP: Arrays and Foreach

Published on Feb 24, 2009   //  Development
Off

Everything PHP

This week we’ll be going over arrays and foreach loops. Both are very powerful and useful, and will be used frequently in your programming.

Arrays

No, arrays are not loops, however we’ll be taking this slight detour as foreach loops are based around arrays. An array is a type, just as variables, strings and integers are all types. Arrays can be best described as an ordered map. They can either associate values to keys, or be a list of values. Arrays are often contained within a variables. Let’s look at a couple of value and key based arrays:

[code='php']$array1 = array('one' => 1,
'two' => 2,
'three' => 3);

$array2 = array('first_name' => 'Matt',
'last_name' => 'Freedman',
'website' => 'http://mattsblog.ca/');[/code]

Arrays syntax are simple, keys are placed first, followed by =>, followed by the key’s value. These “sets” are separated with a comma. Keys must either be a string or integer, while values can be of any type (including, yes, other arrays). You may have an infinite amount of key and value sets. Sets are usually written on new lines, to make the code easier to read, but it’s completely optional.

We can also choose to just specify a list of values:

[code='php']$array3 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);[/code]

This will cause the keys to be automatically set to an incrementing number, starting from 0. So, the above array is equivalent to the following array:

[code='php']$array4 = array(0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
7 => 8,
8 => 9);[/code]

You can list an infinite amount of values in an array.

You may have noticed that attempting to echo or print an array results in an error. There is a special function for outputting entire arrays to a browser, print_r(). Which is used like this:

[code='php']$array5 = array('key0' => 'value0',
'key1' => 'value1');
print_r($array5);[/code]

And will output the following:

Array
(
[key0] => value0
[key1] => value1
)

It is also possible to retrieve the value of a specific key and also modify it. We can do that like so:

[code='php']echo $array5['key0'];
$array5['key1'] = 'banana';[/code]

Which would output value0 and change the value of the key key1 in $array5 to banana.

There are many aspects to arrays, and many functions available to manipulate them, that we cannot cover them all here. So, if you want to learn more about arrays, you can find more about them on their PHP page.

Foreach

Foreach is used to iterate over arrays. It allows you to loop through each array key and get its value.

[code='php']$array = array('1', '2', '3', '4', '5', '6');
foreach($array as $value) {
echo $value;
}[/code]

This will go through all the values of $array (starting from the beginning) and temporarily assign the current iteration’s value to $value, which you can then use within the curly brackets (in this example we just output the value to the browser). You’re free the call the $value variable whatever you want in the foreach statement.

If you’re dealing with more complex arrays, with keys and values, you can also choose to assign the key to a variable.

[code='php']$fruit_colours = array('banana' => 'yellow',
'apple' => 'red',
'pineapple' => 'yellow');
foreach($fruit_colours as $key => $value) {
echo $key . ': ' . $value;
}[/code]

This would assign the value of the key (the portion of the array before the =>) to $key and the corresponding value to $value.

If you need to ever modify the current key or value inside of a foreach loop, you can do so by assigning the variable by reference (this is PHP 5 and up only). So, let’s take our first example and change it to modify the current value instead.

[code='php']$array = array(1, 2, 3, 4, 5, 6);
foreach($array as &$value) {
$value = $value + 5;
}
unset($value);[/code]

As you can see, we set $value as a reference, then we’re able to modify that value within the foreach. After the foreach is complete, the $value reference will still exist, so we should destroy it using unset().

Closing statements

That concludes our article on arrays and foreach loops. Next week we’ll be looking at PHP’s most complex loop, for.

Business 101: The Hired Twitter Manager

Published on Feb 24, 2009   //  Business Topics

A couple of days ago, we asked you the Twitter Question of the Week. If you owned a business would you pay someone to Twitter for you?

The response was a mixed bag with some people saying that only they could tweet for their company whereas others said it was perfectly acceptable so long as this hired employee was representative of your company’s message and perspective. In an effort to expand your brand presence, it is a good idea to have a company Twitter account, but what if you don’t have the time to manage it?

Assuming that you believe it’s acceptable to have someone other than the business owner manage the company’s Twitter account, there are a more than a couple of questions that you will want to keep in mind. First, the Twitter handle will obviously be reflective of your company, but what about the field where people normally enter their real names?

For example, we have our good friend Hummingbird604. That’s the moniker that he uses on his blog and on his Twitter account, but under the “name” field in his Twitter account, he has his real name: Raul Pacheco. If you hired someone to run your business Twitter account, would it be more appropriate for them to enter their real name there or should the name of the company be placed there?

Going with the former strategy, the Twitter community may have an easier time connecting with your representative on a personal level, just like publisher managers for affiliate networks. Going with the latter strategy, you get more flexibility in case you need someone else to take over the account temporarily or permanently. There will be more continuity.

What do you think? If you were to hire someone to Twitter for you, what guidelines and strategies would you have in place?

Page 2 of 1212345...10...Last »