We help IT Professionals succeed at work.

Using while loop to check for names

peter Ojeda
peter Ojeda asked
on
103 Views
Last Modified: 2018-03-27
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

Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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?
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
What does your data look like?

Author

Commented:
Here is some data from both of my tables that I am running a select query from.
ee_example_5_18_2.PNG
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
I meant what does your data structure look like - tables, relationships - and some data in each to see how they fit together.

Author

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.