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

Laravel News:
Utilizing Laravel’s Cache with Query Params
Apr 04, 2017 @ 14:27:53

On the Laravel News site there's a quick tutorial posted showing you how to use Laravel's query cache with query params to help increase the performance of your application by caching query responses.

Laravel provides a very intuitive and useful means of caching the responses of your projects, whatever your project is (RESTful API, Web Platform, etc.). In general, Laravel can store in the cache system whatever data you send (HTML, JSON, collections, Eloquent instances, and similar) accordingly with a provided expiration time.

[...] The question here is “How does Laravel determine when to store data?”

They give an example of saving a "remember" key value to the cache manually using the "remember" method on the Cache facade. He then talks about what happens internally when the "remember" method is called to know if the data was previously cached. It then moves into the caching of data based on URL values and how query params would confuse things and not provide much benefit to the caching. Thanks to some internal handling the caching ignores the query params and returns the same data as before. This is an issue if you want the updated data but is relatively easily solved with a bit of code to sort the params and normalize the URL being used as the "remember" key.

tagged: laravel cache query params tutorial sort normalize

Link: https://laravel-news.com/cache-query-params

Giorgio Sironi:
Property-based testing primer
Jun 19, 2015 @ 17:15:29

Giorgio Sironi has a new post to his site today talking about a method (and tool) around a different sort of testing practice: property-based testing. The difference is that, instead of hard-coding values and evaluating them post-processing, you're generating random values and ensuring they match against a set of properties, not values.

I'm a great advocate of automated testing and of finding out your code does not work on your machine, 30 seconds after having written it, instead of in production after it has caused a monetary loss and some repair work to be performed. [...] However, classic test suites written with xUnit and BDD styles have some scaling problems they hit when you want to exercise more than some happy paths. [...] Property-based testing is an approach to testing coming from the functional programming world.

He helps to make the point a bit more clear with an example of testing the "sort" function and its results. He talks about how to test it using normal data, empty data, etc. but notes that this kind of testing can become "boring and error-prone". Instead he proposes the property-based testing of the results. He generates random values to feed into the method and checks to ensure the results are sorting by comparing them to each other. He expands this with a bit more complex example, showing how to test some DateTime handling and evaluating the result with logic in a closure. To help make this kind of testing easier, he's created a library (Eris) that extends PHPUnit and provides the methods seen in his examples.

tagged: property testing unittest phpunit extension random datetime sort eris

Link: http://www.giorgiosironi.com/2015/06/property-based-testing-primer.html

PHPEasy.co.uk:
Sorting array data using SplMaxHeap
Jul 18, 2012 @ 13:17:25

On PHPEasy.co.uk today there's a new tutorial posted showing you how to sort using the SplMaxHeap functionality provided by the Standard PHP Library.

The SplMaxHeap class basically stores numbers in any order, and then returns them in descending order. In order to make this class useful for other data types other than numbers I have extend the SplMaxHeap class to compare dates and strings. SplMaxHeap extends SplHeap and implements the abstract class SplHeap::compare. Our extention of SplMaxHeap is therefore expected to implement the compare method.

Code examples are given showing sorting by things like "last name" and "date of birth" (using strtotime). There's also an example of its use in sorting values from an array and outputted in a loop. You can find out more about this functionality in the PHP manual.

tagged: sort data splmaxheap compare function tutorial

Link:

PHPMaster.com:
MongoDB Revisited
Jan 17, 2012 @ 18:44:07

In this new post to PHPMaster.com today Ahmed Shreef continues on from his previous introduction to MongoDB and gets into more detail on things like cursors, query operators, queries on embedded documents and the sort/skip methods.

In my previous article Introduction to MongoDB I discussed installing Mongo, its PHP extension, and how to perform simple insert and find operations. Of course there are many, many more features than what I mentioned so I wanted to write another article to show you some of them.

Other topics mentioned include queries on arrays of data and running queries with indexes to improve their performance. Code is also included for each example.

tagged: mongodb tutorial cursor query index sort skip

Link:

PHPBuilder.com:
PHP Arrays: Advanced Iteration and Manipulation
Dec 09, 2011 @ 18:50:11

In this new tutorial from PHPBuilder.com, Jason Gilmore shows you some of the more advanced things you can do with arrays in PHP (specifically in the areas of iterating through them and manipulating their contents).

Sporting more than 70 native array-related functions, PHP's array manipulation capabilities have long been one of the language's most attractive features. [...] There are however many array-related tasks which ask a bit more of the developer than merely knowing what part of the manual one needs to consult. Many such tasks require a somewhat more in-depth understanding of the native features, or are possible only when a bit of imagination is applied to the problem.

In his examples he shows how to do things like sorting a multi-dimensional array, iterating recursively (with the help of a RecursiveArrayIterator), converting an object to an array and doing "natural" sorting on an array's contents.

tagged: array manipulation advanced iteration spl recursive sort

Link:

Chris Shiflett's Blog:
Sorting Multi-Dimensional Arrays in PHP
Jul 01, 2011 @ 14:48:26

In a sort of "remind himself later" kind of post, Chris Shiflett has shared a quick example of a common task PHP developers face sorting a multi-dimensional array.

Every time I need to sort a multi-dimensional array in PHP, I have to remind myself how to do it. It's not quite as quick and easy to look up as most things, so I'm going to blog a quick example. I've always felt like there must be a better way to do this, so please let me know if there is, and I'll update this post accordingly.

His method sorts a multi-dimensional array of user data using the array_multisort function together with a list of the usernames in each record. In the comments, people recommend using usort instead with a callback+closure combo that keeps things nice and clean.

tagged: sort multidimensional array hint

Link:

Brian Swan's Blog:
Sorting Azure Table Entities by Timestamp with PHP
Apr 27, 2011 @ 15:48:17

Brian Swan has a quick new post to his blog for the PHP + Azure users out there on how to easily sort table entities by a timestamp through the Azure SDK for PHP. The timestamp is a property on the parent storage table class, so you don't need to create it in your entity, it's already there.

The problem boils down to sorting an array of objects by a timestamp property, so the information here is nothing that hasn’t been done before. However, after spending some time looking for a way to use a filter in the Windows Azure SDK for PHP API, I didn’t find one, so I’m hoping this post might save others some time. In the end, my solution simply uses the PHP array_multisort function.

Sample code is included - a basic class extending Microsoft_WindowsAzure_Storage_TableEntity, a fetch for the entity set of the contacts and using a custom "objSort" method that sorts based on the returned timestamp values from the object set.

tagged: azure sort table timestamp arraymultisort tutorial

Link:

Chris Hartjes' Blog:
Sorting Relationship Results In Doctrine 1.2
Feb 05, 2010 @ 16:51:32

Doctrine allows you to set up relationships to link data in various tables together. Unfortunately, those aren't always in the order they need to be in. In a new post to his blog Chris Hartjes shows you how to sort these relationship results just by adding a simple line to your request.

I started digging around via search engine. Took me about an hour to find the solution. First, it took me half the time to dive deep enough to find out WHERE I can define the default sort order. Surprisingly, it was in an area that made total sense but I could not find before.

You can see an example of it in the "hasMany" call in his code snippet - the addition of the "orderBy" option and the value showing the sorting order. Here's the StackOverflow page that gave him the answer he needed.

tagged: relationship doctrine sort tutorial orderby

Link:

Zend Developer Zone:
Paging and Sorting Data with Zend Framework, Doctrine and PEAR (part 2)
Jan 04, 2010 @ 17:52:33

The Zend Developer Zone has posted the second part of their look at pagination with the combination of the Zend Framework, Doctrine and PEAR and how the Zend_Paginator component compares to the PEAR and Doctrine alternatives.

In the previous segment of this article, I introduced you to the Zend_Paginator class, which provides a flexible API for paginating any data collection, whether it is expressed as an array or a database result set. [...] This article will explore two such alternatives, the PEAR Pager class and the Doctrine Pager class, and give you a crash course in how you can use them to quickly add paging and sorting features to your PHP application.

The article gives examples for both of the other methods - a simple pagination of database information with the PEAR pager component (and other packages that can make the results more effective) and the creation of a Doctrine instance where the results are handled via a series of built-in method calls.

tagged: tutorial zendframework pear doctrine pagination sort

Link:

PHPFreaks.com:
PHP Custom List Order
Dec 04, 2008 @ 22:00:07

The PHPFreaks site has a new tutorial posted today showing how to create a method for custom list ordering in PHP.

So you have some tabular data printed out in your browser. You can even change the order of the information by clicking on the column name at the top of your list. But can you make your own custom list order? Or maybe you're deciding to make a content management system (CMS). You're making your features modular, so it's easier to add/remove modules. How do you go about displaying them in a custom order in the browser?

They create some MySQL tables to store the data in and set off into the code. The sorting allows for the data to be ordered by any column in either ascending or descending order based on the users' clicks. The results are output in a standard HTML table.

tagged: tutorial custom list sort ascending descending

Link:


Trending Topics: