In anticipation of installing Drupal on my 1&1 account soon, I went into my control panel and poked around a bit. First, I found that my account was set to use PHP 4. It was pretty easy to switch it to PHP 5. A call to phpinfo() shows that I’m now at 5.2.17. That’s not quite up to date, but it’s probably close enough.
I also looked into the MySQL setup. Several years ago, I set up a MySQL database on my account. That database is still there, at MySQL 4, with a 100 MB limit. Just for the heck of it, I created a new database. The new one is MySQL 5, and has a 1 GB limit. So, that’s nice. (There doesn’t seem to be any way to upgrade the old MySQL 4 db to MySQL 5, but that’s fine, since it’s empty.)
I even went as far as uploading the Drupal 7 tar.gz file today, but the 1&1 web file browser can’t unzip tar.gz files, so I’m going to need to get to a command prompt to do that, and it’s a little late to get into that tonight.
Category: PHP
Drupal 7
After staying out too late Tuesday night, then going to bed at 8:30pm last night, I finally had some free time after work tonight to play around with Drupal. I installed Drupal 7 on my MacBook, following these instructions. The only real trouble I had was in making sure that every host reference was set to 127.0.0.1 rather than localhost, or anything else.
Most of the obvious stuff seems to be working. I haven’t figured out clean URLs yet though.
xAMP on the Mac
I’ve been trying to learn a bit about Drupal recently. It looks like we might be getting some Drupal projects at work, so it seemed like a good idea.
To get a working setup for Drupal on my Mac, I wanted to get all the pieces in place — Apache, PHP, and MySQL, basically. Apache is, of course, already there, and I already had that turned on, so no problem.
PHP was already installed, but apparently got turned off during the 10.7 upgrade. All you need to do to turn it on is edit httpd.conf, and uncomment one line, per this SO page.
For MySQL, there’s an installer that works pretty smoothly, per this page. One odd thing I stumbled across at one point is that you usually need to refer to your local server as ‘127.0.0.1’ rather than ‘localhost’. Long story, but something worth noting. Also, if you’re not sure how to set the root password, take a look at this SO page.
I tested to make sure that MySQL was working from PHP using this little test script:
<?php $db = mysql_connect("127.0.0.1:3306", "root", "password"); if (!$db) { die('Could not connect' . mysql_error()); } echo 'Connected successfully'; ?>
I can’t remember exactly where I found that, but it’s a pretty basic script.
I then got a little ambitious and decided to try to get phpMyAdmin working. I made a couple of simple mistakes here, including not quite understanding that config.inc.php needed to be in the root phpMyAdmin folder and not in the config subfolder.
Also, the warning from phpMyAdmin about mcrypt not being installed was bugging me, so I decided to try and fix that. That turned out to be kind of complicated. I followed these instructions, and they worked, but only on my second try. I must have gotten something wrong on the first try. Also, I found another page with similar instructions, so referencing that may help if anything on the first page seems confusing.
In the end, I think I really should have just gone with MAMP, but of course I was doing this as a learning exercise, so it was valuable to go through all this, even if it took a lot longer than was probably necessary.
And I still don’t have Drupal installed. Maybe tomorrow!
PHP tip of the day
If you’re running an array through json_encode(), and part of that array is a reference to another array, do _not_ json_encode() *that* array first. JSON-encoding something that’s already JSON results in… confusion. This is one of those things that I would have figured out a lot faster if I had any kind of debugging set up for PHP. Maybe I should look at FirePHP. I don’t do a lot of PHP work, but I’m using PHP right now to test some REST web services that I’m writing in .Net/C#. (Debugging the .Net code, of course, is a piece of cake.)
Blogger label cloud
I just found a PHP script by this guy that does pretty much the same thing that my script does, except in the form of a cloud instead of a list. Nice. He also uses a cache file so he doesn’t have to read the directory on every call. Both of those were things that I had thought about adding to my PHP program. Now I can probably just borrow this guy’s code.
more fun with Blogger tags and PHP
I wanted to enhance my tag list PHP program to do a couple more things. Basically, I wanted it to be aware of when we are on a label page, so that we could make the list item for the current page plain text (instead of a link), and so that I could put a link back to the main page on any label page.
Normally, I have a link back to the main page in the header of any page other than the main one. I do this through the Blogger template, using the “ItemPage” and “ArchivePage” Blogger template tags to figure out when we’re not on the main page. Unfortunately, there’s no “LabelPage” template tag, as far as I can tell.
Making one of the list items text rather than a link broke the sorting, since I was just sorting on the text of the list item, including the “a href…” stuff. To get around this, I switched to using an associative array, where the key value is the tag.
Here’s the code:
<?php $dirname = "."; # current directory $uri = $_SERVER["REQUEST_URI"]; # the page we're on $bOnLabelPage = false; # are we on a label page? $dir = opendir($dirname); $file_list = array(); $i = 0; while (false != ($file = readdir($dir))) { if (ereg(".html$", $file)) { $tag = substr($file, 0, strlen($file)-5); $key = strtoupper($tag); if (ereg("$file$", $uri)) { $file_list[$key] = "<li>$tag"; $bOnLabelPage = true; } else $file_list[$key] = "<li><a href='/labels/$file'>$tag</a>"; $i++; } } closedir($dir); #natcasesort($file_list); ksort($file_list); ?> <h1>My Tags</h1> <?php if ($bOnLabelPage) echo ("<a href='/'>[Back to main page]</a>"); ?> <ul> <?php foreach ($file_list as $file) echo($file); ?> </ul>
A little more complex than what I started with, but still not too bad.
As a side note, I am using TextWrangler to edit PHP files on my Mac, and it’s working pretty well. It’s got syntax-highlighting for PHP and HTML. I’m also now using Fugu to copy files up to my server. It integrates well with TextWrangler, so that I can just keep a file open in TextWrangler and have it copied back to the server every time I save. Nice.
On Windows, I’m mostly using Multi-Edit and WinSCP. That combo works pretty well too, though I’m using an older version of Multi-Edit that doesn’t have PHP syntax highlighting.
sorted list of tags
OK, this time I’ve revised my PHP code to show a sorted list of labels:
<?php $dirname = "."; # current directory $dir = opendir($dirname); $file_list = array(); $i = 0; while (false != ($file = readdir($dir))) { if (ereg(".html$", $file)) { $tag = substr($file, 0, strlen($file)-5); $file_list[$i] = "<li><a href='/labels/$file'>$tag</a>"; $i++; } } closedir($dir); natcasesort($file_list); ?> <h1>My Tags</h1> <ul> <?php foreach ($file_list as $file) echo($file); ?> </ul>
Not a big deal, but it does show a (probably) typical use of arrays in PHP. As a side note, the “foreach” construct in PHP is a bit different from the usual foreach in other languages. It’s usually something like “foreach (item in list)”, while in PHP it’s reversed: “foreach (list as item)”. I should also mention that I first tried the regular sort() function, but switched to natcasesort(), since a case-insensitive sort makes more sense here.