php wishlist script/app

Because I can’t help being self-absorbed and excited by all the presents I’m going to get for my birthday, and because I wanted to practice a bit with php, I made a PHP-based wishlist.

Basically it allows your friends to see your wishlist and get dibs on stuff you want so that multiple people don’t end up getting you the same thing.

It’s flat-file based because I don’t fully understand MySQL yet and I like not having to deal with a completely separate entity.

Files involved:

  • wishlist.csv – this contains all the stuff you want, with each line containing “item,description,link” with description and link being optional. Each time a person selects an item they’re going to get you, a wishlist2.csv is created with your original wishlist sans the item he/she picked, and then renamed to wishlist.csv.
  • index.php – reads wishlist.csv and creates a form with a bunch of radio buttons for each item you have
  • wishlist-form.php – processes the item selection and recreates wishlist.csv without the item picked, and shows a thank you message

Example of wishlist.csv:

prodeco bike,+ pegs + rack + fenders? (I LIKE EXPENSIVE THINGS SUE ME),
running pants (small/sz 30),cause winter's too cold for shorts
raspberry drops,yummy yummy raspberry drops
origami,bonus points if made out of cash

Code for reading wishlist.csv and the form which displays it:

What are you going to get for Roman's BIRTHDAY??

Since bet everyone's just dying to get me gifts I made this little webpage to make sure no one gets me the same present. Cause who needs 4 electric bikes, amiright?

Whichever gift you pick will disappear from this page.

<?php $row = 1; if (($handle = fopen("wishlist.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); switch ($num) { case 0: break; case 1: echo " $data[0]
"; break; case 2: echo " $data[0] - $data[1]
"; break; case 3: echo " $data[0] - $data[1]
"; break; } $row++; } fclose($handle); } ?>

Lastly, wishlist-form.php:

$aGift = $_POST['gift'];
	echo "

You didn't select any presents =(.

"; else { $fp = fopen('wishlist2.csv', 'w'); if (($handle = fopen("wishlist.csv", "r")) !== FALSE) { $row = 1; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($row!=$aGift[0]) { fputcsv($fp,$data); $row++; } else { $present = $data[0]; $row++; } } fclose($handle); fclose($fp); unlink('wishlist.csv'); rename('wishlist2.csv','wishlist.csv'); } echo "

Thanks for getting $present for me!

"; } ?>

You are the pride of [subject town].


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s