ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

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'] . '';
}
?>

(View Live Demo)

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'] . '';
}
?>

(View Live Demo)

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';
}

?>

(View Live Demo)

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 '
'
;

?>

(View Live Demo)

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.

« Previous: Basic CRUD functionality
Next: Handling relationships & Conclusion »



One Response to “Taking a first look at the AutoCRUD for PHP library”

  1. Mac OS X Things » Blog Archive » PHPit - Totally PHP » Taking a first look at the AutoCRUD for PHP library Says:

    […] 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. […]

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Overview
  2. Setting it all up
  3. Basic CRUD functionality
  4. Selecting data
  5. Handling relationships & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file