Index: /trunk/htdocs/index.php
===================================================================
--- /trunk/htdocs/index.php	(revision 257)
+++ /trunk/htdocs/index.php	(revision 258)
@@ -798,5 +798,5 @@
 	unset($_SESSION['cals']); // The result set of cals
 	unset($_SESSION['obs']); // The result set of observations
-	unset($_SESSION['cart_items']); // The result set of observations
+	//unset($_SESSION['cart_items']); // The result set of observations
 }
 
Index: /trunk/htdocs/print_tables.php
===================================================================
--- /trunk/htdocs/print_tables.php	(revision 257)
+++ /trunk/htdocs/print_tables.php	(revision 258)
@@ -91,6 +91,4 @@
   }
 	
-  $index = $startRange;
-	
   // For each row in the observations results, print it out as a table
   // and append any matching cals if requested
@@ -98,59 +96,39 @@
   // obs_cals is a hash table with key = the index of the observation
   // and value = an array of cal results
-  for ($i=$startRange; ($i < count($all_results) && $index <= $endRange); $i++) {
-
-    if(($params[$i]!="data_loc")&&($params[$i]!="file_size_bytes")){
+  for ( $index = $startRange; $index <= $endRange; $index++) {
+
+    if(($params[$index]!="data_loc")&&($params[$index]!="file_size_bytes")){
 
       $row_html_unchecked = "\t<td class=\"result_row\">
-					<input id=\"chkSelect-$i\"  type=\"checkbox\" name=\"selected_obs[]\" value=\"$i\" />
-					<span class=\"glyph\" id=\"spaToggle-$i\"></span>
+					<input id=\"chkSelect-$index\"  type=\"checkbox\" name=\"selected_obs[]\" value=\"$index\" />
+					<span class=\"glyph\" id=\"spaToggle-$index\"></span>
 					</td>\n";
 	
       $row_html_checked = "\t<td class=\"result_row\">
-					<input id=\"chkSelect-$i\" type=\"checkbox\" name=\"selected_obs[]\" value=\"$i\" checked=\"true\" />
-					<span class=\"glyph\" id=\"spaToggle-$i\"></span>
+					<input id=\"chkSelect-$index\" type=\"checkbox\" name=\"selected_obs[]\" value=\"$index\" checked=\"true\" />
+					<span class=\"glyph\" id=\"spaToggle-$index\"></span>
 					</td>\n";
 
-      $line = $all_results[$i];
-		
+      if( $state == 'cart' )
+	$line = $cart_items[$index];
+      else
+        $line = $all_results[$index];
+		
+      if ($index % 2 == 0) {
+	echo "<tbody class=\"even\">\n";
+      }
+      else {
+	echo "<tbody class=\"odd\">\n";
+      }
+      echo "\t<tr>\n";
+      
       // Print the checkbox column
-      if ($state == 'cart') {
-	if (in_array($i, $cart_items)) {
-	  if ($index % 2 == 0) {
-	    echo "<tbody class=\"even\">\n";
-	  }
-	  else {
-	    echo "<tbody class=\"odd\">\n";
-	  }
-	  echo "\t<tr>\n";
+      if ($state == 'cart' && in_array( $line, $cart_items ))
+      {
 	  echo $row_html_checked;
-
-	  // Makes the values of array the keys (index)
-	  // Guaranteed that values are unique so it's OK
-	  //$flipped_cart = array_flip($cart_items);
-				
-	  //$cart_index = $flipped_cart[$i];
-	}
-	else {
-	  continue;
-	}
-      }
-      else {			
-	if ($index % 2 == 0) {
-	  echo "<tbody class=\"even\">\n";
-	}
-	else {
-	  echo "<tbody class=\"odd\">\n";
-	}
-	  	
-	echo "\t<tr>\n";
-
-	if (in_array($i, $cart_items)) {
-	  echo $row_html_checked;
-	}
-			
-	else {
+      }
+      else
+      {	
 	  echo $row_html_unchecked;
-	}
       }
 		
@@ -205,6 +183,4 @@
       //print_r($obs_cals);
       //echo "</pre>\n";
-
-      $index++;
     }
   }
Index: /trunk/htdocs/query.php
===================================================================
--- /trunk/htdocs/query.php	(revision 257)
+++ /trunk/htdocs/query.php	(revision 258)
@@ -245,10 +245,10 @@
       case DOWNLOAD_ALL_OBS_CALS:
 
-	if (isset($cart_items) && isset($all_results)) {
-	  foreach( $cart_items as $index )
+	if (isset($cart_items) ) {
+	  foreach( $cart_items as $next_item )
 	       logger_download( $_SESSION['username'],
-				$all_results[$index]['data_loc'],
-				$all_results[$index]['filename'], 
-				$all_results[$index]['file_size_bytes'] ); 
+				$next_item['data_loc'],
+				$next_item['filename'], 
+				$next_item['file_size_bytes'] ); 
 	  //echo "Download all<br>\n";
 	  $tar_file = tarObsCalFiles($cart_items, $all_results, $cals_within_mins, $db );
@@ -260,10 +260,10 @@
       case DOWNLOAD_ALL_OBS:
 
-	if (isset($cart_items) && isset($all_results)) {
-	  foreach( $cart_items as $index )
+	if (isset($cart_items) ) {
+	  foreach( $cart_items as $next_item )
 	    logger_download( $_SESSION['username'],
-			     $all_results[$index]['data_loc'],
-			     $all_results[$index]['filename'], 
-			     $all_results[$index]['file_size_bytes'] ); 
+			     $next_item['data_loc'],
+			     $next_item['filename'], 
+			     $next_item['file_size_bytes'] ); 
 	  //echo "Download all<br>\n";
 	  $tar_file = tarObsFiles($cart_items, $all_results);
@@ -275,10 +275,10 @@
       case DOWNLOAD_ALL_CALS:
 
-	if (isset($cart_items) && isset($all_results)) {
-	  foreach( $cart_items as $index )
+	if (isset($cart_items) ) {
+	  foreach( $cart_items as $next_item )
 	    logger_download( $_SESSION['username'],
-			     $all_results[$index]['data_loc'],
-			     $all_results[$index]['filename'], 
-			     $all_results[$index]['file_size_bytes'] ); 
+			     $next_item['data_loc'],
+			     $next_item['filename'], 
+			     $next_item['file_size_bytes'] ); 
 	  //echo "Download all<br>\n";
 	  $tar_file = tarCalFiles($cart_items, $all_results, $cals_within_mins, $db );
@@ -322,7 +322,8 @@
       case REMOVE_FROM_CART:
 	if (isset($selected_obs)) {
-	  if ($actions == REMOVE_FROM_CART) {
-	    $cart_items = array_diff($cart_items, $selected_obs);
-	  }
+	    $items_to_remove = Array();
+	    foreach( $selected_obs as $next_index )
+	      $items_to_remove[] = $cart_items[$next_index];
+	    $cart_items = array_diff2($cart_items, $items_to_remove);
 	}
 	break;
@@ -330,5 +331,8 @@
       case ADD_TO_CART:
 	if (isset($selected_obs)) {
-	  foreach ($selected_obs as $ob) {
+	  $items_to_add = Array();
+	  foreach( $selected_obs as $next_index )
+	    $items_to_add[] = $all_results[$next_index];
+	  foreach ($items_to_add as $ob) {
 	    if (!in_array($ob, $cart_items)) {
 	      array_push($cart_items, $ob);
@@ -600,6 +604,6 @@
 $total_cart_size_bytes = 0;
 	
-foreach ($cart_items as $row) {
-  $total_cart_size_bytes += $all_results[$row]['file_size_bytes'];		
+foreach ($cart_items as $item) {
+  $total_cart_size_bytes += $item['file_size_bytes'];		
 }
 	
Index: /trunk/htdocs/utils.php
===================================================================
--- /trunk/htdocs/utils.php	(revision 257)
+++ /trunk/htdocs/utils.php	(revision 258)
@@ -53,8 +53,8 @@
 	
 
-  foreach ($cart_obs as $index) {
-    $file = $all_results[$index]['filename'];
-    $dir = $all_results[$index]['data_loc'];
-    $path = $all_results[$index]['pathname'];
+  foreach ($cart_obs as $item) {
+    $file = $item['filename'];
+    $dir = $item['data_loc'];
+    $path = $item['pathname'];
 
     // Query for all the respective cals
@@ -255,3 +255,25 @@
 }
 
+
+
+/**
+ * array_diff2  Subtract rhs from lhs, both are arrays of arrays. This is used when you have an array of 
+ *              records, each represented by an array, and you have a copy of one of those records that
+ *              you wish to remove.
+ *
+ * @param lhs   The array to subtract from
+ * @param rhs   The array to subtract
+ * @example e   array_diff2( Array( Array( 'help' ), Array('me') ), Array( Array( 'help' ) ) )
+ *              will return Array( Array( 'me' ) );
+ **/
+ 
+function array_diff2( $lhs, $rhs )
+{
+  $result = Array();
+  foreach( $lhs as $next )
+    if( !in_array( $next, $rhs ) )
+      $result[] = $next;
+  return $result;
+}
+
 ?>
