Index: /trunk/htdocs/constants.php
===================================================================
--- /trunk/htdocs/constants.php	(revision 195)
+++ /trunk/htdocs/constants.php	(revision 196)
@@ -197,5 +197,5 @@
 define('MAX_DOWNLOAD_MB', 10000); 
 
-define('MAX_RESULTS', 3000);
+define('MAX_RESULTS', 1000);
 
 ?>
Index: /trunk/htdocs/print_tables.php
===================================================================
--- /trunk/htdocs/print_tables.php	(revision 195)
+++ /trunk/htdocs/print_tables.php	(revision 196)
@@ -1,4 +1,3 @@
 <?php
-
   ///////////////////////////////////////////////////////////////////////////////////////////////
   //
@@ -16,10 +15,14 @@
     $rahour, $ramin, $rasec, $decdeg, $decmin, $decsec,
     $radius, $from_day,	$from_month, $from_year,
-    $to_day, $to_month, $to_year, $state, $cals, $cals_within_mins;
+    $to_day, $to_month, $to_year, $cals, $cals_within_mins;
+
+  $bandstr = "";
+  foreach($bandname as $band){
+    $bandstr .= "&bandname[]=$band";
+  }
   		
-  echo "$PHP_SELF?startRange=$startRange&endRange=$endRange&$params_URL&cals=$cals&pulsar_name=$pulsar_name&orderBy=$orderBy&orderDir=$orderDir&cals=$cals&cals_within_mins=$cals_within_mins&data_type=$data_type&bandname=$bandname&rahour=$rahour&ramin=$ramin&rasec=$rasec&decdeg=$decdeg&decmin=$decmin&decsec=$decsec&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&state=$state";
-
-}
-
+  echo "$PHP_SELF?$params_URL&pulsar_name=$pulsar_name&orderBy=$orderBy&orderDir=$orderDir&cals=$cals&cals_within_mins=$cals_within_mins&data_type=$data_type$bandstr&rahour=$rahour&ramin=$ramin&rasec=$rasec&decdeg=$decdeg&decmin=$decmin&decsec=$decsec&radius=$radius&from_day=$from_day&from_month=$from_month&from_year=$from_year&to_day=$to_day&to_month=$to_month&to_year=$to_year&startRange=$startRange&endRange=$endRange&state=$state";
+
+}
 
   // The page navigator
Index: /trunk/htdocs/query.php
===================================================================
--- /trunk/htdocs/query.php	(revision 195)
+++ /trunk/htdocs/query.php	(revision 196)
@@ -1,3 +1,3 @@
-<?php	session_start(); ?>
+<?php session_start(); ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
 <head>
@@ -22,24 +22,18 @@
     <script type="text/javascript" src="scripts/atnf-css-utils.js"></script>
     <script type="text/javascript" src="scripts/atnf-dom-utils.js"></script>
-    <script type="text/javascript" src="scripts/results.js"></script>
-    <script type="text/javascript">
-    function submit_form()
-{
-  document.frmResults.submit();
-}
-</script>
-</head>
-<title>ATNF Pulsar Data Archive Query Results</title>
-<?php include_once("header.html"); ?>
-
-<body class="atoa">
-<!-- Start Content -->
+    <script type="text/javascript" src="scripts/results.js"></script> 
 
 <?php
-  // query.php
-  // 
-  // Performs the user's query on the database
-  // Albert Teoh 15.11.2005
-	
+// echo "<script type=\"text/javascript\">\n";
+// echo "function submit_form(){\n";
+// echo "  document.frmResults.submit();\n";
+// echo "}\n</script>\n";
+echo "</head>\n";
+echo "<title>ATNF Pulsar Data Archive Query Results</title>\n";
+include_once("header.html"); 
+
+echo "<body class=\"atoa\">\n";
+echo "    <!-- Start Content -->\n";
+
 include_once("constants.php");
 include_once("database.php");
@@ -51,6 +45,4 @@
 
 // Processes the form and queries the database on the given parameters
-//echo "username = " . $_SESSION['username'] . " authcode = " . $_SESSION['auth_code'] . " <br>\n";
-	
 	
 if ( !( (isset($_SESSION['username']) && isset($_SESSION['auth_code'])) &&
@@ -68,8 +60,8 @@
 
 // List of request parameters to display
-$params_in           = get_param('params', $HTTP_GET_VARS);	
-	
+$params_in        = get_param('params', $HTTP_GET_VARS);
+
 $params_URL       = array2urlstring("params", $params_in);	
-
+	
 // The current state. E.g cart or results
 // Need to maintain this because other actions could be performed without changing state 
@@ -81,5 +73,5 @@
 // List of checked observations. Note: Not necessarily observations in the cart.
 // Cart observations are stored in $cart_items variable
-$selected_obs              = get_param('selected_obs', $HTTP_GET_VARS);
+$selected_obs     = get_param('selected_obs', $HTTP_GET_VARS);
 	
 // A boolean value to indicate whether if the user wants to view cals as well
@@ -88,5 +80,5 @@
 // an array mapping observation number (corresponding to the index in all_results) 
 // to an array of matching cals
-$obs_cals = array();
+$obs_cals         = array();
 		
 // Display cals within this many minutes of the observation
@@ -95,5 +87,5 @@
 // cart_items is an array of numbers that index to the list of 
 // results in all_results
-$cart_items          = $_SESSION['cart_items'];
+$cart_items       = $_SESSION['cart_items'];
 	
 	
@@ -130,5 +122,5 @@
 $decsec           = trim(get_param('decsec', $HTTP_GET_VARS));
 $radius           = trim(get_param('radius', $HTTP_GET_VARS));
-$bandname         = trim(get_param('bandname', $HTTP_GET_VARS));
+$bandname         = get_param('bandname', $HTTP_GET_VARS);
 $from_day         = get_param('from_day', $HTTP_GET_VARS);
 $from_month       = get_param('from_month', $HTTP_GET_VARS);
@@ -148,8 +140,8 @@
   $raj = ($rahour + $ramin/60. + $rasec/3600.) * 15.;
   $decj = abs($decdeg) + $decmin/60. + $decsec/3600.;
-  if($decdeg[0]=='-'){$decj *= -1.;}
- }
-
-$radius /= 60.; // put into degrees.
+  if($decdeg[0]=='-') {
+    $decj *= -1.;
+  }
+ }
 
 $params = array();
@@ -159,8 +151,10 @@
  }
 
+
 ///////
 // Do some basic error checking: SR>0, |dec|<90, ...
-//echo "state=$state<br>\n";
-//echo "actions=$actions<br>\n";
+// echo "state=$state<br>\n";
+// echo "actions=$actions<br>\n";
+// echo "size of _SESSION['results']= ".count($_SESSION['results'])."<br>\n";
 if(!isset($actions)){
 
@@ -195,102 +189,111 @@
 // has already been determined ($_SESSION['obs_cals']).
 	
-if (isset($actions) && $actions != "") {
-  switch ($actions) {
-
-  case DOWNLOAD_ALL_OBS_CALS:
-
-    if (isset($cart_items) && isset($all_results)) {
-      //echo "Download all<br>\n";
-      $tar_file = tarObsCalFiles($cart_items, $all_results, $cals_within_mins);
-      echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
-    }
-
-    break;
-				
-  case DOWNLOAD_ALL_OBS:
-
-    if (isset($cart_items) && isset($all_results)) {
-      //echo "Download all<br>\n";
-      $tar_file = tarObsFiles($cart_items, $all_results);
-      echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
-    }
-
-    break;
-			
-  case DOWNLOAD_ALL_CALS:
-
-    if (isset($cart_items) && isset($all_results)) {
-      //echo "Download all<br>\n";
-      $tar_file = tarCalFiles($cart_items, $all_results, $cals_within_mins);
-      echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
-    }
-
-    break;
-			
-  case VIEW_PROFILES:
-				
-    if (isset($selected_obs) && isset($all_results)) {
-					
-      // redirect to the view_profile page and pass through the selected_obs and obs_cals arrays
-      $_SESSION['selected_obs'] = $selected_obs;
-      $_SESSION['display_cals'] = false;
-					
-      include_once("view_profiles.php");
-      exit;
-    }
-
-    break;
-			
-  case VIEW_PROFILES_CALS:
-				
-    if (isset($selected_obs) && isset($all_results)) {
-					
-      // redirect to the view_profile page and pass through the selected_obs and obs_cals arrays
-      $_SESSION['selected_obs'] = $selected_obs;
-      $_SESSION['display_cals'] = true;
-					
-      include_once("view_profiles.php");
-      exit;
-    }
-
-    break;
-			
-  case REMOVE_FROM_CART:
-    if (isset($selected_obs)) {
-      if ($actions == REMOVE_FROM_CART) {
-	$cart_items = array_diff($cart_items, $selected_obs);
-      }
-    }
-    break;
-
-  case ADD_TO_CART:
-    if (isset($selected_obs)) {
-      foreach ($selected_obs as $ob) {
-	if (!in_array($ob, $cart_items)) {
-	  array_push($cart_items, $ob);
-	}
-      }
-    }
-    break;
-  }
- }
-	
-if (!(isset($startRange) && isset($endRange))) {
-		
-  if ($HTTP_GET_VARS['submit_button'] == 'submit' || !(isset($_SESSION['start_range']) || isset($_SESSION['end_range']))) {
-    $startRange = 1;
-    $endRange = MAX_DISPLAY_ROWS;
-  }
-	
-  else {
-    $startRange = $_SESSION['start_range'];
-    $endRange = $_SESSION['end_range'];
-  }
-		
- }
-	
 if (!isset($cart_items)) {
   $cart_items = array();
  }
+	
+if (isset($actions) && $actions != "") 
+  {
+  
+    switch ($actions) 
+      {
+
+      case DOWNLOAD_ALL_OBS_CALS:
+
+	if (isset($cart_items) && isset($all_results)) {
+	  //echo "Download all<br>\n";
+	  $tar_file = tarObsCalFiles($cart_items, $all_results, $cals_within_mins);
+	  echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
+	}
+
+	break;
+				
+      case DOWNLOAD_ALL_OBS:
+
+	if (isset($cart_items) && isset($all_results)) {
+	  //echo "Download all<br>\n";
+	  $tar_file = tarObsFiles($cart_items, $all_results);
+	  echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
+	}
+
+	break;
+			
+      case DOWNLOAD_ALL_CALS:
+
+	if (isset($cart_items) && isset($all_results)) {
+	  //echo "Download all<br>\n";
+	  $tar_file = tarCalFiles($cart_items, $all_results, $cals_within_mins);
+	  echo " <meta http-equiv=\"REFRESH\" content=\"0; URL=".WEB_DOWNLOADS_DIR."/$tar_file\">\n";
+	}
+
+	break;
+			
+      case VIEW_PROFILES:
+	
+// 	echo "<center><b>HELLO!</b></center><br>\n";
+// 	echo "sel-obs: ".isset($selected_obs)."<br>\n";
+// 	echo "all-res: ".isset($all_results)."<br>\n";
+// 	echo "ses-res: ".isset($_SESSION['results'])."<br>\n";
+				
+	if (isset($selected_obs) && isset($all_results)) {
+					
+	  // redirect to the view_profile page and pass through the selected_obs and obs_cals arrays
+	  $_SESSION['selected_obs'] = $selected_obs;
+	  $_SESSION['display_cals'] = false;
+					
+	  include_once("view_profiles.php");
+
+	  exit;
+	}
+
+	break;
+			
+      case VIEW_PROFILES_CALS:
+				
+	if (isset($selected_obs) && isset($all_results)) {
+					
+	  // redirect to the view_profile page and pass through the selected_obs and obs_cals arrays
+	  $_SESSION['selected_obs'] = $selected_obs;
+	  $_SESSION['display_cals'] = true;
+					
+	  include_once("view_profiles.php");
+	  exit;
+	}
+
+	break;
+			
+      case REMOVE_FROM_CART:
+	if (isset($selected_obs)) {
+	  if ($actions == REMOVE_FROM_CART) {
+	    $cart_items = array_diff($cart_items, $selected_obs);
+	  }
+	}
+	break;
+
+      case ADD_TO_CART:
+	if (isset($selected_obs)) {
+	  foreach ($selected_obs as $ob) {
+	    if (!in_array($ob, $cart_items)) {
+	      array_push($cart_items, $ob);
+	    }
+	  }
+	}
+	break;
+      }
+
+  }
+	
+if (!(isset($startRange) && isset($endRange))) 
+  {		
+    if ($HTTP_GET_VARS['submit_button'] == 'submit' || !(isset($_SESSION['start_range']) || isset($_SESSION['end_range']))) {
+      $startRange = 1;
+      $endRange = MAX_DISPLAY_ROWS;
+    }
+    else {
+      $startRange = $_SESSION['start_range'];
+      $endRange = $_SESSION['end_range'];
+    }
+    
+  }
 	
 $_SESSION['cart_items'] = $cart_items;
@@ -299,8 +302,11 @@
 	
 
-if (!isset($params) && !isset($_SESSION['results'])) {
-  echo "No parameters were selected. Please select at least one.<br>\n";
-  exit;
- }
+if (!isset($params) && !isset($_SESSION['results'])) 
+  {
+    echo "No parameters were selected. Please select at least one.<br>\n";
+    include("footer.html");
+    echo "</body>\n</html>\n";
+    exit();
+  }
 	
 if (!isset($_SESSION['results']) || $HTTP_GET_VARS['submit_button'] == 'submit') {
@@ -349,161 +355,172 @@
   }
 		
-
-  ///////////////////////////////
-  // Set up the query string
-  //
-
-  $whereString = "";
-  $selectString = "SELECT ";
-
-  ///////////////////////////
-  // Constraint on pulsar name
-  //
-  //  In this case, query on pulsar name and on pulsar position 
-  //       -- obtain the latter from PSRCAT
-  //  if ($pulsar_name) {
-  if ($psrNameOK) {
-
-    // Check to see if the pulsar given is in the catalogue
-    $command = PSRCAT." -nohead -nonumber -o short -c \"\" $pulsar_name";
-    exec($command,$output);
-
-    if($output[0]=="") { // Pulsar is present
-
-      $commandPSR = PSRCAT." -nohead -nonumber -o short -c \"RAJD DECJD\" $pulsar_name";
-      exec($commandPSR, $outputPSR);
-      list($psr_rajd, $psr_decjd) = split("[ ]+", trim($outputPSR[0]));
+  if(!$isError && !isset($actions))  {
+
+    ///////////////////////////////
+    // Set up the query string
+    //
+
+    $whereString = "";
+    $selectString = "SELECT ";
+
+    ///////////////////////////
+    // Constraint on pulsar name
+    //
+    //  In this case, query on pulsar name and on pulsar position 
+    //       -- obtain the latter from PSRCAT
+    //  if ($pulsar_name) {
+    if ($psrNameOK) {
+
+      // Check to see if the pulsar given is in the catalogue
+      $command = PSRCAT." -nohead -nonumber -o short -c \"\" $pulsar_name";
+      exec($command,$output);
+
+      if($output[0]=="") { // Pulsar is present
+
+	$commandPSR = PSRCAT." -nohead -nonumber -o short -c \"RAJD DECJD\" $pulsar_name";
+	exec($commandPSR, $outputPSR);
+	list($psr_rajd, $psr_decjd) = split("[ ]+", trim($outputPSR[0]));
             
-      // Remove the leading J and B
-      $output_psrname = ltrim($pulsar_name, "JBbj");
-
-      $angsepStringPSR = "degrees( acos( cos(radians(rajd-$psr_rajd))*cos(radians(decjd))*cos(radians($psr_decjd))+sin(radians(decjd))*sin(radians($psr_decjd))) )";
+	// Remove the leading J and B
+	$output_psrname = ltrim($pulsar_name, "JBbj");
+
+	$angsepStringPSR = "degrees( acos( cos(radians(rajd-$psr_rajd))*cos(radians(decjd))*cos(radians($psr_decjd))+sin(radians(decjd))*sin(radians($psr_decjd))) )";
       
-      //      $whereString .= " AND ( (($angsepStringPSR - 0.25*(bmaj+bmin)) < ".PSR_SEARCH_RADIUS.") OR src_name LIKE '%$output_psrname' )";
-      $whereString .= " AND ( (($angsepStringPSR - 0.25*(bmaj+bmin)) < $radius) OR src_name LIKE '%$output_psrname' )";
+	$whereString .= " AND ( (($angsepStringPSR - 0.25*(bmaj+bmin)) < ($radius/60.)) OR src_name LIKE '%$output_psrname' )";
       
-      $selectString .= "$angsepStringPSR as distance, ";
+	$selectString .= "$angsepStringPSR as distance, ";
       
-    }
-    else{
-      $whereString .= "AND (1 = 0)"; // return nothing;
-      $selectString .= "0 as distance, ";
-      $psrNameOK = False;
-
-      $isError = True;
-      $errorMsg .= "<center><b>Error:</b> The pulsar name given, <b>$pulsar_name</b>, is not in the catalogue</center><br>\n";
-    }
-
-
-  } 
-  else{// if ($radius) {
-  ////////////////////////////
-  // Constraint on boundary
-  //  The pulsar name search takes precedence if both fields are filled in.
-
-    $angsepString = "(degrees( acos( cos(radians(rajd-$raj))*cos(radians(decjd))*cos(radians($decj))+sin(radians(decjd))*sin(radians($decj))) ) )";
+      }
+      else{
+	$whereString .= "AND (1 = 0)"; // return nothing;
+	$selectString .= "0 as distance, ";
+	$psrNameOK = False;
+
+	$isError = True;
+	$errorMsg .= "<center><b>Error:</b> The pulsar name given, <b>$pulsar_name</b>, is not in the catalogue</center><br>\n";
+      }
+
+
+    } 
+    else{// if ($radius) {
+      ////////////////////////////
+      // Constraint on boundary
+      //  The pulsar name search takes precedence if both fields are filled in.
+
+      $angsepString = "(degrees( acos( cos(radians(rajd-$raj))*cos(radians(decjd))*cos(radians($decj))+sin(radians(decjd))*sin(radians($decj))) ) )";
 		  
-    $whereString .= " AND (($angsepString - 0.25*(bmaj+bmin)) < $radius) ";
+      $whereString .= " AND (($angsepString - 0.25*(bmaj+bmin)) < ($radius/60.)) ";
 		  
-    $selectString .= "$angsepString as distance, ";
-
-  }
-
-  $selectString .= $parameters;
-
-  ////////////////////////////
-  // Constraint on obs mode
-
-  if ($obs_mode == "search") {
-    $whereString .= " AND filename like '%.sf'";
-  }
-  else if($obs_mode == "timing"){
-    $whereString .= " AND filename not like '%.sf'";
-  }
-
-  ////////////////////////////
-  // Constraint on data type
-
-  if ($data_type) {
-    $whereString .= " AND data_type = '$data_type'";
-  }
-
-  ////////////////////////////
-  // Constraint on survey
-
-  if ($survey) {
-    $whereString .= " AND survey = '$survey'";
-  }
-
-  //////////////////////
-  // Constraint on MJD
-
-  $whereString .= " AND (MJD < $to_mjd AND MJD > $from_mjd)";
-
-  //////////////////////
-  // Constraint on freq
-
-  $bandString = "";
-  for ($i=0; $i < count($bandname); $i++) {
-    if($bandname[$i] = "70cm") $bandString .=" and (obsfreq<500.)";
-    if($bandname[$i] = "50cm") $bandString .=" and (obsfreq>600. and obsfreq<700.)";
-    if($bandname[$i] = "20cm") $bandString .=" and (obsfreq>1199.99 and obsfreq<1800.01)";
-    if($bandname[$i] = "13cm") $bandString .=" and (obsfreq>1999.99 and obsfreq<2500.01)";
-    if($bandname[$i] = "10cm") $bandString .=" and (obsfreq>2599.99 and obsfreq<3600.01)";
-    if($bandname[$i] = "6cm")  $bandString .=" and (obsfreq>4499.99 and obsfreq<5100.01)";
-    if($bandname[$i] = "5cm")  $bandString .=" and (obsfreq>5899.99 and obsfreq<6800.01)";
-    if($bandname[$i] = "3cm")  $bandString .=" and (obsfreq>8099.99 and obsfreq<8600.01)";
-    if($bandname[$i] = "1.3cm") $bandString .=" and (obsfreq>20999.99 and obsfreq<24000.01)";
-  }
-  for ($i=0; $i < count($bandname); $i++) {
-    if($bandname[$i] = "anyBand") $bandString = "";
-  }
-  $whereString .= $bandString;
-  ////////////////////////
-  //  Ordering the results
+      $selectString .= "$angsepString as distance, ";
+
+    }
+
+    $selectString .= $parameters;
+
+    ////////////////////////////
+    // Constraint on obs mode
+
+    if ($obs_mode == "search") {
+      $whereString .= " AND filename like '%.sf'";
+    }
+    else if($obs_mode == "timing"){
+      $whereString .= " AND filename not like '%.sf'";
+    }
+
+    ////////////////////////////
+    // Constraint on data type
+
+    if ($data_type) {
+      $whereString .= " AND data_type = '$data_type'";
+    }
+
+    ////////////////////////////
+    // Constraint on survey
+
+    if ($survey) {
+      $whereString .= " AND survey = '$survey'";
+    }
+
+    //////////////////////
+    // Constraint on MJD
+
+    $whereString .= " AND (MJD < $to_mjd AND MJD > $from_mjd)";
+
+    //////////////////////
+    // Constraint on freq
+
+    $bandString = "";
+    for ($i=0; $i < count($bandname); $i++) {
+      if($bandname[$i] = "70cm") $bandString .=" and (obsfreq<500.)";
+      if($bandname[$i] = "50cm") $bandString .=" and (obsfreq>600. and obsfreq<700.)";
+      if($bandname[$i] = "20cm") $bandString .=" and (obsfreq>1199.99 and obsfreq<1800.01)";
+      if($bandname[$i] = "13cm") $bandString .=" and (obsfreq>1999.99 and obsfreq<2500.01)";
+      if($bandname[$i] = "10cm") $bandString .=" and (obsfreq>2599.99 and obsfreq<3600.01)";
+      if($bandname[$i] = "6cm")  $bandString .=" and (obsfreq>4499.99 and obsfreq<5100.01)";
+      if($bandname[$i] = "5cm")  $bandString .=" and (obsfreq>5899.99 and obsfreq<6800.01)";
+      if($bandname[$i] = "3cm")  $bandString .=" and (obsfreq>8099.99 and obsfreq<8600.01)";
+      if($bandname[$i] = "1.3cm") $bandString .=" and (obsfreq>20999.99 and obsfreq<24000.01)";
+    }
+    for ($i=0; $i < count($bandname); $i++) {
+      if($bandname[$i] = "anyBand") $bandString = "";
+    }
+    $whereString .= $bandString;
+    ////////////////////////
+    //  Ordering the results
   
-  $orderString = " ORDER BY ";
-  $orderString .= $orderBy . " " . $orderDir;
-
-  // add other alternatives, in order of preference, if they haven't been selected first.
-  if($orderBy!="distance") $orderString .= ",distance asc";
-  if($orderBy!="filename") $orderString .= ",filename desc";
-  if($orderBy!="date") $orderString .= ",date asc";
-  if($orderBy!="obsfreq") $orderString .= ",obsfreq asc";
-
-
-  ///////////////////////////////
-  // Performing SQL query
-  //
-
-  $link = db_connect();
-
-  // Remove any leading or trailing AND and OR
-  $whereString = trim($whereString, " AND OR");
-
-  $query = "( $selectString FROM s70 WHERE $whereString ) UNION ( $selectString FROM obs WHERE $whereString )";
-
-  $query .= $orderString;
-
-//    echo "$query<br>";
-
-  $result = mysql_query($query) or die('Main Query failed: ' . mysql_error() . "\n". $query);
-
-  $all_results = array();
-
-//   $temp_num_results = mysql_num_rows($result);
-//   echo "<br><br><br>$temp_num_results<br>\n";
-//   return 0;
-
-  while ($line = mysql_fetch_assoc($result)) {
-    array_push($all_results, $line );
-  }
-		
-  $_SESSION['results'] = $all_results;
-	
-  mysql_free_result($result);
-
-  mysql_close($link);
-	
+    $orderString = " ORDER BY ";
+    $orderString .= $orderBy . " " . $orderDir;
+
+    // add other alternatives, in order of preference, if they haven't been selected first.
+    if($orderBy!="distance") $orderString .= ",distance asc";
+    if($orderBy!="filename") $orderString .= ",filename desc";
+    if($orderBy!="date")     $orderString .= ",date asc";
+    if($orderBy!="obsfreq")  $orderString .= ",obsfreq asc";
+
+
+    ///////////////////////////////
+    // Performing SQL query
+    //
+
+    $link = db_connect();
+
+    // Remove any leading or trailing AND and OR
+    $whereString = trim($whereString, " AND OR");
+
+    $query = "( $selectString FROM s70 WHERE $whereString ) UNION ( $selectString FROM obs WHERE $whereString )";
+
+    $query .= $orderString;
+
+    $query .= " limit ".MAX_RESULTS; // limit the maximum number of results to be returned.
+
+    $numquery = "select a.num+b.num as num from (select count(*) as num from s70 where $whereString) as a, (select count(*) as num from obs where $whereString) as b";
+    $numresult = mysql_query($numquery);
+    $numrows = mysql_fetch_assoc($numresult);
+    if( ($numrows['num'] > MAX_RESULTS) && ($state=="results") ) {
+      echo "<center>\n<b>Note:</b> Your query returns more results (".$numrows['num'].") than the internal limit.<br>\n";
+      echo "Only showing the first ".MAX_RESULTS." rows.\n</center><br>\n";
+    }
+    mysql_free_result($numresult);
+    
+    //    echo "$query<br>";
+
+    $result = mysql_query($query) or die('Main Query failed: ' . mysql_error() . "\n". $query);
+  
+    $all_results = array();
+
+    while ($line = mysql_fetch_assoc($result)) {
+      array_push($all_results, $line );
+    }
+
+//     echo "size of all results = " . count($all_results) . "<br>\n";
+	       
+    $_SESSION['results'] = $all_results;
+//     echo "size of session results = " . count($_SESSION['results']) . "<br>\n";
+	       
+    mysql_free_result($result);
+
+    mysql_close($link);
+
+  }
  }
 	
@@ -521,123 +538,156 @@
 }
 	
-if ($state == 'cart') {
-  $num_display_rows = count($cart_items);
- }
+if ($state == 'cart') 
+  {
+    $num_display_rows = count($cart_items);
+  }
  else {
    $num_display_rows = $num_rows;
  }
 	
-if (!(isset($startRange) && isset($endRange))) {
-  $startRange = 1;
-		
-		
-  if ($endRange < MAX_DISPLAY_ROWS) {
-    $endRange = $num_display_rows;
-  }
-  else {
-    $endRange = MAX_DISPLAY_ROWS;
-  }
- }
-?>
-<form action="<?php echo $PHP_SELF; ?>" method="GET" id="frmResults">
-  <input type="hidden" name="startRange" value="<?php echo $startRange;?>">
-  <input type="hidden" name="endRange" value="<?php echo $endRange;?>">
-
-
-  <table class="top_menu" border="0" id="tabQueryResults">
-  <?php 
-  if ($state == 'cart') { 
-    ?>
-    <tr class="top_menu"><td class="top_menu_heading" colspan="4"><h2>Observations Cart</h2></td></tr>
-    <?php 
+if (!(isset($startRange) && isset($endRange))) 
+  {
+    $startRange = 1;
+
+    if ($endRange < MAX_DISPLAY_ROWS) {
+      $endRange = $num_display_rows;
+    }
+    else {
+      $endRange = MAX_DISPLAY_ROWS;
+    }
+
+  }
+
+
+echo "<form action=\"" . $PHP_SELF . "\" method=\"GET\" id=\"frmResults\">\n";
+
+foreach($params_in as $par){
+  echo "<input type=\"hidden\" name=\"params[]\" value=\"".$par."\">\n";
+}
+echo "<input type=\"hidden\" name=\"startRange\" value=\"".$startRange."\">\n";
+echo "<input type=\"hidden\" name=\"endRange\" value=\"".$endRange."\">\n";
+echo "<input type=\"hidden\" name=\"pulsar_name\" value=\"".$pulsar_name."\">\n";
+echo "<input type=\"hidden\" name=\"obs_mode\" value=\"".$obs_mode."\">\n";
+echo "<input type=\"hidden\" name=\"data_type\" value=\"".$data_type."\">\n";
+echo "<input type=\"hidden\" name=\"orderBy\" value=\"".$orderBy."\">\n";
+echo "<input type=\"hidden\" name=\"orderDir\" value=\"".$orderDir."\">\n";
+echo "<input type=\"hidden\" name=\"survey\" value=\"".$survey."\">\n";
+echo "<input type=\"hidden\" name=\"rahour\" value=\"".$rahour."\">\n";
+echo "<input type=\"hidden\" name=\"ramin\" value=\"".$ramin."\">\n";
+echo "<input type=\"hidden\" name=\"rasec\" value=\"".$rasec."\">\n";
+echo "<input type=\"hidden\" name=\"decdeg\" value=\"".$decdeg."\">\n";
+echo "<input type=\"hidden\" name=\"decmin\" value=\"".$decmin."\">\n";
+echo "<input type=\"hidden\" name=\"decsec\" value=\"".$decsec."\">\n";
+echo "<input type=\"hidden\" name=\"radius\" value=\"".$radius."\">\n";
+foreach($bandname as $band){
+  echo "<input type=\"hidden\" name=\"bandname[]\" value=\"".$band."\">\n";
+}
+echo "<input type=\"hidden\" name=\"from_day\" value=\"".$from_day."\">\n";
+echo "<input type=\"hidden\" name=\"from_month\" value=\"".$from_month."\">\n";
+echo "<input type=\"hidden\" name=\"from_year\" value=\"".$from_year."\">\n";
+echo "<input type=\"hidden\" name=\"to_day\" value=\"".$to_day."\">\n";
+echo "<input type=\"hidden\" name=\"to_month\" value=\"".$to_month."\">\n";
+echo "<input type=\"hidden\" name=\"to_year\" value=\"".$to_year."\">\n";
+echo "<input type=\"hidden\" name=\"cals\" value=\"".$cals."\">\n";
+echo "<input type=\"hidden\" name=\"cals_within_mins\" value=\"".$cals_within_mins."\">\n\n";
+
+echo "<table class=\"top_menu\" border=\"0\" id=\"tabQueryResults\">\n";
+if ($state == 'cart') 
+  { 
+    echo " <tr class=\"top_menu\"><td class=\"top_menu_heading\" colspan=\"4\"><h2>Observations Cart</h2></td></tr>\n";
   } 
-  else if ($state == 'results') { 
-    ?>
-    <tr class="top_menu"><td class="top_menu_heading" colspan="4"><h2>Query Results</h2></td></tr>
-      <?php 
+ else if ($state == 'results') 
+   { 
+     echo " <tr class=\"top_menu\"><td class=\"top_menu_heading\" colspan=\"4\"><h2>Query Results</h2></td></tr>\n";
    } 
-?>
-
-<tr class="top_menu"><td class="top_menu"  align="left" width="30%"><a class="top_menu" href="index.php?command=logout">Logout</a> | 
-  <a class="top_menu" href="change_password.html?prev_page=<?php echo $PHP_SELF;?>">Change Password</a> |
-  <a class="top_menu" href="index.php?actions=new_query">New Query</a>
-  </td>
-  <td class="top_menu" width="50%" align="center">
-  Actions: <select name="actions">
-  <?php 
-  if ($state == 'cart') 
-    { 
-      if ($total_cart_size_bytes/1000000 < MAX_DOWNLOAD_MB) {
-	echo "<option>".DOWNLOAD_ALL_OBS_CALS."</option>\n";
-	echo "<option>".DOWNLOAD_ALL_OBS."</option>\n";
-	echo "<option>".DOWNLOAD_ALL_CALS."</option>\n";
+
+echo " <tr class=\"top_menu\">\n";
+echo "  <td class=\"top_menu\"  align=\"left\" width=\"30%\">\n";
+echo "    <a class=\"top_menu\" href=\"index.php?command=logout\">Logout</a> | \n";
+echo "    <a class=\"top_menu\" href=\"change_password.html?prev_page=".$PHP_SELF."\">Change Password</a> |\n";
+echo "    <a class=\"top_menu\" href=\"index.php?actions=new_query\">New Query</a>\n";
+echo "  </td>\n";
+
+echo "  <td class=\"top_menu\" width=\"50%\" align=\"center\">\n";
+echo "    Actions: <select name=\"actions\">\n";
+
+if ($state == "cart") 
+  { 
+    if ( ($total_cart_size_bytes/1000000) < MAX_DOWNLOAD_MB ) {
+      echo "      <option>".DOWNLOAD_ALL_OBS_CALS."</option>\n";
+      echo "      <option>".DOWNLOAD_ALL_OBS."</option>\n";
+      echo "      <option>".DOWNLOAD_ALL_CALS."</option>\n";
+    }
+    else {
+      echo "      <option disabled>Download limit ".MAX_DOWNLOAD_MB."MB exceeded</option>\n";
+    }
+    echo "      <option>".REMOVE_FROM_CART."</option>\n";
+    echo "    </select>\n";
+    echo "    <input type=\"submit\" value=\"Perform action\">\n";
+    echo "  </td>\n  <td class=\"top_menu\" align=\"right\">\n";
+  
+    echo "    Cart size: <b><a href=\"";
+    printURL("cart",1,count($cart_items));
+    echo "\">".count($cart_items);
+    $sizeMB = sprintf("%.1f MB", $total_cart_size_bytes/1000000);
+    echo " Item/s</a> (".$sizeMB.")\n  </td>\n";
+  
+    echo "  <td class=\"top_menu\" align=\"right\" width=\"1%\">\n";
+    echo "    <a href=\"";
+    printURL("results",1,MAX_DISPLAY_ROWS);
+    echo "\">";
+    echo "<img alt=\"Revert to query results\" border=\"0\" title=\"Revert to query results\" src=\"/images/table_30px_white.png\"></img>\n";
+    echo "    </a>\n  </td>\n";
+  }
+ else 
+   { 
+     echo "      <option>".ADD_TO_CART."</option>\n";
+     echo "      <option>----- Profile Viewing Options -----</option>\n";
+     echo "      <option>".VIEW_PROFILES."</option>\n";
+     if (isset($cals) && ($cals != "")) {
+       echo "      <option>".VIEW_PROFILES_CALS."</option>\n";
+     }
+     echo "    </select>\n";
+     echo "    <input type=\"submit\" value=\"Perform action\">\n";
+     echo "  </td>\n";
+   
+     echo "  <td class=\"top_menu\" align=\"right\" width=\"20%\">\n";
+     $sizeMB = sprintf("%.1f MB", $total_cart_size_bytes/1000000);
+     echo "    Cart size: <b><a href=\"";
+     printURL("cart",1,count($cart_items));
+     echo "\">".count($cart_items);
+     echo " Item/s</a> (".$sizeMB.")\n";
+     echo "  </td>\n";
+     echo "  <td class=\"top_menu\" align=\"right\" width=\"1%\">\n";
+     echo "    <a href=\"";
+     printURL("cart",1,count($cart_items));
+     echo "\">";
+     echo "<img alt=\"View observation cart\" border=\"0\" title=\"View observation cart\" src=\"/images/cart_30px_white.png\"></img>\n";
+     echo "    </a>\n  </td>\n";
+ }
+echo " </tr>\n</table>\n";
+
+if ($num_display_rows == 0) 
+  {
+    if($state == "cart"){
+      echo "<center>Your cart is empty.</center><br>\n";
+      echo "<center><a href=\"";
+      printURL("results",1,MAX_DISPLAY_ROWS);
+      echo "\">Go back to search results</a></center><br>\n";
+    }
+    else if(!isset($actions)) {
+      if($isError){
+	echo $errorMsg;
       }
-      else {
-	echo "<option disabled>Download limit ".MAX_DOWNLOAD_MB."MB exceeded</option>\n";
+      else{
+	echo "<center>Sorry, no results matched your search</center><br>\n";
       }
-      ?>
-      </option>
-      <option><?php echo REMOVE_FROM_CART; ?></option>
-      </select>
-      <input type="submit" value="Perform action">
-      </td>
-      <td class="top_menu" align="right"><b><?php echo count($cart_items); ?></b>
-      Item/s (<?php printf("%.1f MB", $total_cart_size_bytes/1000000);?>)</td>
-      <td class="top_menu" align="right" width="1%"><a href="<?php echo $PHP_SELF;?>?state=results&startRange=1&endRange=<?php echo MAX_DISPLAY_ROWS; ?>">
-      <img alt="Revert to query results" border="0" title="Revert to query results" src="/images/table_30px_white.png"></img></a>
-      </td>
-      <?php 
-    } 
-  else 
-    { ?>
-      <option>
-	<?php 
-	echo ADD_TO_CART; 
-      ?>
-	</option>
-	    <option>----- Profile Viewing Options -----</option>
-	    <option><?php echo VIEW_PROFILES; ?></option>
-						    <?php 
-										  echo "cals = $cals<br>\n";
-    if (isset($cals) && $cals != "") {
-      echo "<option>".VIEW_PROFILES_CALS."</option>\n";
-    }
-    ?>
-      
-    </select>
-    <input type="submit" value="Perform action">
-	</td>
-	<td class="top_menu" align="right" width="20%"><b><?php echo count($cart_items); ?></b>
-	<a href="<?php echo $PHP_SELF;?>?state=cart&startRange=1&endRange=<?php echo count($cart_items);?>">Item/s</a> (<?php printf("%.1f MB", $total_cart_size_bytes/1000000);?>)</td>
-	<td class=\"top_menu\" align=\"right\" width=\"1%\"><a href="<?php echo $PHP_SELF;?>?state=cart&startRange=1&endRange=<?php echo count($cart_items);?>">
-    <img alt="View observation cart" border="0" title="View observation cart" src="/images/cart_30px_white.png"></img></a>
-    </td>
-<?php
-  }
-echo "</tr></table>\n";
-
-if ($num_display_rows == 0) {
-  if($state == "cart"){
-    echo "<center>Your cart is empty.</center><br>\n";
-    echo "<center><a href=\"";
-    echo $PHP_SELF;
-    echo "?state=results&startRange=1&endRange=";
-    echo MAX_DISPLAY_ROWS;
-    echo "\">Go back to search results</a></center><br>\n";
-  }
-  else {
-    if($isError){
-      echo $errorMsg;
-    }
-    else{
-      echo "<center>Sorry, no results matched your search</center><br>\n";
-    }
-    //    if(!$psrNameOK && $pulsar_name!="") echo "<center>The pulsar name given, <b>$pulsar_name</b>, is not in the catalogue</center><br>\n";
-    echo "<center><a href=\"index.php\">Search again</a></center><br>\n";
-  }
-  include "footer.html";
-  echo "</body>\n</html>\n";
-  exit();
- }
+      echo "<center><a href=\"index.php\">Search again</a></center><br>\n";
+    }
+    echo "</form>\n";
+    include("footer.html");
+    echo "</body>\n</html>\n";
+    exit();
+  }
 
 echo "<center>\n";
@@ -645,33 +695,24 @@
 echo "</center>\n";				
 	
-	
-if ($num_display_rows < $endRange) {
-  echo "Showing <b>$startRange - $num_display_rows</b> of <b>$num_display_rows</b> rows\n";
- }
- else {
-   echo "Showing <b>$startRange - $endRange</b> of <b>$num_display_rows</b> rows\n";
- }
-	
-	
-// TODO: Is this a better way to do it?
-//if (mysql_data_seek($result, $startRange);
-
-// Show first few
-//print_r($all_results);
+if ($num_display_rows < $endRange) 
+  {
+    echo "Showing <b>$startRange - $num_display_rows</b> of <b>$num_display_rows</b> rows\n";
+  }
+ else 
+   {
+     echo "Showing <b>$startRange - $endRange</b> of <b>$num_display_rows</b> rows\n";
+   }
+	
 showTable($startRange-1, $endRange-1, $heading, $all_results, $params_cpy, $cals, $cals_within_mins);
 	
-
 echo "<center>\n";
 printNavigate($startRange, $num_display_rows);
 echo "</center>\n";
 	
-//echo "count all results = " . count($all_results);
-	
+echo "</form>\n";
+include("footer.html");
+echo "</body>\n";
+echo "</html>\n";
 
 
 ?>
-</form>
-<?php include "footer.html"; ?>
-</body>
-</html>
-
Index: /trunk/htdocs/utils.php
===================================================================
--- /trunk/htdocs/utils.php	(revision 195)
+++ /trunk/htdocs/utils.php	(revision 196)
@@ -1,3 +1,3 @@
-<?php	session_start(); 
+<?php session_start(); 
 
 // A set of commonly used functions
@@ -251,6 +251,8 @@
   $brackets = urlencode("[]");
 	
-  foreach ($array as $item) {
-    $str .= "$arrayName$brackets=$item&";
+  if(is_array($array)){
+    foreach ($array as $item) {
+      $str .= "$arrayName$brackets=$item&";
+    }
   }
   $str = trim($str, "&");
