Posts Tagged ‘php’

Order Posts by Meta Value Numbers in Wordpress

Tuesday, September 8th, 2009

The following changes work on Wordpress version 2.8.*, prior version will need a slightly different change.

A brief background first. I needed to have a page in which my posts were ordered by the meta_values to a particular meta_key. By default meta_value is LONGTEXT data in mysql, therefore we are unable to order posts by meta_value NUMBERS. To force mysql to return the meta_value entry as a NUMBER, we must pass a query like this: ‘meta_value+0′.

I set my custom page up to run a query_posts.

1
query_posts('meta_key=views&orderby=meta_value_number&order=DESC');

Then use The Loop as normal.

Inside query.php I did the following:
Goto line ~2035, add below

1
$allowed_keys[] = 'meta_value_number';

Goto line ~2057, after case break, add below

1
2
3
'meta_value_number':
$orderby = "$wpdb->postmeta.meta_value+0";
break;

Thats it! You can now pull posts ordered by meta_value NUMBERS!!

Remember that this method only works in 2.8.*, if you have an earlier version and would like to figure it out, or have a solution, please feel free to comment.

Creating a PHP Verification Image

Thursday, March 22nd, 2007

You will need to have GD library loaded on your server, and you should be using PHP4+. To check to see if you have either of these running, simply create a file, call it whatever you want, I call mine info.php. Place the following code in there:

1
<? phpinfo(); ?>

If all checks out, then you are ready to move on.

Firstly, you will need to create a session, both in your image script, and the page that will be serving the image, you will see why later.

1
session_start();

And for some, you will have to create a save path for your sessions.

1
session_save_path('path/to/tmp');

Once you have set this up, you can now start creating your image. Lets start with the variables needed.

1
2
3
4
5
6
$w = 120;
$h = 28;
$x = 6;
$y = 20;
$font = "fonts/arial.ttf";
$font_size = 16;

These variables can be changed to your liking, I chose the basics to get you started. But, for example, you can change arial.ttf to whatever .ttf you like, as long as it is not wingdings, you should be all set.
Next we create the image, and set its background color and text color.

1
2
3
$image = imagecreate($w,$h);
$background_color = imagecolorallocate($image,215,255,255);
$text_color = imagecolorallocate($image,90,90,90);

Now we create our random string for which we set our text to the image for display, and set our SESSION variable.

1
2
3
$str = md5(rand(0,9999));
$output = substr($str, 14, 6);
$_SESSION['verify'] = $output;

With that complete, we move on to applying the random string to the blank image we created earlier.

1
2
3
4
5
6
7
8
9
10
11
if(file_exists($font)) {
putenv('GDFONTPATH=' . realpath('.'));
for($i = 0; $i<strlen($output); $i++) {
$ny = rand(200,245)/10;
$angle = rand(-22,19);
imagettftext($image, $font_size, $angle, $x, $ny, $text_color, $font, $output[$i]);
$x += rand(16,18);
}
} else {
die('Font file does not exist.');
}

Now our image has been created, and our SESSION variable has been set, we are almost finished. All that is left is to set our headers, and send the image off.

1
2
3
4
5
6
7
8
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

We do not ever want to cache our images, and we always want to destroy them after we send them.

Now, in whatever page you choose to have this verification work, you simply need to add a session_start(); and in an image tag, point the source to this php file.

1
<img src="image_verification.php" title="Verify" />

You should be able to figure out the rest when it comes to comparing the user input with the SESSION variable.

Optimizing PHP & SQL

Tuesday, May 17th, 2005

Commonly we see php being used as an interface between the client and the database providing the information. Very few implementations utilize optimization, and thus, reduce performance. Listed below are many techniques that should be used to optimize php to sql communication.

  1. Do not use SELECT * unless you must
  2. Use SELECT priorities for better control
  3. SELECT var IN ([constance,...]) is very fast
  4. Use default values for INSERT when you can
  5. Do not create indexes you are not going to use
  6. Indexes are good for reads, but bad for writes
  7. When joining tables, use numbers instead of strings if you can
  8. Create your tables with a fixed-table format if possible
  9. Use OPTIMIZE TABLE and ANALYZE TABLE regularly

There are other techniques both in php design and database design that will optimize performance, but in terms of php and sql communication, this list will get you running more efficiently.