Jump to content

My search function returns the same results for muliple records.


Cep

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]

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.