Link to home
Create AccountLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Query and Change Case

I'm making a query.  When I receive the returns,, I need to turn them all into upper case so I can match them a gains another list.  What am I doing wrong here.  I'm a newbie so detail is appreciated:

$q="SELECT manufacturers_id, manufacturers_name FROM manufacturers";
	$r = mysql_query($q) or die(mysql_error());
		while ($row = mysql_fetch_array($r)) {
			$mid = $row["manufacturers_id"];
			$mname = $row["manufacturers_name"];
			//  Create an array of names using the ID as an index			
				$data[$mid] = $mname=array_change_key_case($data, CASE_UPPER);
				 echo $data.'<br />';	
			}

Open in new window

Avatar of Gary
Gary
Flag of Ireland image

$mname = strtoupper($row["manufacturers_name"]);

Not sure what you are trying to do here
$data[$mid] = $mname=array_change_key_case($data, CASE_UPPER)
Avatar of Robert Granlund

ASKER

I get the warning: array_change_key_case() [function.array-change-key-case]: The argument should be an array in  on the LINE:$data[$mid] = $mname=array_change_key_case($data, CASE_UPPER);
$q="SELECT manufacturers_id, UPPER(manufacturers_name) FROM manufacturers";
      $r = mysql_query($q) or die(mysql_error());
            while ($row = mysql_fetch_array($r)) {
                  $mid = $row["manufacturers_id"];
                  $mname = $row["manufacturers_name"];
                  //  Create an array of names using the ID as an index                  
                  $data[$mid] = $mname
                  echo $data.'<br />';      
                  }
OK, both of those work, but I guess my problem is something else.
When I run the following script, it gives me the number of returns.  What I want/need is for the final part return to me the $mid.  If there is a name match in the two querys it reruns to me the first $mid.

$q="SELECT manufacturers_id, manufacturers_name FROM manufacturers";
	$r = mysql_query($q) or die(mysql_error());
		while ($row = mysql_fetch_array($r)) {
			$mid = $row["manufacturers_id"];
			$mname = strtoupper($row["manufacturers_name"]);
			//  Create an array of names using the ID as an index			
				$data[$mid] = $mname;
				 	//  echo $mname.'<br />';
			}



    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {	

 	$nameb = $data[2];
 	// Search the $data array using the name from the CSV file.
 	// If there is a match then $key will have the $mid value that you are looking for.
 	if ($key = array_search($nameb, $data)){
 		$name_B = $key;
		echo $name_B.'<br />';
 	}

Open in new window

Sorry, that makes no sense to me
I have mad an Array out of $mname that is equal to $mid
Then for a CSV file I have created another Array:
   
 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {	
 	$nameb = $data[2];

Open in new window


Then I want to compare the two Arrays and if there are matches, where they match, I want to create a variable using  $mid  with the following:

if ($key = array_search($nameb, $data)){
 		$name_B = $key;
		echo $name_B.'<br />';

Open in new window

Bump this thread tomorrow, I have to go now unless someone else comes along.
Is manufacturers_id an index or a UNIQUE column (so that there are no overwrites possible in the $data array you're building from the query?

Also, why are you reusing (overwriting) the $data variable in the while() loop?  That doesn't make sense to me.
The manufacturers_id is unique.
I'm not sure how to write this, that is why I am asking.
Whats the error/problem,it should work as you have it now?
OK, that's good that manufacturers_id is unique.

Next question: why are you reusing (overwriting) the $data variable in the while() loop?  That doesn't make sense to me.
@Ray should it be like this?

$q="SELECT manufacturers_id, manufacturers_name FROM manufacturers";
	$r = mysql_query($q) or die(mysql_error());
		while ($row = mysql_fetch_array($r)) {
			$mid = $row["manufacturers_id"];
			$mname = strtoupper($row["manufacturers_name"]);
			//  Create an array of names using the ID as an index			
				$data[$mid] = $mname;
				 	//  echo $mname.'<br />';
			}
//  END CREATE MANUFACTURE ID ARRAY
/////////////////////////////////////////////////////////////////////////

//  START WHILE
    while (($data2 = fgetcsv($handle, 1000, ",")) !== FALSE) {
	

 	$nameb = $data2[2];
 	// Search the $data array using the name from the CSV file.
 	// If there is a match then $key will have the $mid value that you are looking for.
 	if ($key = array_search($nameb, $data2)){
 		$name_B = $key;
		echo $name_B.'<br />';
 	}

Open in new window

I'm not sure.  This looks suspicious to me:
$nameb = $data2[2];
if ($key = array_search($nameb, $data2)){

Open in new window

From the description on the man page, it seems like $nameb (the needle) would have to be in the $data2 (haystack) array, since it was just copied out of the $data2 array.  And since we know the key used to copy it was 2, then we would not really need this code, right?
http://php.net/manual/en/function.array-search.php

Maybe if you can give us a plain-language description of what you're trying to achieve, we can offer some traditional design patterns.
data2 should be data - if it's meant to be the first array, searching the second element (presumably the id) of the csv array in the db array.

if ($key = array_search($nameb, $data)){

Confirm what the second element is in the csv
The second element I am searching for is $data2[2];

I have an array of names and ID numbers for each name.
1.I query the DB to get the list of names and ID numbers.
2. I import a CSV that has names but no ID numbers.
If I match a Name from the DB and CSV, I give the CSV Name the corresponding ID Number then enter the CSV info into another table including the discovered ID Number.

That is what I am trying to figure out how to do.
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer