Archive for the ‘Script snippet’ Category

Force File Download

Friday, February 9th, 2007

How to force a file to download (without having to zip it up).

$filename = "secure/writeToFile.doc";

header("Content-Length: " . filesize($filename));
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename=writeToFile.doc');

readfile($filename);

a few other content types - lots more at the above link…

"pdf": "application/pdf"
"zip": "application/zip"
"xls": "application/vnd.ms-excel"
"ppt": "application/vnd.ms-powerpoint"
"gif": "image/gif"
"png": "image/png"
"jpg": "image/jpg"
"mp3": "audio/mpeg"
"mp3": "audio/mp3"
"wav": "audio/x-wav"
"mpe": "video/mpeg"
"mov": "video/quicktime"
"avi": "video/x-msvideo"

Store all post variables/values in a session

Wednesday, February 7th, 2007

This is one of those things I use all the time but because I can’t remember things like this, I always have to look for the exact code to do it…

This will take all the values submitted in a form and store them in a session:

foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}

And when you’re ready to dump the session values…

session_unset();// reset session array
session_destroy(); // destroy session.

Javascript set selection in select element

Tuesday, February 6th, 2007

I needed to set the selection of a drop down menu. As far as I can tell, if you don’t know the “index” value, then you just have to loop through to set the item as selected. If there’s an easier way to do this, please speak up in the comments. I spent WAY too long hunting for a better solution, but this was the only thing that worked:

for (var i=0; i < document.formname.dropdownboxname.length; i++) {
if (document.formname.dropdownboxname[i].value == “value”) {
document.formname.dropdownboxname[i].selected = true;
}
}

Get current URL with PHP

Monday, January 29th, 2007

Because I was having a “duh” moment. :)

$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]

gifs that execute a php script

Tuesday, October 19th, 2004

Problem: (somewhat in line with my last post) I needed to run a script in the “background”, but I only had the ability to present the script as a .gif.

Solution: actually, I came up with two ways of doing this and both use .htaccess to pull it off.

1) I’m a gif but really I’m a php script
This one is fairly straightfoward. I put my script in a seperate directory, (that doesn’t actually include any images!) and renamed it from filename.php to filename.gif (or filename.jpg) (Yes, I know it’s not really an image file. Hang in there)

Then, in the htaccess file for JUST THAT FOLDER! (similar to this trick) I added this:
AddType application/x-httpd-php .php .jpg .gif

FYI - in your php file (that now looks like a .gif or .jpg) make sure you don’t have any headers/text echoed, and at the end of the file add this to your php code:
header('Content-Type: image/gif');
@readfile( '/SERVERPATH/TO/A/REAL/IMAGE/spacer.gif' );

You can then include the “script” as if it were an image file, it will run the script, but only display a gif to the user.

You can even pass it variables like filename.gif?somevariable=somevalue&anotervariable=anothervalue, but there’s another way to do that too without the ugly URL.

2) I’m a gif that’s actually a php script super-powered with rewrite
Ok, starting from the beginning. You have your script (filename.php) in a folder, add the following to the .htaccess file in that folder:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/(.*)/spacer.gif /YOURFOLDER/FILENAME.php?somevar=$1&anothervar=$2 [QSA]

Before we continue, I must tell you that I dont’ completely understand rewrite rules. There’s a LOT of holes in my knowledge. So if you have a better way, PLEASE post it in the comments. I would LOVE to be able to understand what the hell I’m doing. LOL.

OK, moving along. Now you can call your script by actually calling that “phantom” spacer.gif, with the variables in the URL as if they’re directories like this:

http://www.yourdomain.com/YOURFOLDER/somevalue/anothervalue/spacer.gif

Again, make sure you have this at the end of you php code in your filename.php:
header('Content-Type: image/gif');
@readfile( '/SERVERPATH/TO/A/REAL/IMAGE/spacer.gif' );

Copy values from one field to another with javascript

Friday, October 8th, 2004

Needed to write a javascript that would take the values of one select box - and copy it to all the other select boxes on the same page. I found this script which will take all the values from one set of fields - and copy it to another set of fields (kind of like those billing / shipping forms, where if your billing and shipping address is the same, you just check a box - and it prefills the other set of fields for you with the same info from above). And while it “inspired me” - it wasn’t exactly what I was looking for. In my case - I didn’t know the exact names of all the other fields, and I didn’t even know how many of them there would be - they are generated dynamically. It’s a pretty simple script, but here it is anyway:

<script type=”text/javascript”>
function applyToAll()
{
for (var i=0; i< document.YOURFORMNAME.length; i++) {
document.YOURFORMNAME[i].selectedIndex = document.YOURFORMNAME.NAMEOFMAINSELECT.selectedIndex;
}
}
</script>

So your select box probably looks like this:

<select name=”NAMEOFMAINSELECT”>
<option value=”1″>1</option>
<option value=”2″>2</option>
… etc. …
</select>

and then, next to the select box (which will be the one all the others are based on) put a link like this:

<a href=”javascript:applyToAll()”>Apply To All</a>

PHP: working with multiple databases

Tuesday, June 15th, 2004

I’m in the process of moving a few other blogs over to use WP - one of which makes calls to a seperate database unrelated to WP. This proved to cause a conflict. After many hours of trying to figure out where the specific conflict was - I narrowed it down to my mysql_select_db line. Apparently selecting your database this way - even if you close your mysql connection - and then select a different database later on - it can cause you headaches (and make you stay up later than you had intended to) LOL! =Yawn=

The remedy was specifying the databasename when referencing the table and avoiding using the mysql_select_db line altogether.

So instead of code that looked like this:
$databaseConnection = mysql_connect($databaseServer, $databseUsername, $databasePassword) or
die ('I can't connect to the database.');
mysql_select_db($databaseName,$databaseConnection);
$query = "SELECT * from tablename;";
$result = mysql_query($query);

I changed it to this and it seemed to work:
$databaseConnection = mysql_connect($databaseServer, $databseUsername, $databasePassword) or
die ('I can't connect to the database.');
$query = "SELECT * from " . $databaseName . ".tablename;";
$result = mysql_query($query,$databaseConnection);

Quick Tags for comments

Thursday, June 10th, 2004

Since Alex released his js quick tags - it seemed like the perfect thing to add to the comments form so that people could put in the html tags more easily. However, while his script is really powerful - just for the comments form, it’s more than what’s needed. So I stripped it down to the most basic version - using only the most common HTML tags used in comments - italic, bold, and links.

Here are the instructions on how to integrate it into your (wordpress) comments:
(more…)

Random with MySQL

Wednesday, June 2nd, 2004

I have a random quote and also a random link on my sidebar. Since I have 99 links and 190+ quotes, it was easiest to use a MySQL db to manage it. This is the query I was using:

$sql = “SELECT * FROM blogroll ORDER BY rand() LIMIT 1″;

The problem I had was that it wasn’t really random. When I had about 10 links in my blogroll, I hit refresh 80 times and got the first entry EVERY TIME. It worked better when I had more entries, but I still got that first one way too often. I tried to use the mt_rand() function, but it wouldn’t work in the query. I had create a variable that used mt_rand() to pick a number within my range and then call that id in the query. So when I deleted a link or added a new one, it broke.

Then I found the following on php.net:

$sql = “SELECT * FROM blogroll ORDER BY rand(” . time() . ” * ” . time() . “) LIMIT 1″;

This works MUCH better, and it’s showing a lot of links and quotes that almost never showed up before. Hope that helps someone!

Change textarea text color with checkbox and javascript

Friday, May 7th, 2004

Just want to store a little javascript snippet. I needed to grey out the text in a text area depending on if a checkbox was checked or not. I’m sure there’s a way to get the function to be flexible enough so that it can be passed the specific parameters of WHICH form, WHICH checkbox, and WHICH textfield to grey out - but my usage was very limited, so I didn’t bother going further.
(more…)