I want to create a sequence numbering by ID group which comes from a mysql query.
The mysql table looks like this:
--------------------------
id | user_id | company_id 
--------------------------
1  |   61    |     1 
2  |   71    |     1 
3  |   81    |     1 
4  |   91    |     2 
5  |   10    |     2
6  |   11    |     2
And I would like to output soething like this:
Company: 1 , User: 61, position: 1
Company: 1 , User: 71, position: 2
Company: 1 , User: 81, position: 3
Company: 2 , User: 91, position: 1
Company: 2 , User: 10, position: 2
Company: 2 , User: 11, position: 3
I come up with this code but its not working as I wanted to.
    $sql = 'SELECT `id`, `user_id`, `company_id` FROM `user_company` ORDER BY `company_id`';
    $result = $conn->query($sql);
    
    if($result->num_rows){
        $id = '';
        while($obj = $result->fetch_object()){
            if($id != $obj->id){
                $seq = 1;
                $id = $obj->id;
            }else{
                $seq++;
            }
            
            echo 'Company: '.$obj->company_id.', User: '.$obj->user_id.', position: '.$seq.'<br/>';
        }
    }
How can I do this?
 
     
    