Showing posts with label data driven. Show all posts
Showing posts with label data driven. Show all posts

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.

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.