We help IT Professionals succeed at work.

Echo'd values in dropdowns

peter Ojeda
peter Ojeda asked
on
158 Views
Last Modified: 2017-04-13
Hi experts, I have come here before asking for assistance in this scheduling application that I am creating and received great help. I have run into an issue recently and cannot figure out how to fix it. I am attempting to make an update screen for my application, where users can select a date and crew and echo the submitted schedule. This IS working. The issue comes in that I am echoing names into dropdowns as the first value, where the names are already existing. This is causing that name to appear once as echod, and again in the dropdown.

Is there a way I can select a name based off the echo'd value?

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
					<html>
					<head>
					<meta charset="utf-8">
					<meta name="viewport" content="width=device-width, initial-scale=1">
					<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
					<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
					<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
div.container {	background-color:#ffffb3;length: 250%;margin-right: 100px;width: 300%;}
div.submitbutton {position:relative; left:700px;}
									</style>
									</head>
									<body>
									<div class="container">
<article> 
<?php
//$_SESSION['DATE'];
//$_SESSION['CREW'];
		$ID= $_SESSION["DATE"];
		$CREW=$_SESSION["CREW"];
		//$ID=$_GET["ID"];
		//$CREW=$_GET["CREW"];
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$strID = null;
// DEFAULT RETURN
$result = array();
	$serverName = "XX";
	$userName = "";
	$userPassword = '';
	$dbName = "IS_DGMC";
// GET PARAMS SAFELY
$strID = isset($ID) ? $ID : false;
$strCREW = isset($_SESSION["CREW"]) ? $_SESSION["CREW"] : false;
// ONLY PROCEED IF WE GOT VALID PARAMETERS
if ($strID && $strCREW) {
 	$connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);
  $conn = sqlsrv_connect( $serverName, $connectionInfo);
  if( $conn === false ) {
    die( print_r( sqlsrv_errors(), true));
  }
  // SET UP QUERY WITH PARAMETERS
// I THINK THIS MAY BE CLOSER TO WHAT YOU WANT
$stmt = "SELECT * FROM [CE_MANNING_PG2] WHERE [DATE] = ? AND [CREW]= ?
";
  
  // ADD PARAMETER VALUES
  $params = array($strID, $strCREW);
  
$query = sqlsrv_query( $conn, $stmt, $params);
if (!$query) trigger_error( sqlsrv_errors(), E_USER_WARNING); 
  

}
// RETURN DATA
?>
<form id="commonClassForThreeSelects" name="commonClassForThreeSelects" method="post" action= "iframe2_save.php">
<table width="300" border="1" id="commonClassForThreeSelects">
  <tr>
    <th width="91"> <div align="center">Area</div></th>
    <th width="20PX"> <div align="center">Person1</div></th>
    <th width="91"> <div align="center">Person2</div></th>
    <th width="91"> <div align="center">Person3</div></th>
    <th width="91"> <div align="center">Person4</div></th>
    <th width="91"> <div align="center">Person5</div></th>
    <th width="91"> <div align="center">Person6</div></th>
    <th width="91"> <div align="center">Person7</div></th>
    <th width="91"> <div align="center">Person8</div></th>



  </tr>

<?php
$k=0;
while($result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC))
{
	
?>

	<!--<td align="center"  height="50" width="120PX" nowrap> <?php if ($result["DATE"]!= NULL ){echo $result["DATE"] ->format("Y-m-d") ;}?></td>-->
<?php
$selectedname2[0] = $result['PERSON1'];
$selectedname2[1] = $result['PERSON2'];
$selectedname2[2] = $result['PERSON3'];
$selectedname2[3] =$result['PERSON4'];
$selectedname2[4] =$result['PERSON5'];
$selectedname2[5] =$result['PERSON6'];

  echo "<tr>";
echo "<input type='hidden' name='CREW_".$k."' value='$result[CREW]'/>"; 
echo "<input type='hidden' name='SHIFT_".$k."' value='$result[SHIFT]'/>"; 


echo"<td>";echo "<input type='text' name='AREA".$k."' style='width:120px' value='$result[AREA]'/>"; ECHO"</td>";

	for ($a=0; $a<8; $a++){

	echo"<td>";echo"<select  name='PACKER_".$k."_".$a."'/>";

			$location_query = "select distinct NAME from XX.dbo.CE_EMPLOYEES_CREW_A where POSITION = 'GENERAL' order by NAME";
			$locationQ = sqlsrv_query( $conn, $location_query );
			if( $locationQ === false) {
				die( print_r( sqlsrv_errors(), true) );
			}
			$p=0;
            $Lines[0] = '';
			
	
				echo "<option value='$selectedname2[$a]'>$selectedname2[$a]</option>";
			while( $row = sqlsrv_fetch_array( $locationQ, SQLSRV_FETCH_ASSOC) ) {
					if($row['NAME'] != $selectedname[$a]);
					{
						echo "<option value='".$row['NAME']."'>".$row['NAME']."</option>";			

                  $Lines[$p] = $row['NAME'];
                $p++;
		}}}
			$numLines = sizeof($Lines);
			sqlsrv_free_stmt($locationQ);
	echo"</select>";
  ECHO"</tr>";
  
?>
<?php
$k++;
}
  ECHO"</TABLE>";
?>

<div class="submitbutton">
<input type="submit" style="height:30px; width:200px" value="submit" id="#q5">
</div>
</form>
<?php
sqlsrv_close($conn);
?>
</div>
</article>
<script>
var $selects = $('select');
    $('select').change(function () {
        $('option:hidden', $selects).each(function () {
            var self = this,
                toShow = true;
            $selects.not($(this).parent()).each(function () {
                if (self.value == this.value) toShow = false;
            })
            if (toShow) $(this).show();
        });
        if (this.value != 0) //to keep default option available
          $selects.not(this).children('option[value=' + this.value + ']').hide();
    });
</script>

</body>
</html>

Open in new window

Thankyou so much for any help provided.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
Trying to understand the issue here. I tried out a version of your code (adapted so that it will work here) - not sure what I am looking for though. The code itself has some issues that need to be addressed - for instance

The SESSION variables don't make sense - assigning a date to an ID field is confusing and you have this on lines 25-25
$ID= $_SESSION["DATE"];
$CREW=$_SESSION["CREW"];

Open in new window

And then this on lines 39 and 40
$strID = isset($ID) ? $ID : false;
$strCREW = isset($_SESSION["CREW"]) ? $_SESSION["CREW"] : false;

Open in new window

- What is the $CREW variable for
- where is SESSION set
- what is this page supposed to do

Here is my version of it http://www.marcorpsa.com/ee/t2310.php

How must I change my data to see the problem and what am I looking for?

Author

Commented:
Hi Julien thankyou for your input I am still relatively new to a lot of things so I am sure a lot of my code is either unnecessary or not in the best practice. The session variables on line 25 are coming from the previous page. I am passing these variables into  $ID and $CREW, and am then passing those into strings which is passed into the $params (line 54) for the select query. I am attemping to make a scheduling program, where users can go back and modify a schedule that is set on a previous day. After the select statement I am attempting to echo the names from my sql database into the dropdowns as selected. So if "Bob" is echo'd into the dropdown, Bob will appear selected.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Hi sorry for the delay yes the function to not be able to select the same name twice is exactly what I was looking for. But if you see in my code I am echoing a name from my sqldb. I have attached some images below. As you can see, the name "Bishop, La" is being echo'd from my db schedule table. The rest of the value of the dropdown are being echo'd from my db table Employee_Names. If possible, I want to just have the employee name selected based off the value echo'd from the schedule db.
ecample1.PNG
example2.PNG
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Hi Julien I'm sorry but I'm going to have to close the question. I found many more issues in my program that I need to fix first. I will come back to this question sometime in the future.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions