Monthly Archives: August 2008

Generate Javascript Files with PHP

There are times when you might want to fetch stuff from a database using PHP and then spit out a JavaScript file. For example, many image slideshows are controlled by javascript – but what if the image references are stored in MySQL? You can use PHP to fetch the info, and then include the javascript/php file in the <head> of your HTML page, something like this:

<script type="text/javascript" src="/includes/slideshow.js.php"></script>

The above file is being called by the browser as a JavaScript file – but because it has the “.php” extension, the server knows that it has some php work to do before it delivers the file to the browser.

However, there’s one gotcha…

My JavaScript File Doesn’t Have Line Breaks!

I admit I had to beat my head on the desk for awhile, until it dawned on me that this could be fixed by adding a simple header call to the top of the PHP file:

Header("content-type: application/x-javascript");

Now, all output will maintain the linebreaks that javascript so depends on. In the PHP code, just add the newline character where you need a line break, like so:

echo  "\n";


I’m Still Here!

I haven’t had a new post in awhile, so I just wanted to drop a quick “hello!” and let folks know I’m still alive! It’s just been an incredibly busy summer for me. I’ve been working too much, traveling, working on the house, gigging (I play drums), seems everybody I know is getting married, etc. etc.

On top of that, I’ve been dealing with some wrist/arm pain from over working – so last month was a super drag because I had all of this work to do, but could only spend so much time in the office because I didn’t want to make my pain-issues any worse (I’m wearing wrist braces as I type this – they help).

So, all this will eventually lead into a post on keeping those hands and arms healthy, and what to do when they aren’t feeling so good. My wife reminded me this is something I seem to go through every summer, and I’ve learned a few things about managing it that I hope to share when things calm down a bit.