Blog Tag: Php
Testing Should Be Hilarious
2 Comments | Latest by: Karin | Add A Comment! »»
I just wrote the following code. All I'm doing here is simply making sure an include file is found properly, so it's function can be called:
<?php
include_once('system/stats/admin.php');
return Notify_admin_main();
?>
# system/stats/admin.php
<?php
function Notify_admin_main()
{
die('smell my butt');
}
?>
I crack myself up!
Obviously:
- I edited the die message for this entry. In dev it was dirtier.
- When I can see 'smell my butt' on the web, I'll immediately be replacing the Notify_admin_main() function with something useful.
Dare to be happy! And silly...
Different Blogging Concept
Tumblelogs
I've been noticing tumblelogs such as my personal favorite, Projectionist or the Zen Habits Tumblelog. A tumblelog is much like a linkblog, only instead of just links and witty summaries, you have varying types of items including, links, quotes, code samples, chat log snippets, images, and movies. If I'm off on my simple description of tumblelogs, please correct me in the comments.
I like tumblelogs. The simplicity speaks to me.
What Did You Do Last Week, Mr. Gonzalez?
Another pair of thoughts that have been rolling around in my head today are Alberto Gonzales (Conservapedia Entry, for kicks) not being able to recall details of his daily work a week later when being questioned by congress, and, secondly, the various ways I have tracked my daily activities in my professional career.
Kases?
In the new 37 Signals application Highrise, (37 signals are the fine people who brought you Basecamp), they have a section for Cases, i.e. like a project or series of tasks - as in "I'm on the case, cheif!".
The url for this is /kases. I did a quick search and found out why the url is /kases. It turns out to be a work around for a language key-word colision in Ruby, which gets exposed in Rails.
Not to be smug (ok, a little...), but I am suddenly quite thankful for the "do it by hand" nature of my current software set-up - build on fusebox for PHP. I feel like posting several new pages on podo, like: /if, /case, /switch, /foreach, etc.
SQL Foo
I was having a problem on this blog. I wanted to get the recent entries, a count of their comments, and a list of their tags. The query went something like this (this is just a sample; the real one is basically the same, but grabs more columns):
select entry_id, title, count(comment_id) as comments, group_concat(tag) as tags from entries e left join comments c on e.entry_id = c.content_id left join entries_tags et on e.entry_id = et.entry_id left join tags t on et.tag_id = t.tag_id group by e.entry_id order by e.created_at desc limit 0,2
This worked almost perfectly, except I started noticing that entries with more than one comment and more than one tag had incorrect results. Here's an example from a recent entry:
entry_id: 256
title: Royalty
comments: 20
tags: royalty,comments,royalty,comments,37s,royalty,blog,comments,37s,blog,37s,blog,37s,blog,37s,royalty,blog,comments,royalty,comments
Hmm... That doesn't look right.
select count(*) from comments where content_id = 256; +----------+ | count(*) | +----------+ | 5 | +----------+
select count(*) from entries_tags where entry_id = 256; +----------+ | count(*) | +----------+ | 4 | +----------+
OK, Five comments and four tags. "royalty,comments,37s,blog" to be exact. What's going on here? Well, the 20 comments in our result set above gives us a clue. The comments and tags are being multiplied. Duh! OK, I figured that out in like 2 seconds. But how do I fix it? I want it to say 5 comments, and only list the 4 related tags.
Enter the DISTINCT key word. I didn't know you could do this until tonight when I stumbled across it almost by accident - scouring the docs for the answer to a similar, but unrelated problem. The query now becomes:
select entry_id, title, count(distinct comment_id) as comments, group_concat(distinct tag) as tags from entries e left join comments c on e.entry_id = c.content_id left join entries_tags et on e.entry_id = et.entry_id left join tags t on et.tag_id = t.tag_id group by e.entry_id order by e.created_at desc limit 0,2
Note the placement of the distinct keywords. The result is more like what we want! Hooray!
entry_id: 256
title: Royalty
comments: 5
tags: blog,37s,royalty,comments
Hopefully, this helps other SQL noobs like me! Now if I could just get that other query to work right, we might have something...
Cheap Output Buffering
A common way for developers to make standard layouts in PHP is to create header and footer include files and then call them from each page. Like so:
<? include('header.php'); ?>
<h2>Web Page</h2>
... etc ...
<? include('footer.php'); ?>
Something I learned using Fusebox is to create layout files that are called last, store the content into a variable and echo the content in the layout file. This method is preferable to me instead of using headers and footers, since it lets you edit your layout files as a whole.
Application Scope for PHP
1 Comment | Latest by: hemant | Add A Comment! »»
With the help of David Huyck, I have developed a small class file to handle Application Scope in PHP. Check it out and laugh at my amateurish code. Please send me any comments or post something on the SourceForge forum. This AppScope file is being used in the new PHP Fusebox core that several people are developing.