@suseelan27
How to repeat foreign id sequentially while insert in php
You have to explain a little more clearly what are you trying to do. You didn't provide any query or any php code related to your issue. I mean a table is good.
LastMitch
Industrious Poster
4,212 posts since Mar 2012
Reputation Points: 134
Solved Threads: 336
Skill Endorsements: 45
Could the assign_work table be incorporated into the complaints table?
Table complaint:
+---------------+---------------+------------+------------+
| complaint_id | complaint | user_id | assigned |
+---------------+---------------+------------+------------+
| 1 | os issue | 7 | 3 |
| 2 | USB issue | 8 | 4 |
| 3 | OS currepted | 7 | 5 |
| 4 | HD issue | 9 | 3 |
| 5 | DVD issue | 6 | 4 |
| 6 | SW problem | 9 | 5 |
| 7 | Network issue| 9 | 3 |
| 8 | system issue | 6 | 4 |
+---------------+---------------+------------+------------+
To keep this flexible, I think you'd need to query the user table first to list all the team members, something like:
SELECT user_id WHERE user_level = 'team' ORDER BY user_id
Make this into an array with a fetchAll (if using mysqli / PDO)
So your array should contain {3,4,5}
Get the last complaint from the table:
SELECT assigned FROM complaint ORDER BY complaint_id DESC LIMIT 1
So from the output of the above we should get '4'. Now get the next team member for your insert query (PDO style)
$last = 4; //from DB
$arr = array(3,4,5); //from DB
function getNextTeam($last, $arr){
$maxKey = count($arr) - 1;
//if the last team member is still in the DB
if($key = array_search($last, $arr)){
if($key < $maxKey){
$getKey = $key + 1;
}elseif($key == $maxKey){
$getKey = 0;
}
//if the last team member has been deleted (just in case)
}else{
foreach($arr as $k=>$v){
if($last < $v){
$getKey = $k;
break;
}
}
if(!isset($getKey))$getKey = 0;
}
return $arr[$getKey];
}
$assigned = getNextTeam($last, $arr);
The above maybe a bit spaghetti-ish (head's shot) and could probably do with some reducing.
So if using a parameterized PDO-style query:
INSERT INTO complaint SET complaint = ':complaint', user_id = :id, assigned = :assigned
diafol
Keep Smiling
10,681 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57
How to repeat foreign id sequentially while insert in php
You might get an error with foreign id if your table is not created correctly:
ERROR 1005 (HY000): Can't create table '' (errno:)
To fixed it. It really depend how your table is structure.
But I think daifol help you clear that up.
LastMitch
Industrious Poster
4,212 posts since Mar 2012
Reputation Points: 134
Solved Threads: 336
Skill Endorsements: 45
If you want a cyclic order, then you will have to store the last used employee id in a configuration table somewhere, so you can use it to determine the next one to use.
pritaeas
Posting Prodigy
9,317 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,467
Skill Endorsements: 86