Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Zend Developer Zone:
Fetching multiple random rows from a database
May 06, 2009 @ 16:17:03

On the Zend Developer Zone, a recently posted tutorial looks at fetching multiple random rows from a database table (in a MySQL database).

As a follow up to my earlier article about fetching a single random row from MySQL I will tell you today, how you can fetch multiple random rows from a table without any hassle. Compared to the solution with fetching a single random row, fetching multiple random rows requires some tricks.

The tutorial walks you through the creation of some sample tables, making a simple trigger to keep a "random ID" column in a value range of one to the number of rows (gapless) and a bit of PHP code to select some IDs from the table and the SQL to get their information.

Be sure to check out the comments for mentions of the number of table reads and how optimized certain parts of the queries might be.

tagged: mysql random gapless trigger row multiple database

Link:

Jani Hartikainen's Blog:
Closures coming in PHP 5.3 and that’s a Good Thing
Feb 20, 2009 @ 20:05:56

Jani Hartikainen takes a look at closures in a new post to his blog - why their good and why they can help you more than you know.

PHP 5.3 will be introducing closures to PHP. Closures, also known as anonymous functions, will allow you to declare functions “inline” and store them in variables. While the syntax may seem a bit weird compared to how it is in languages like JavaScript, closures will be a useful addition to the language. However, like reflection, it’s a feature that may not immediately show it’s usefulness...

He explains what they are (a sort of anonymous function) and a simple example of them in use - a function that works with data from an array to display each of the rows with the given "formatter" function. Another brief example is a method of using them in PHP functions like array_map and usort as an argument for the callback parameter.

tagged: closure php5 good anonymous function example row loop callback

Link:

Lorna Mitchell's Blog:
Introduction to Zend_Db
Oct 31, 2008 @ 17:57:07

Lorna Mitchell has an introduction to Zend_Db, the Zend Framework database abstraction layer posted to her blog today:

I recently worked on a project which was based on Zend Framework - I haven't worked with it before and I was temporarily confused by the existing implementation of some of the database-level stuff. After much reading and untangling of code, I'm now pretty clear how this should look, so here's my overview. I'm not going to go into setting up a whole application, but this is a quick primer on how data models go together.

She talks about table modeling, fetching rows/data from the model and working with rows/rowsets.

tagged: introduction zenddb database zendframework fetch row rowset

Link:

Jani Hartikainen's Blog:
Understanding Doctrine's NestedSet feature
Sep 02, 2008 @ 15:29:56

On his CodeUtopia blog Jani Hartikainen gives an inside look at a feature of Doctrine, nested sets.

The Doctrine library comes with a feature called nested set, which makes saving trees in a database easy. However, it's quite easy to accidentally cause a lot of extra unneeded queries if not being careful. Here are some pointers to keep in mind while working with the nested set, and some example queries to make understanding it easier.

He gives an example, showing how to get rows from the database - parent and child - and some optimization tips to keep things light. There's also some pros and cons included for doing it either way (the standard fetching or using the more optimized versions).

tagged: doctrine nestedset feature fetch database row parent child

Link:

DevShed:
Using Timers to Benchmark PHP Applications
Apr 30, 2008 @ 19:31:08

DevShed has posted the second article in their series looking at benchmarking your PHP scripts. This time they look at the use of "timers" to check on script execution length.

Since you already know how to create timer functions and classes, in this tutorial, I'll create some concrete examples that show where these timing mechanisms can be applied in order to evaluate the performance of certain PHP applications.

They create a few classes that connect to a database and pull out rows (handing it off to a Result class). They use these classes in two examples - one with compression (output buffering) and one without using their Timer class to evaluate the differences.

tagged: timer benchmark application tutorial class mysql row output buffering

Link:

WebReference.com:
How To Create Alternating Row Colors in PHP
Jun 20, 2007 @ 16:12:00

WebReference.com has a simple new tutorial today showing how to create alternating table rows in the output for your application.

Almost every database driven Web site uses alternating row colors when displaying a list of records, a concept used all over the Internet. You've seen it used, you like it, and now you want to do it yourself. But you're not sure how to accomplish it. Well you've come to the right place. I'm going to discuss the basic idea behind achieving this and give some concrete examples demonstrating different ways to implement it.

Their script loops through the information (numbers in their case) and checks, using the modulus operator to check and see which of the CSS styles it should apply to the div. To give a range of possibility, they also show how to do it with a while loop instead of a for to do a little benchmarking of the methods.

tagged: alternate row color table forloop whileloop css alternate row color table forloop whileloop css

Link:

WebReference.com:
How To Create Alternating Row Colors in PHP
Jun 20, 2007 @ 16:12:00

WebReference.com has a simple new tutorial today showing how to create alternating table rows in the output for your application.

Almost every database driven Web site uses alternating row colors when displaying a list of records, a concept used all over the Internet. You've seen it used, you like it, and now you want to do it yourself. But you're not sure how to accomplish it. Well you've come to the right place. I'm going to discuss the basic idea behind achieving this and give some concrete examples demonstrating different ways to implement it.

Their script loops through the information (numbers in their case) and checks, using the modulus operator to check and see which of the CSS styles it should apply to the div. To give a range of possibility, they also show how to do it with a while loop instead of a for to do a little benchmarking of the methods.

tagged: alternate row color table forloop whileloop css alternate row color table forloop whileloop css

Link:

MSBWare.com:
Alternating Row Colors
May 02, 2007 @ 21:13:00

Sometimes, a simple approach is the best. That's what Michael Berman went with in this light and easy tutorial. It focuses on the creation of an "alternating rows" script with PHP.

This concept is used all over the internet. Almost every database driven website uses alternating row colors when displaying a list of records. You've seen it used, you like it, and now you want to do it yourself. But, you're not sure how to accomplish it. Well you've come to the right place. I'm going to discuss the basic idea behind achieving this and give some concrete examples demonstrating different ways to implement it.

The example code show how to alternate between two colors - white and grey - for the background of a series of DIVs. He even includes a series of tests to show which method of switching them out would be faster (while loop, for loop, etc).

tagged: alternate row color loop benchmark tutorial alternate row color loop benchmark tutorial

Link:

MSBWare.com:
Alternating Row Colors
May 02, 2007 @ 21:13:00

Sometimes, a simple approach is the best. That's what Michael Berman went with in this light and easy tutorial. It focuses on the creation of an "alternating rows" script with PHP.

This concept is used all over the internet. Almost every database driven website uses alternating row colors when displaying a list of records. You've seen it used, you like it, and now you want to do it yourself. But, you're not sure how to accomplish it. Well you've come to the right place. I'm going to discuss the basic idea behind achieving this and give some concrete examples demonstrating different ways to implement it.

The example code show how to alternate between two colors - white and grey - for the background of a series of DIVs. He even includes a series of tests to show which method of switching them out would be faster (while loop, for loop, etc).

tagged: alternate row color loop benchmark tutorial alternate row color loop benchmark tutorial

Link:

DevShed:
Using Unbuffered Queries and More with SQLite with PHP 5
Dec 11, 2006 @ 18:46:00

Continuing with their look at PHP5 and SQLite, DevShed has posted part two of the series - "Using Unbuffered Queries and More with SQLite with PHP 5".

Taking into account the group of characteristics that I mentioned [a few lines above], in this second tutorial of the series, I'll be taking a look at some of them. This will give you a clearer idea of how to use them as part of your existing and -- why not? -- future PHP applications.

They take a look at the hows and whys of working with the unbuffered queries and well as some examples - counting rows and fields in a database table and looking at the use of more "row-processing methods" to get different sorts of data out.

tagged: unbuffer query sqlite part2 php5 row field processing unbuffer query sqlite part2 php5 row field processing

Link:


Trending Topics: