Jump to content


Photo

My search function returns the same results for muliple records.


  • Please log in to reply
No replies to this topic

#1 Cep

Cep
  • Members
  • PipPipPip
  • Advanced Member
  • 539 posts
  • LocationOn a boat in the middle of nowhere

Posted 17 August 2006 - 03:32 PM

Hi,

I am working on a script that will search a database based on the results of either one of three fields.

The results will then be outputted to an array for use later.

If no results are found a blank array is sent, if 1 record is found the record row is placed in an array and sent out. If multiple records are found an array is created for each record, converted to a string and then inserted into another array for later use.

One of the problems I have is that if there are mulitple records found for one of the search criteria, instead of getting each record row into a new array, I get the first and same one. I do not understand why?

function search_client($clientid, $clientno, $clientname) {

  if ($clientid!="") {
     $sql = "SELECT * FROM `DEClient` WHERE `ID` = $clientid";
  } elseif ($clientno!="") {
     $sql = "SELECT * FROM `DEClient` WHERE `Client No` = '$clientno'";
  } elseif ($clientname!="") {
     $sql = "SELECT * FROM `DEClient` WHERE `Client Name` LIKE '$clientname'";
  }

  $result = odbc_exec(db_src(),$sql) or die("SQL Error: Function search_client - unable to select");

  $num_rows = odbc_record_count($result, db_src(), $sql);

  //array(rows,id,no,name,addr,postcode,tel,fax);

  switch ($num_rows) {
    case 0:
         // No Records found return a blank result
         $search = array($num_rows,"","","","","");
         break;
    case 1:
         // Single Record found return result in array
         $result2 = odbc_fetch_array(odbc_exec(db_src(),$sql)) or die("SQL Error: Function search_client - unable to fetch array");

         //add client address vars together into one.
         $addr = array($result2['Client Adr1'], $result2['Client Adr2'], $result2['Client Adr3'], $result2['Client Town'], $result2['Client Cnty']);
         $fulladdr = implode(",", $addr);

         $search = array($num_rows, $result2['ID'], $result2['Client No'], $result2['Client Name'], $fulladdr, $result2['Client Pcd'], $result2['Client Tel'], $result['Client Fax']);

         break;
    default:
         // Mulitple Records found loop through each record, create data as string, add it to result array.
         $search = array();
         $search[0] = $num_rows;

         for ($i = 1; $i <= $num_rows; $i++) {
             $result2 = odbc_fetch_array(odbc_exec(db_src(),$sql)) or die("SQL Error: Function search_client - unable to fetch array");

             //add client address vars together into one.
             $addr = array($result2['Client Adr1'], $result2['Client Adr2'], $result2['Client Adr3'], $result2['Client Town'], $result2['Client Cnty']);
             $fulladdr = implode(",", $addr);

             $data = array($num_rows, $result2['ID'], $result2['Client No'], $result2['Client Name'], $fulladdr, $result2['Client Pcd'], $result2['Client Tel'], $result['Client Fax']);
             $datastring = implode("#", $data);
             $search[$i] = $datastring;
         }

  }
return $search;
}


Thanks Cep




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users