Archive

Posts Tagged ‘print’

Create A Tag Cloud Page In WordPress

April 28th, 2009 No comments

A tag cloud is a name for a collection of keywords that are displayed as a big block of text. Usually the most commonly occurring keyword is the largest, and the least commonly occurring keyword is the smallest. Tag clouds are a neat way of allowing your users to navigate your content in a different way, simply be letting them look over the cloud and linking each keyword to sections of your site that contain that word.

WordPress comes with this function built in and is already available as a widget. However, after adding a few hundred posts this tag cloud can get rather large and this widget can stretch your sidebar to stupid proportions. A way around this is to create a page that can be used to display this large tag cloud. The first step is to create a page template that will be used to run the correct WordPress function to display the tags. This is done by making a copy of your normal page template (call it tagpage.php) and adding the following code at the top.

<?php
/*
Template Name: Tags
*/
?>

When you go to create a page you will see a list of available templates to select from, one of these will be called Tags. Add in your title and some descriptive text and select this template.

Next, we need to add a call to the wp_tag_cloud() function, this basically prints out a small tag cloud. Add the following code into your page template so that is fits in with the rest of your design.

<?php wp_tag_cloud(); ?>

As with most WordPress functions, wp_tag_cloud() allows you to add lots of parameters to change the output of the function. There are lots of different options available, but the main ones that will be used are as follows:

  • smallest – This designates the font size to be used for the least commonly occurring tag. The units are by default in 'pt' or points, but this can be changed via the unit parameter.
  • largest – This designates the font size to be used for the most commonly occurring tag.
  • unit – Unit of measure as pertains to the smallest and largest values. This can be any CSS length value, e.g. pt, px, em, %; default is pt (points).
  • number – This tells the function how many tags to include in the tag cloud. The default is 45, but to print out all tags just use 0.

These parameters are given as an encoded string, for example, to change the number of tags printed from the default of 45 to 20 use the following code.

<?php wp_tag_cloud('number=20'); ?>

Multiple parameters are separated by the & character.

<?php wp_tag_cloud('number=20&largest=200'); ?>

Take a look at the WordPress manual for more information about the wp_tag_cloud() function

To demonstrate I have created a tag cloud page on Talk In Code, using the following code to create the cloud.

<?php wp_tag_cloud('smallest=7&largest=50&number=500'); ?>

Finally, there is also a function called wp_generate_tag_cloud() that has the same functionality as wp_tag_cloud() but will always return the HTML string that makes the tag cloud.

Displaying WordPress Authors

February 26th, 2009 2 comments

WordPress has a couple of rarely used functions that allow author information to be displayed for the current post and a list of all of the authors on the blog.

Adding a written by message to your posts is not difficult at all. Just use the the_author_posts_link() function inside the post loop.

<?php the_author_posts_link(); ?>

This function shouldn’t be confused with the the_author_link() function that prints out the link in the author’s profile.

The second way of printing out author information is by using the wp_list_authors() function. This can take a number of arguments, but the simplest use of it is as follows:

<ul>
<?php wp_list_authors(); ?>
</ul>

This should print out a list of the authors on your blog. There is one very important thing I should mention here. This function will only print out an authors name if they have written a post and are not the default admin account. I tried to print out the authors on a test blog and after a few minutes of frustration I looked at the function call itself on line 460 of the file author-template.php in the directory wp-includes. The function specifically excludes authors that haven’t written any posts but you can turn this on if you give it the right parameter.

The wp_list_authors() function takes a number of arguments in the form of a string of arguments. The available arguments are as follows:

  • optioncount: Print out the number of posts for each author. The default is 0 (false).
  • exclude_admin: Print out the blog admin user. The default is 1 (true)
  • show_fullname: Use the full name of the author instead of their nickname. Default is 0 (false).
  • hide_empty: Show users who don’t have any posts. The default is 1 (true). Note that even if a user with no posts is printed they will not be linked.
  • echo: Immediately echo the list of authors or return as a variable. The default is 1 (true).
  • feed: If set then a link to an RSS feed for that author is printed, along with the text given. The default is not to print anything.
  • feed_image: This is a link to a RSS icon, it works in the same way as the feed parameter, but will override it.

To use the arguments you need to pass them to the function in the form of a string or arguments like this.

<?php wp_list_authors('exclude_admin=0&hide_empty=0&feed=Feed'); ?>

Now that you have a link of authors being printed out it is time to look at where the links go to. Each author has an ID associated with them, and this is appended to the URL using the author tag. If you have permalinks turned on then the authors name is used.

When clicked on WordPress will attempt to display the author information using a template file called author.php. If this is not present then it will display a list of the posts using the archive.php template file. Finally, if this is not present then WordPress will default to the index file.

Your author.php file should contain the following lines so that you can access the author informaiton.

<?php
if(isset($_GET['author_name'])) :
 $curauth = get_userdatabylogin($author_name);
else :
 $curauth = get_userdata(intval($author));
endif;
?>

The $curauth variable now contains an author object that can be used to access the various different data fields associated with authors. The following snippet will print out the authors name, profile and homepage, if this information is not filled in then nothing will be printed.

<h2><?php echo $curauth->first_name . ' ' . $curauth->last_name; ?> (<a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a>)</h2>
<p><?php echo $curauth->user_description; ?></p>

You can also include a normal loop on this page to print out every post that the user has written.

For more information on the author templates (and a full list on the attributes available in the author object) take a look at the author templates section on the WordPress site.

Write To The Output Buffer In PHP

February 10th, 2009 1 comment

The first thing you learn about in PHP is probably how to print something. This is usually done with a call to the echo or print, but there is another way to print things by writing content directly to the output buffer. The following code looks like you are writing to a file, but the text will appear in the browser window because we are writing to the php://output output stream.

$fp = fopen("php://output", 'r+');
fputs($fp, "Hello World");

Or another way…

file_put_contents("php://output", "Hello World");

The php://output stream is an encapsulation between PHP and the browser. The stream doesn’t really exist, but PHP knows what to do with it.

Why would you ever need to know this? Well lets say that you had an application that produced some logs when users performed certain actions. You might want to print those to screen in your development environment rather than save them. So rather than altering the code to see what environment you are in you can just set a configuration option so that you write to php://output instead of the log file. This means that your logs will appear with your output.

Categories: PHP Tags: , , , , , ,

External Script Files And Printing Objects With Flex

November 6th, 2008 No comments

Yesterday I talked about using the Flex Script element to run code within the mxml file, you can also use the source attribute of the Script element to reference external files. To create an external script file FlashDevelop go to the File->New and select Blank Document. You can also do this by pressing Ctrl+N. This will create a blank document that you must save into your src folder of your project with the extension as. Note that if you call this file sourcefile.as then you must reference this in your script tag like this.

<mx:Script source="sourcefile.as">

</mx:Script>

When you do this you will notice that the script file will be part of the Main.mxml file. You can add functions in the same way as you would with an inline script tag.

Printing

With this external script file we will now create some images and print them using the built in printing object that Flex has. This object is called FlexPrintJob and you must include this at the top of your script file if you want to use it. Put the following line at the top of your script file.

import mx.printing.FlexPrintJob;

Create a button in your mxml file that will be used to print.

<mx:Button id="print" label="Print" click="printImage();" />

Add in an image that will be printed.

<mx:Image source="one.png" id="one" />

Here is the image that will be used.

Flex test image

Flex test image

Go back into the script file and add the function that will print the image out. We called this function printImage() in our Button declaration. This function works by creating a new FlexPrintJob object called printJob. This function start() is then used to try and print. Basically, this function returns true if the print job worked, and false if the user clicked cancel.

If the start() function returns true then the object to be printed (in this case the image with the id of "one" is added to the printJob object. Finally, the send() function is called which sends the data off to the printer. Here is the function.

public function printImage():void
{
 // Create a FlexPrintJob instance.
 var printJob:FlexPrintJob = new FlexPrintJob();
 
 // Start the print job and check that it worked
 if (printJob.start() != true) {
  return;
 }
 
 // add object to be printed to the print job
 printJob.addObject(one);
 
 // send to printer
 printJob.send();
}

This is the simplest use of the FlexPrintJob function as there are controls that can be used to format the pages.

It is possible to print off multiple objects, especially if they are children to another object, by adding the parent object to the FlexPrintJob object. Take the following section of mxml, which defines a Canvas element, with three positioned images as children.

<mx:Canvas id="images">
 <mx:Image source="one.png" id="one" x="0" y="0" />
 <mx:Image source="two.png" id="two" x="100" y="0" />
 <mx:Image source="three.png" id="three" x="0" y="100" />
</mx:Canvas>

This produces the following application.

Three images in a canvas

Three images in a canvas

To print out all three images at once just include the Canvas object instead of the single image object. Change line 11 in the printImage() function from this:

 printJob.addObject(one);

To this.

 printJob.addObject(images);

The printJob object knows about inheritance and will print out everything contained within that element. This includes buttons or any other element that you put into the canvas element.

See the Flex documentation for more information about the FlexPrintJob object.

Single Quotes Or Double Quotes With PHP?

July 9th, 2008 1 comment

I have heard tales from the internet about how to write efficient PHP code, and the fact that writing and output string with " (double quote) is slower than writing one with a ' (single quote). The reason is that when PHP encounters a double quote is parses the string to see what is inside. PHP doesn’t do this with single quotes and so the code will run slightly faster.

Rather than take this on face value I thought I would run a benchmark on the code to see what is the quickest way of printing a variable is using the echo command. There are lots of ways to print out a single string using echo, they are as follows.

  • Double quoted string = echo "before variable $variable after variable";
  • Double quoted escaped string = echo "before variable ".$variable." after variable";
  • Double quoted string with commas = echo "before variable ",$variable," after variable";
  • Single quoted escaped string = echo 'before variable '.$variable.' after variable';
  • Single quoted string with commas = echo 'before variable ',$variable,' after variable';
  • Escaped HTML = ?> before variable <?php echo $variable; ?> after varaible <?php

Using the PHP benchmarking function I tested each of these mechanisms to see which was the fastest. Because each test is very quick I ran them all 10,000 times just to get a really good average time. Here is the code for the tests.

<?php
 
function getmicrotime($t){
 list($usec, $sec) = explode(" ",$t);
 return ((float)$usec + (float)$sec);
}
 
$results = array();
$variable = 'the variable value';
 
for($i=0;$i<=10000;++$i){
 $start = microtime();
 echo "before variable $variable after variable";
 $end = microtime();
 $results['double quotes'][] = (getmicrotime($end) - getmicrotime($start));
 
 $start = microtime();
 echo "before variable ".$variable." after variable";
 $end = microtime();
 $results['double escaped quotes'][] = (getmicrotime($end) - getmicrotime($start));
 
 $start = microtime();
 echo "before variable",$variable,"after variable";
 $end = microtime();
 $results['double quotes and commas'][] = (getmicrotime($end) - getmicrotime($start));
 
 $start = microtime();
 echo 'before variable '.$variable.' after variable';
 $end = microtime();
 $results['single escaped quotes'][] = (getmicrotime($end) - getmicrotime($start));
 
 $start = microtime();
 echo 'before variable',$variable,'after variable';
 $end = microtime();
 $results['single quotes and commas'][] = (getmicrotime($end) - getmicrotime($start));
 
 $start = microtime();?>
 before variable <?php echo $variable; ?> after variable
 <?php $end = microtime();
 $results['html'][] = (getmicrotime($end) - getmicrotime($start));
}

When all this is finished I had a bunch of arrays with lots of times in them. I used the following bit of code to work out the average and see which is the fastest.

foreach($results as $method=>$time){
 $average = number_format((array_sum($time)/count($time)),10);
 $fastestaverage[$method]=$average;
}
// sort the averages
krsort($fastestaverage);
echo '<pre>'.print_r($fastestaverage,true).'</pre>';

This produced the following result.

Array
(
 [double quotes and commas] => 0.0000049084
 [double escaped quotes] => 0.0000049590
 [single quotes and commas] => 0.0000050394
 [double quotes] => 0.0000053428
 [single escaped quotes] => 0.0002022830
 [html] => 0.0002147275
)

So it looks as though the quickest way to print out a string using the echo command is to use a combination of double quotes and commas. Like the following snippet.

echo "before variable",$variable,"after variable";

Either I am doing something wrong here, or PHP doesn’t work like I thought it does. Can anyone shed any light on why this result occurs? I know that there are only microseconds between one result and the next, but I would appreciate any information.

Categories: PHP Strings Tags: , , ,