Taking a first look at the AutoCRUD for PHP library
(Page 4 out of 5)Selecting data
AutoCRUD for PHP provides two methods for selecting data: select() and get(). The select() method is used to select a list of records, and is used like this:
// ...
// setup AutoCRUD
// ...
// Get a list of articles
$list = $crud->article->select();
echo 'Number of records: ' . count($list) . '';
foreach ($list as $article) {
echo $article['title'] . '';
}
?>
The select() method will return an array of all the records that were retrieved. If there are no records it will return an empty array.
You can specify a custom WHERE clause to select only certain articles by using the 'where' property, like we saw with the update() and delete() method. It's also possible to specify a custom ORDER BY clause by using the orderby propery. See the example below:
// Specify WHERE and ORDER BY
$crud->article->where = "articleid != 6";
$crud->article->orderby = "title DESC";
// Get a list of articles
$list = $crud->article->select();
echo 'Number of records: ' . count($list) . '';
foreach ($list as $article) {
echo $article['title'] . '';
}
?>
The articles will now be sorted on the title in descending order, and the article with articleid 6 won't be in the list.
The library also makes paging really easy. If you have a table with a lot of records, you might want to display a certain number of records per page, and let users navigate through the pages. Without a library this causes a lot of hassle, but AutoCRUD for PHP makes it really easy for you. All you have to do is set a few properties, and away you go:
// Do paging
$crud->article->paging = true; # Enable paging
$crud->article->perpage = 2; # Set number of records to show per page
$crud->article->currentpage = (empty($_GET['page'])) ? 1 : $_GET['page']; # Set current page
// Get a list of articles
$list = $crud->article->select();
echo 'Number of records: ' . count($list) . '';
foreach ($list as $article) {
echo $article['title'] . '';
}
if ($crud->article->currentpage > 1) {
echo '($crud->article->currentpage-1) . '">Previous Page ';
}
if ($crud->article->currentpage < $crud->article->totalpages) {
echo ' ($crud->article->currentpage+1) . '">Next Page';
}
?>
As you can see there's nothing to it, and it takes only three lines of code to be able to paginate the results.
The other method to get records is get(), and it's used to get a single record. Instead of returning an array of records, it will return a single record. You can pass the primary key to it as the first argument, but you can also leave this out, and use a custom WHERE clause. See the example below for a demonstration of the get() method:
// Get article #5
$article = $crud->article->get(5);
if ($article == false) {
echo 'Article doesn\'t exist';
}
echo ''
;
print_r ($article);
echo '';
?>
As you can see, the get() method will return false if the record you tried to select doesn't exist.
Now that we know how to select data, let's have a look at something that makes this library unique: the ability to automatically handle relationships.
June 8th, 2006 at 10:33 pm
[…] PHPit - Totally PHP » Taking a first look at the AutoCRUD for PHP library In this article we’ll take a look at the AutoCRUD for PHP library, which is a database abstraction library specifically for MySQL. I’ll take you through all major features of this library, and demonstrate everything with examples. […]