Search Blog Posts

‘PHP’

PHP capitalising the first letters of each word in a sentance

Friday, June 27th, 2008

I recently wanted a contact form to convert a form element, in this case someones name to uppercase. For example ‘tom peters’ decided to enter his name without any capitals. The desired result would ideally be ‘Tom Peters’.

Using PHP we can convert the form field variable by doing the following:

$personsname= "tom peters";
$PersonsName = ucwords($personsname);

echo "<p>The old string - $personsname <br />";
echo "The new string - $PersonsName </p>";

There are a few more examples that you could experiment with for example:
strtolower(); will convert a string to lowercase.
strtoupper(); will convert a string to uppercase.

PHP For Loop…as simple as this!

Sunday, June 15th, 2008

[code]for ( $count = 5; $count <= 5; $counter ++) {
echo $counter;
echo ” <br />”;
}[/code]

Excluding Posts from the Index Page on Wordpress

Tuesday, June 3rd, 2008

If you have ever wanted to exclude posts from your homepage on wordpress keep reading…..

Excluding posts on the index page of the blog was one of those things that i knew was possible but never really got around to actually doing it!

I thought i would share the solution to the problem as i managed to find a quick and easy way of excluding the posts.

Here’s how you do it

Step 1
Locate the index.php file in your current active theme. This is usually situated in the ‘wp-content/themes/’ folder of your server. Once you have located this folder pick your current theme and look inside to find the index.php file. You will then need to download this file and open in your default editor.

Step 2
Once you’ve managed to open the file do a search for the following line of code:
<?php while (have_posts()) : the_post(); ?>

You will then need to insert the following code so you have something that looks like this:
<?php while (have_posts()) : the_post(); ?>
<?php if (in_category('79')) continue; ?>

Step 3
Notice that the category 79 has been used in this example.
To find the id of the category that you want to exclude from the homepage simply login to your admin page and head over to the manage > categories section. Once there simply mouse over the category name that you wish to exclude and notice in the url there will be an id number (this normally displayed to the bottom of your browser).

Extra Steps
If you want to exclude multiple categories simply duplicate the same line of code as shown above changing the id number. See the example below:

<?php while (have_posts()) : the_post(); ?>
<?php if (in_category('79')) continue; ?>
<?php if (in_category('29')) continue; ?>
<?php if (in_category('30')) continue; ?>
<?php if (in_category('12')) continue; ?>

This is its as simple as that!

PHP Include function

Monday, June 2nd, 2008

Using the php include(); statment can be a handy way to speed up your development time , making it easier to amend or add features to a set template or snippet of code.

I have uploaded a zip file with an example of a few include statements. This can be found at the bottom of this post.

Example Code

<?php include("includes/filename.php"); ?>

Take a look at the attached example and see how you can use the php include function to speed up your development time.

Things to note

Unless your machine is set up to allow php files to run locally you will need to upload it to your server to test.

Download

PHP Include Example Download

Returning the Current Page URL

Sunday, March 2nd, 2008

Using PHP you can return the current url of the page by simply using :

$pageURL = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
echo $pageURL;

By using string manipulation you can also us this function to return the end characters of the url request.

For the following url: http://minimalistics.co.uk/gallery/main.php?g2_itemId=89 we can return the end numbers using the following code:
$pageURL = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$pageURL = substr($pageURL,strrpos($pageURL,"=")+1);
echo $pageURL;

The result of this would be 89.

© Copyright 2003-2008 Minimalistic Designs | Terms | Links | Sitemap
Website Design Liskeard - www.minimalistics.co.uk