Link to home
Start Free TrialLog in
Avatar of peter Ojeda
peter Ojeda

asked on

Using while loop to check for names

Hi experts, I will post the code I am having an issue with below and a screenshot of what it's doing so far. I have two lists of names, one list being packers and my other list being names. I am attempting to perform a check where if a name is used in my one database, it echo's into the "Used" column, and if the name is not in my database it appears as "Unused". In my attached example, you can see the first 4 dropdowns names are used, so they appear in the used column but not the unused column. When I go to the 5th name, it is out of order and then appears in both lists.

Is there a way that my packer variable can look at all $name variables rather then just the ones in chronological order?
   $conn = sqlsrv_connect( $serverName, $connectionInfo);

	if( $conn === false ) {
		die( print_r( sqlsrv_errors(), true));
	}

    $stmt = "SELECT *
FROM CE_EMPLOYEES_CREW_A
WHERE POSITION !='Unavailable'
AND CREW = ?
ORDER BY NAME";
	$params = array($CREW);
   $query = sqlsrv_query($conn, $stmt, $params);

   
       $stmt2 = "SELECT *
FROM CE_MANNING_PACKERS
WHERE ID = ?";
	$params2 =array($ID);
   $query2 = sqlsrv_query($conn, $stmt2, $params2);
   
?>



<table width="500" border="1" id="myTable">
  <tr>
	<th width="91" nowrap> <div align="center">UnUsed</div></th>
	<th width="91" nowrap> <div align="center">Used</div></th>
  </tr>
<?php
$result2 = sqlsrv_fetch_array($query2, SQLSRV_FETCH_ASSOC);
$i=1;
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
$name = $result["NAME"];
$packer =  $result2["PACKER_".$i.""];
if($name != $packer)
	{$test= $name;}
else{$test="";}
?>
  <tr>
	<td NOWRAP><div align="center"><?php echo $test;?></td>
	<td NOWRAP><div align="center"><?php echo $packer;?></td>
  </tr>
<?php
$i++;
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peter Ojeda
peter Ojeda

ASKER

Ray, so after I run the first select query to get all of the packer names, I run the query with all possible names against the first query?
Avatar of Julian Hansen
What does your data look like?
Here is some data from both of my tables that I am running a select query from.
ee_example_5_18_2.PNG
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I meant what does your data structure look like - tables, relationships - and some data in each to see how they fit together.
Hello experts sorry for the delay I thought I commented on this the other day.  I ended up going with another option by using not exists in my sql query. This prevented the names from showing if they were previously submitted