10 Feb, 2003
Posted by: Jennifer In: Scripts
Promo asked for a script that would display comments on the main page, but in a scrolling box, so that if the comments ran long, they didn't make the page unbareably long (but still all visible on the one page). It was actually incredibly simple to do thanks to CSS. (He also only wanted it to display if there WERE comments to display (so you don't just have this empty box there for no reason…but that's actually a script I've posted here before – so here's the combination of them both)
Read the rest of this entry »
It is usually recommended to do some javascript validation on a form before it's sent. This way, you don't have to waste server resources doing this. (It's still recommended to do some server side validation as well – but hopefully you can cut out some of the mistakes with the client side/javascript validation).
Read the rest of this entry »
(another non-blog post)
Problem: Let's say you're displaying rows of data from your database, and there's just too many rows for one page. You'd like to show X number of rows of data per page, and then have "next"/"previous" buttons to navigate through the list. Let's also say you want to be able to sort all this data by certain fields, and have that "sort" remembered as you go from page to page (clicking the "next" "previous" buttons).
Solution: (If there's another way to do this without the double query – please let me know)…
Read the rest of this entry »
09 Feb, 2003
Posted by: Christine In: MT hacks
From my travels for today:
:: Want to convert from LiveJournal to MT? Meredith has information on how to do it here and is working on a full tutorial.
:: SimpleComments from Kalsey Consulting Group will allow you to display your comments and your trackback pings all together in one spot. The logic being that trackbacks are really just comments left on someone else's site. (found at RevJim)
08 Feb, 2003
Posted by: kristine In: Bookmarks
I've had the opportunity to play with a teeny tiny bit of perl while helping on the Plugin Manager project (which you are welcome to leave some of your thoughts and comments on!), and so since I have a ton of windows open with great info right now, I figured I'd post them here
- PerlFAQ – excellent assortment of questions and answers, in a easy to understand way.
- CPAN — source for perl modules and info on how to use them (your host may have some of this info available on your control panel so you know which modules are installed).
- Online Perl Library — read perl manuals online for free.
- Perl Documentation
Ok… this is my first post here, so I'm going to start out relatively small.
While I was working on the update of my flip gallery script today I needed a way to check a variable for a particular string, and if that string didn't appear I wanted to execute any php code that was embedded in the variable (which was the contents of Movable Type's Extended Entry field). Then I wanted to dump the output into a new variable. The eval() function worked perfectly for this:
ob_start();
eval("?>" . $entry_text . "<?php ");
$result_text = ob_get_contents();
ob_end_clean();
ob_start(), ob_get_contents(), and ob_end_clean() are functions that control output buffering. Basically, ob_start() causes php to begin caching output rather than sending it to the browser, ob_get_contents() returns all the cached results, and ob_end_clean() empties the output buffer and turns output buffering off again.
Feel free to check out the Flip Gallery code if you want to see how this works.
The output buffer catches all output, not only script output:
<? ob_start(); ?>
stuff, more stuff, even more stuff
<?
$normal_text = ob_get_contents();
ob_end_clean();
print "Output Buffer: " . $normal_text;
?>
We've just added a new author here: Adam of gessaman.com. He's got a ton of programming and scripting knowledge – and he basically blew me away with that flip gallery script he did and he's actually just recently updated it. As well as a script he's working on for the photobloggies (one which the original bloggies might want to think about adopting to avoid some of the controversy around them).
(I'll get his name up on the author list side box hopefully by tonight)
Again, another "not so interesting for blog stuff" type post. If you're displaying several rows of data and would like to alternate the background colors of the rows so they're easier to read:
<? for ($i=0; $i<$someNumber; $i++) { ?>
<tr <? if ($i%2) { ?> bgcolor="#CCCCCC" <? } ?>>
<td>data</td>
<td>more data</td>
</tr>
<? } ?>
that "$i%2" basically checks to see if $i is even or not, and if it is, print that "bgcolor" thingy so that it will color the background of that row.
This isn't exactly useful for blog stuff – but if you're using mySQL for a business application, then downloading your data into a CSV file (CSV = Comma Seperated Values) is probably something that might come in handy. The key that I've found is that you can have any kind of characters inside each field, so long as you surround the data with double quotes. Of course, that presents the problem of, if your data uses double quotes, it will kind of mess things up. The solution I came up with was to take the data in the field, and replace the double quotes with two double qoutes (this indicates that it's part of the data and not the end of the field.)
So here's what your code will look like (what I did was store all the data in a string, then write the string to a file – this demonstrates how I create that string):
$query = "SELECT field1, field2, field3 FROM tablename";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);<br>
//write header row
$stringToWrite = "\"field1\"","\"field2\"',"\"field3\""\n";<br>
for ($i=0; $i<$num_results; $i++) {
$row = mysql_fetch_array($result);
$stringToWrite .= "\"".str_replace("\"", "\"\"",$row["field1"])."\",\"".str_replace("\"", "\"\"",$row["field2"])."\",\"".str_replace("\"", "\"\"",$row["field3"])."\"\n";
}
Some notes again (mainly for myself) but maybe it'll help someone.
Problem:
Create a form that (after inserting data into a database) takes a file and uploads it to the server after renaming it with the timestamp, auto_incremented id, and a value from one of the fields from the mysql insert. (heh. Still with me?)
Solution:
First, in your form, to get one of those fields with the "browse" button next to it that prompts you with a "file locating dialog thingy":
<input name="filename" type="file" size="40">
Let's assume you set up your table with one field as an auto increment id table key. After running the insert query, you can do this to get the id of that last insert:
mysql_insert_id();
When we include the value from one of the fields in the form (lets say the field is "username"), we have to remove all the spaces before we upload it. So we do this:
str_replace(' ', ", $HTTP_POST_VARS['username']);
Now lets create the name of the file to be uploaded, which will look like tablekeyid_username_timestamp.ext
$uploaddir = '/path/to/upload/dir/';
$ext = strstr($_FILES['filename']['name'], ".");
// that "['name']" part does not change
// however ['filename'] does
// (filename was the name of the file input field.
// For more information about that $_FILES thing go here
// By doing the above I'm making sure I keep the same
// extension to the filename.
$username = str_replace(' ', ", $HTTP_POST_VARS['username']);
$idnum = mysql_insert_id();
$newfilename = $idnum."_".$username."_".time().$ext;
// time() gets the current time as a unix time stamp.
if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploaddir . $newfilename)) {
echo "File uploaded!";
} else {
echo "File upload failed.";
}
Update: As Kyle reminded me, one of the things that you have to include in the form tag to get a file upload to work is this:
method='post' enctype="multipart/form-data"
So your form tag looks something like:
<form name="sendfile" method="post" action="sendfile.php" onSubmit="return validate();" enctype="multipart/form-data">
Just a side note here, the text in italics above is whatever you set it to. Also, that onSubmit="return validate()" – I'll have to make a post about that at some point – validating your forms, but in short, validate is a function that I defined that goes through the form and checks each field for apporpriate content. If it goes through okay, it simply ends with "return"… if there's something wrong with the form, I throw up an "alert" with an explanation of what's wrong, and then a "return false"… but there will be a more in-depth post about that to come…