Monday, August 6, 2007

PHP: count($var) in loops

I can remember -- when I was in college -- someone mentioning that
for(i=0;i<array.length;i++)
was a bad idea, because you were calling the length function every time through the loop. The better way was to set a variable to the length and use that. I never saw any speed difference tho, so I just kinda skipped that step every time.

However, in a site I recently built, the menu was database driven and it seemed to be taking forever to load. I was going thru the code trying to optimize it and found some places in the PHP where I was using count($array) in loops. So, just for the fun of it, I changed my code to
for($i=0, $num=count($array);$i<$num;$i++)
and it fixed all the issues. Granted, I was probably looping between 3-400 times, but the change probably saved 3-4 seconds of render time. Pretty cool. So anyway, keep in mind that those little optimizing hints actually do work.

Wednesday, April 25, 2007

PHP: mysql_fetch_row vs mysql_fetch_object

This is probably another one that everybody and their brother knew except me. I've always used mysql_fetch_row() to parse thru SQL results in PHP, but recently discovered mysql_fetch_object().

With mysql_fetch_row, you get an array of the items in the row... meaning if you queried the database with 'SELECT id, name, date FROM table', and call $row = mysql_fetch_row($query), then $row[0] is the id, $row[1] is the name, and $row[2] is the date. Seems pretty easy, until i found mysql_fetch_object.

Using that command, those same array vals are now object vals, so you can reference $row->id, $row->name, and $row->date. A little more typing, a lot easier to read. I think I'll stick with it.

Friday, April 13, 2007

CSS min-height and IE6

Today (for the last month, but i just attacked it today) I was trying to get someone else's CSS design from a template to a fully database driven page. I had it working in Firefox and IE7, but I don't have IE6 on my machine so I hadn't really tested it much on that. I had heard there were problems, but wasn't worried about them until today. So today, I discovered a couple fun problems with IE6. One is that min-height doesn't work very well on things that aren't tables. (I thought the whole point, or a big point, of CSS was to get rid of tables.) So in order to make my min-heights work in IE6 without breaking anything else, I used a little trick i found at a blog by Dustin Diaz (which is appropriately ranked #1 on Google for 'min-height IE6' searches). Apparently this problem has been around a while, I've just been busy using Flash. Good deal for me, it's already fixed.

Add this to your classes that have min-height in them:
class {
min-height:500px;
height:auto !important;
height:500px;

}

Worked like a charm. For me and about 500 other people who commented about it on his blog.

Thursday, April 5, 2007

Gimme back my cursor

Here's something I read this morning that reminded me of something I hate. Normally when you open a web browser, by default, your cursor is in the address bar. But some sites steal your cursor and move it where they want it. For example, Yahoo! moves it to the search bar of their homepage. That makes it hard to type a new address into the address bar and move on when you don't want to be on Yahoo!'s page. I thought maybe I was the only person bothered by this, since there is no sign of it changing, but thankfully I'm not.

Saturday, March 31, 2007

crossdomain.xml

Lately, I've run into a couple problems trying to work with Flash across domains. In one case another domain was trying to pull information from my server and couldn't. In the other case, I was trying to pull info from another domain (an rss feed specifically) and couldn't get to it. These problems arise from XML.load, XML.sendAndLoad, LoadVars.load, and LoadVars.sendAndLoad calls to files on other domains.

In the first case, I solved it by adding a cross domain policy on the web server. The cross domain file looks like this:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="www.domain.com" />
// repeat as needed
</cross-domain-policy>

Name that file 'crossdomain.xml' and place it in the root of your web server, and it allows each listed domain to pull from the server into any .swf that calls php scripts or xml files or whatever.

For more information on crossdomain policies, check out Adobe's Tech Note.

In the second situation, I didn't have access to the server I was getting info from. Safari's Activity Window was telling me that Flash was looking for and not finding 'crossdomain.xml,' so I was afraid I was out of luck, short of asking the webmaster of the feed to create the file and add my domain to it (which is prob unlikely to happen). However, I figured out I could create a file on my server to act as a proxy for that file, thanks to another Adobe Tech Note. I used the Server-Side Proxy method.

In that method, you create a php script which reads the file on the remote server and "recreates" it on your own, like so:

<?php
$dataURL = "http://www.domain.com/feed.rss";
readfile($dataURL);
?>

This file reads and ouputs the exact same file. So I saved that file as 'rssProxy.php' and put it on my server. Now 'http://www.myserver.com/rssProxy.php' is exactly the same page as 'http://www.domain.com/feed.rss' and I can call that file into XML.load.

So there's my solutions to both cross-domain problems that Flash has.

What's The Point?

I decided to start this blog for a few reasons.

1) I didn't want to clutter up the blog Katie and I do with a bunch of web crap.
2) I wanted to have a place to remember all these things I have learned.
3) Maybe someone else will find this stuff useful.
4) It's kinda fun to write these things, and I figured this one might be a little more focused than the other.

So stick around and maybe we'll all learn something.