Archive for the ‘Scripts’ Category

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…)

Mozilla and IE decoder

Tuesday, June 8th, 2004

History: I had previously written this decoder, but it wasn’t compatible with anything but IE. (A frustrating point since I’ve been using Firefox pretty religously). I saw that Mark had written one compatible with mozilla - but the only one there convereted EVERYTHING (making it difficult to go back an edit anything you decoded). I also saw Alex had published his quick tags. So I took a little bit of all of them and created the following script. It can’t be turned into a bookmarklet - because it requires having the fieldname passed to it - but at least I could add back the “decoder” button for comments here. (Although - Mark updated his post with the bookmarklet that will just encode brackets, etc.)
(more…)

Show/Hide Comments in WordPress

Wednesday, June 2nd, 2004

Update by Jennifer: In the interest of making things easier - please also see this post for a version of wp comments you can use. Upload it and be done.)

The cool thing about WordPress being a PHP-based dynamic system is that a certain part of your site, such as the wp-comments.php file, can take on a different form or function depending on its use. Here’s how to make expandable/retractable comments.

This tutorial assumes that you are using Jenn’s Show hide “More” plugin, which places the necessary JavaScript into the head of your WordPress pages. Although it uses that JavaScript, no modification of the plugin is needed, we can handle all of this by modifying our index.php and wp-comments.php files.

It also requires my Get Comments Count plugin.
(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…)

ASP: 404 handling

Wednesday, April 21st, 2004

I recently published a new site (in ASP), and took down the old pages. Some of them had been updated (with new page names) - others were simply going away. Just as I had done with PHP - I wanted a simple 404 page (in ASP) that would email me when people tried to hit the old pages. That way I could decide if I wanted to set up a redirect for some pages - but in all cases, I didn’t want to show a 404 - (I’d just send them back to the home page). I was only interested in htm, htmls, and asp type pages (not interested in missing .ico or gifs)… so here’s the script I came up with for my custom 404 page:

(That long explanation was basically to give you the idea that there’s probably better/other ways to do this - but I had a specific goal in mind. You’re welcome to post alternate, more elaborate methods in the comments. Just thought I’d give you the background first. I should also note that I am NOT anything even CLOSE to an expert on ASP. I only need to use it occasionaly, and I fumble my way through it the best I can - usually, very ungracefully.)
(more…)

User Editable Comments

Thursday, April 15th, 2004

I can’t tell you how many times I’ve left a comment on a blog, and even though I had previewed it, noticed about ten spelling errors just as soon as I pressed the ’submit’ button.

I decided to fix this problem, at least on my blog, by letting users modify their comments for a designated period of time. I decided to share the code in case anyone else wants to implement this. It works directly with the MySQL database, so obviously it requires a MySQL-powered Movable Type blog.
(more…)

Average Posts Per Day

Thursday, April 15th, 2004

I wrote this little snippet of code to display my average posts per day on my journal. Unfortunately that number turned out embarrassingly small (0.383) so I added functionality to display how many days between posts (a post every 2 days looks better). Just uncomment the echo you prefer.

Requires PHP and MySQL.
(more…)

Text Field Auto-Fill

Thursday, April 1st, 2004

A brief break from the eye-bleeding CSS talk. LOL! ;o)

I needed a text field to be prefilled with text. If the user clicked on it, the text would clear out. If they “left” the text field (changed focus) without changing it - I needed it to revert back to the original text. If they DID enter information in it, I needed it to NOT revert back to the original text. (Clear as mud?)

Here’s what I mean:

Here’s how (and why) I did this:
(more…)