Jump to content

Archived

This topic is now archived and is closed to further replies.

Cep

My search function returns the same results for muliple records.

Recommended Posts

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?

[code]
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;
}
[/code]

Share this post


Link to post
Share on other sites

×

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.