Rossitten
Rossitten

Reputation: 1166

Two loops - how to stop concatenation

Well, I have spent THREE (!!!) hours trying to solve this I swear, simple problem. The problem is this: I have two foreach loops. One goes through a "work", another loops through "workers".

Desired result is a list of works with a where all workers are listed and one which is selected.

  1. work one - worker John (PeterJohn)
  2. work two - worker - Peter etc.

here is the code:

// GETTING WORKS
$get_works = Db::query("SELECT * FROM `tasks_works` WHERE `task_id` = '$get_task[id]' ORDER BY `id`");

if(mysqli_num_rows($get_works) > 0)
{

    $get_workers = Db::query("SELECT `name_first`, `name_last`, `email`, `id` FROM `users` WHERE `rights` = '1' OR `rights` = '2' AND `deleted` != '1'");

    foreach($get_works as $task_work)
    {

            // looping through workers
            foreach($get_workers as $worker)
            {


                $workers_list .= '<option value="'.$worker['id'].'" '.(($task_work['worker'] == $worker['id']) ? 'selected' : '').'>'.$worker['name_first'] . ' ' . $worker['name_last'].'</option>';


            }               


        // combining it all                     
        $works .= '<div class="row wrk" id="'.$task_work['id'].'">
        <div class="col-sm-5"><input type="text" class="wrk_additional" value="'.$task_work['work'].'"></div>
        <div class="col-sm-2"><input type="text" class="wrk_additional_price" value="'.$task_work['price'].'"></div>
        <div class="col-sm-4"><select class="wrk">'.$workers_list.'</select></div>
        <div class="col-sm-1 delete_item kill_work" data-id="'.$task_work['id'].'"><img src="'.Site_url.'/gov/i/delete.png" alt="X" /></div>
        </div>';


    }


} 

With this code I receive incremented concatenated list of workers

<select>
<option>John</option>
<option>Paul</option>
</select>


<select>
<option>John</option>
<option>Paul</option>
<option>John</option>
<option>Paul</option>
</select>

etc

...

Obviously - if I place the workers loop outside of the tasks loop - everything is just fine but I cannot insert "selected" to the list of options.

I have tried to add a condition to the workers loop:

$i = 0;
foreach($get_workers as $worker){

if($i == mysqli_num_rows($get_workers)) break;
$workers_list .= '<option value="'.$worker['id'].'" '.(($task_work['worker'] == $worker['id']) ? 'selected' : '').'>'.$worker['name_first'] . ' ' . $worker['name_last'].'</option>';
$i++;

}   

this works BUT - for some reason "select" is placed wrongly and my brain rejects to comprehend - why...

Please help!

Upvotes: 0

Views: 37

Answers (1)

madalinivascu
madalinivascu

Reputation: 32354

Reset the value after each work loop

$workers_list = '';//set the list empty
foreach($get_workers as $worker)
            {


                $workers_list .= '<option value="'.$worker['id'].'" '.(($task_work['worker'] == $worker['id']) ? 'selected' : '').'>'.$worker['name_first'] . ' ' . $worker['name_last'].'</option>';

            }

Upvotes: 1

Related Questions