// controller function
public getcolors($request) {
$db = CMS::GetDB();
$request->favcolor = $db->transform($db->getRows("select * from favcolor order by color, name"), "collect", "color");
}
// and the template
{@ favcolor ?
"<ul>"
`<li><strong>{$key}</strong>: {$:implode(", ")}</li>`
"</ul>" @}
the template uses a control structure called a conditerator (more info at the site). The controller uses a data transform to convert the set of rows into a hash of rows keyed on color.I think that is more concise and reads better than the BRL example.
$result = $conn->execute('select * from favcolor order by color, name');
while($row = $result->next()) {
if ($result->groupBeginning('color')) {
echo $row['color'].': '.$row['name'];
} else {
echo ', '.$row['name'];
}
if ($result->groupEnding('color')) echo "\n";
}
echo 'Count:'.$result->count();
I just came up that off the top of my head, but there's nothing impossible in there. Also, instead of field name for groupBeginning(), passing a closure would also be possible. echo $conn->execute('select * from favcolor order by color, name')
->groupBy(L(val)->color)
->map(L(key)->wrap('<li><b>', '</b>')
->append(
': ',
L(val)->map(L(val)->name)->join(','),
'</li>'))
->join("\n");Also http://commonphp.blogspot.com is where I write about it.
sql_apply("select id,somefield from sometable","applicator_function");
PHP is a programming language, it was not designed for 'simple web/database apps' any more than any other language, you can use the building blocks provided to create access at the level you require. But you can customize it so that it does become usable for 'simple web/database apps'.The above is a short sample of how I'd do the thing you describe and I don't have to remember to fetch the next row from the table and I don't have to check that I'm at the end of the result set, grouping is as easy as SQL will make it and all cleanup is automatic.
Um, yes it was. See http://en.wikipedia.org/wiki/PHP#History to verify that PHP started as a bunch of CGI binaries to serve some simple dynamic web pages. The database bit came later, but at the start it was explicitly designed for quick and dirty web pages.
You could make such statements about every other mainstream language.
I guess we can't blame PHP for preserving the $ in front of variable-names with all the legacy code lying around. But the following should parse in a sane language:
$width = getimagesize($filename)[0];
Adding support for this would not break legacy code. Nobody can tell me that naming all intermediate results like $sizes = getimagesize($img);
$width = $sizes[0];
is such a preferable style, that the parser should enforce it.By the way, this is how PHP pretends to support higher order functions:
function cube($n) {return($n * $n * $n);}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);And yes, you can make such statements about every other mainstream language. For instance Perl was more designed for text manipulation than most other general purpose programming languages. Java was more designed for multi-threading than most other general purpose languages. C was more designed for close to the machine programming than most other general purpose languages.
All of these things are true. All of these things still show in various ways. Yet every one of those is a general purpose language that can be used for anything you want.
As an example, in the Clojure space there are two libraries that are good exemplars of the ends of the spectrum: Hiccup and Enlive.
Hiccup (http://github.com/weavejester/hiccup) uses sexpressions to define the (ht|x)ml output. Effectively either the developer and designer roles are fused or the developer gets a design from the designer and encodes it into hiccup manually.
Enlive (http://github.com/cgrand/enlive) is (in overly simplistic terms) a selector driven substitution engine. You give it an HTML file for a template and some CSS selectors describing what to modify, alternatively you define a snippet in terms of some elements within the page. In this model the designers work is first class resource, and not modified by the developer.
This is exactly the same breadth of options as I've seen in just about any real language being used for development tools.
(Whether MVC is the right style is a topic for another discussion.)
http://brl.codesimply.net/brl_4.html#SEC31
I can't forget to fetch the next row from the database. I can't forget to check that I'm at the end of the result set. Grouping the results is easy and straightforward. The statement and connection objects are automatically closed when the request is done. Compare that to PHP.
I'm getting better, but even today I feel a little irritated when someone says PHP was designed for simple web/database apps. I know what a language designed for simple web/db apps looks like, and PHP is not it.