问题
I have an application in which I can assign task like cleaning the bathroom, washing the car and etc. I can assign a single task to let's say 3 to 4 persons using the code below
if (isset($_POST["btnassign"])){
$proj = $_REQUEST['projhid'];
$analyst = $_POST['analyst'];
$commaList = implode('| ', $analyst);
$queupass = "UPDATE projects set assignedto='$commaList', assignedby = '$uname' where projectname='$proj'";
$queresupass = odbc_exec($conn,$queupass);
$notifassign = "New Project Assignment";
$queprojgn = "INSERT INTO notification (notification,datetime,isread,createdby,createddate)
values('$notifassign',GETDATE(),0,'$uname',GETDATE())";
$queprojresnot = odbc_exec($conn,$queprojgn);
echo "<script type='text/javascript'>";
echo "window.close();";
echo "</script>";
The code above is the assigning part which works great since I can assign a single task to number of workers using their ID.
Now, I also have an anchor tag which will show the workers assigned for that project like the one below which says ANALYST
If I click that anchor tag, I want to get the list of analyst assigned for that task, but remember that the assigned workers using their ID are imploded like to
1| 2| 3| 4| 5
I have this code below which explodes it
$que = "SELECT assignedto FROM PROJECTS where projectname = '$projname'";
$queres = odbc_exec($conn,$que);
$res = odbc_result($queres, 1);
$analysts = explode("| ", $res);
echo $analysts[0]; // piece1
echo $analysts[1]; // piece2
echo $analysts[2]; // piece1
echo $analysts[3]; // piece2
echo $analysts[4]; // piece1
echo $analysts[5]; // piece2
echo $analysts[6]; // piece1
echo $analysts[7]; // piece2
die();
which is eventually works but what I need is to explode it an place it in table that has a remove anchor tag like the one below, any help will be highly appreciated.
回答1:
This can be achieved by:
Selecting the
assignedto
from the table like this$que = "SELECT assignedto FROM PROJECTS where projectname = '$projname'";
Exploding the string received:
$analysts = explode("| ", $res);
Using array_diff to get the remove the desired worker:
$new_analysts = array_diff($analysts, $remove_worker_id);
Imploding the new array to a pipe seperated string:
$result = implode("| ", $new_analysts);
EDIT:
For creating the HTML table with the remove link,
$que = "SELECT assignedto FROM PROJECTS where projectname = '$projname'";
$queres = odbc_exec($conn,$que);
$res = odbc_result($queres, 1);
$analysts_arr = explode("| ", $res);
$analysts = implode(",", $analysts_arr);
$que2 = "SELECT [user_id, first name, last name] FROM [user table] where [user_id] IN '$analysts'";
$query = odbc_exec($conn,$que2);
$result = odbc_result_all($query);
foreach($result as $val){
$user_id = $val['user_id'];
$users[$user_id] = $val;
}
After this, all you have to do is iterate the $analysts_arr
and create the HTML needed for the table.
Hope this helps.
Cheers!
来源:https://stackoverflow.com/questions/34308023/editing-an-imploded-string