Jump to content

Zip file, shopping cart, downloadable items - need some advice...


Bhaal

Recommended Posts

Hey all,

 

Here's the issue as concisely as I can make it.

 

A band site sells MP3s that are downloaded once notification of payment is received.

 

Buyers can add individual MP3s into a cart and/or add whole Albums (containing multiple MP3s) into a cart.  Tables are setup similar to Categories/Products, except it's Albums/Mp3s  (one table for Albums and another for MP3s; one-to-many: each Album holds multiple MP3s; an MP3 must have an Album record).

 

All MP3s chosen are then zipped into a single archive file for downloading.

 

It was kind of freaking me out that there are two levels of 'product' - since each item added to the cart is a single line item (due to pricing: purchasing an Album is cheaper than purchasing all the MP3s in that album separately).  So, if user adds 2 MP3s and 1 Album, that's 3 line items.

 

Found a script that takes care of the archiving.  It's nice.  Anyway, what I'd done is set a flag in the cart so it knows the difference between MP3s and Albums.  At checkout, I run the following queries to gather the files and zip them:

 

//create zip file and save name of file to orders_info table

$filename = "dwn_" . $order_id . ".zip";
$test = new zip_file("zip_files/$filename");
$test->set_options(array('inmemory' => 0, 'recurse' => 0, 'storepaths' => 0)); //these are params for the archive script

$qnew = "update orders_info set
	dwnfile = '$filename'
where OrderID = '$order_id' ";
mysql_query($qnew) or die(mysql_error());

//get the path and mp3s in shopping cart
//first check the 'mp3' flag

$q4 = "select ItemID from cart where ItemType = 'mp3' and OrderID = '$order_id' ";
$r4 = mysql_query($q4) or die(mysql_error());
while($a4 = mysql_fetch_array($r4))
{
$ItemID[] = $a4[itemID];
}

if(count($ItemID) > '0')
{
	while(list(, $value) = each($ItemID))
	{
		$ItemInfo = explode("|", $value);

		$q5 = "select song_file_name, downloadURL from mp3s where rec_id = '$ItemInfo[0]' ";
		$r5 = mysql_query($q5) or die(mysql_error());
		while($a5 = mysql_fetch_array($r5))
		{
			$song = $a5[song_file_name];
			$path = $a5[downloadURL];

			$mydwn = $path . $song;

			$test->add_files($mydwn);

		}

	}
}

//then check the 'album' flag
$q6 = "select ItemID from cart where ItemType = 'album' and OrderID = '$order_id' ";
$r6 = mysql_query($q6) or die(mysql_error());

while($a6 = mysql_fetch_array($r6))
{
$ItemID[] = $a6[itemID];
}

if(count($ItemID) > '0')
{
while(list(, $value) = each($ItemID))
{
	$ItemInfo = explode("|", $value);

	$q7 = "select song_file_name, downloadURL from mp3s where AlbumID = '$ItemInfo[0]' ";
	$r7 = mysql_query($q7) or die(mysql_error());
	while($a7 = mysql_fetch_array($r7))
	{
		$song = $a7[song_file_name];
		$path = $a7[downloadURL];

		$mydwn = $path . $song;

		$test->add_files($mydwn);
	}
}
}


$test->create_archive(); //create the zip file and save it

 

This works - it actually does gather the MP3 files and zips them - yippee!  However, I'm worried about a couple of things:

1. There's no error checking - not really sure how/what I should do about that

2. It's possible to add multiple instances of the same file to the cart (which is actually the 'add to cart' code, not this 'checkout' code...still, thought I'd mention it to see if anyone has ideas about this).

 

I'm quite confident that there are much better ways of doing this but as a newbie, this is the solution I was able to cobble together. Any advice on how to improve/error check something like this?  (Next step is sending an email with a temporary link to actually download the zip.)

 

Thanks!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.