How to assign one array elements to another array?

Viewed 63

I have two tables in database one table is saloon_staff and the second table is saloon_staff_timings

Saloon_staff enter image description here

saloon_staff_timings enter image description here

Now in the saloon_staff_timings table, I am inserting the timing of saloon staff. I have run two queries to get data from the table, The first query is getting the names of saloon staff through saloon_staff_id in saloon_staff_timings table and the second query is getting the timings of the saloon staff.

Here is my code.

<?php 

 foreach ($saloon_staff as $key => $value)
  {
    echo"<tr>";

    echo"<td> $value->staff_name </td>";


      foreach ($staff_timings as $key => $stf_timngs) 
       {
  ?>
     <td><?php echo $stf_timngs->start_time ?></td>
  <?php 
        } 
       echo"</tr>";
     } 
?>

The result of this code. enter image description here

The first foreach loop is showing the names of saloon staff and the second foreach loop is showing the timings of the saloon staff. I have a problem in the second foreach loop, what I want to get is the name and the time against that name. But for now, I am getting all the times available in the database, which is wrong.

Saloon staff timings print_r($saloon_staff)

  Array
    (
        [0] => stdClass Object
            (
                [staff_name] => Salman Iqbal
                [id] => 1
            )

        [1] => stdClass Object
            (
                [staff_name] => Alludin 
                [id] => 2
            )

    )

Saloon staff timings print_r($staff_timings)

 Array
    (
        [0] => stdClass Object
            (
                [id] => 1
                [saloon_staff_id] => 1
                [day] => Monday
                [start_time] => 01:03:00
                [end_time] => 18:00:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 098098098
                [email] => SalmanIq@facebook.com
                [appointments_enabled] => 1
                [role_in_saloon] => Owner
                [name] => Salman Iqbal
            )

        [1] => stdClass Object
            (
                [id] => 1
                [saloon_staff_id] => 1
                [day] => Tuesday
                [start_time] => 03:06:00
                [end_time] => 19:04:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 098098098
                [email] => SalmanIq@facebook.com
                [appointments_enabled] => 1
                [role_in_saloon] => Owner
                [name] => Salman Iqbal
            )

        [2] => stdClass Object
            (
                [id] => 1
                [saloon_staff_id] => 1
                [day] => Wednesday
                [start_time] => 02:08:00
                [end_time] => 21:08:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 098098098
                [email] => SalmanIq@facebook.com
                [appointments_enabled] => 1
                [role_in_saloon] => Owner
                [name] => Salman Iqbal
            )

        [3] => stdClass Object
            (
                [id] => 1
                [saloon_staff_id] => 1
                [day] => Thursday
                [start_time] => 01:00:00
                [end_time] => 14:08:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 098098098
                [email] => SalmanIq@facebook.com
                [appointments_enabled] => 1
                [role_in_saloon] => Owner
                [name] => Salman Iqbal
            )

        [4] => stdClass Object
            (
                [id] => 1
                [saloon_staff_id] => 1
                [day] => Friday
                [start_time] => 01:06:00
                [end_time] => 19:09:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 098098098
                [email] => SalmanIq@facebook.com
                [appointments_enabled] => 1
                [role_in_saloon] => Owner
                [name] => Salman Iqbal
            )

        [5] => stdClass Object
            (
                [id] => 1
                [saloon_staff_id] => 1
                [day] => Saturday
                [start_time] => 01:09:00
                [end_time] => 15:09:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 098098098
                [email] => SalmanIq@facebook.com
                [appointments_enabled] => 1
                [role_in_saloon] => Owner
                [name] => Salman Iqbal
            )

        [6] => stdClass Object
            (
                [id] => 1
                [saloon_staff_id] => 1
                [day] => Sunday
                [start_time] => 00:03:00
                [end_time] => 19:09:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 098098098
                [email] => SalmanIq@facebook.com
                [appointments_enabled] => 1
                [role_in_saloon] => Owner
                [name] => Salman Iqbal
            )

        [7] => stdClass Object
            (
                [id] => 2
                [saloon_staff_id] => 2
                [day] => Monday
                [start_time] => 02:03:00
                [end_time] => 16:09:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 98790809809
                [email] => alludin@gmail.com
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

        [8] => stdClass Object
            (
                [id] => 2
                [saloon_staff_id] => 2
                [day] => Tuesday
                [start_time] => 01:09:00
                [end_time] => 20:01:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 98790809809
                [email] => alludin@gmail.com
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

        [9] => stdClass Object
            (
                [id] => 2
                [saloon_staff_id] => 2
                [day] => Wednesday
                [start_time] => 01:09:00
                [end_time] => 21:00:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 98790809809
                [email] => alludin@gmail.com
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

        [10] => stdClass Object
            (
                [id] => 2
                [saloon_staff_id] => 2
                [day] => Thursday
                [start_time] => 11:00:00
                [end_time] => 13:09:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 98790809809
                [email] => alludin@gmail.com
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

        [11] => stdClass Object
            (
                [id] => 2
                [saloon_staff_id] => 2
                [day] => Friday
                [start_time] => 01:07:00
                [end_time] => 21:00:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 98790809809
                [email] => alludin@gmail.com
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

        [12] => stdClass Object
            (
                [id] => 2
                [saloon_staff_id] => 2
                [day] => Saturday
                [start_time] => 00:00:00
                [end_time] => 00:00:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 98790809809
                [email] => alludin@gmail.com
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

        [13] => stdClass Object
            (
                [id] => 2
                [saloon_staff_id] => 2
                [day] => Sunday
                [start_time] => 00:00:00
                [end_time] => 00:00:00
                [break_start] => 00:00:00
                [break_end] => 00:00:00
                [saloon_profiles_id] => 1
                [phone_number] => 98790809809
                [email] => alludin@gmail.com
                [appointments_enabled] => 1
                [role_in_saloon] => No Access
                [name] => Alludin 
            )

    )

Any help will be appreciated.

4 Answers

Simple You are missing if statement...Try this:-

<?php 

foreach ($saloon_staff as $key => $value){
    echo"<tr>";
    echo"<td> $value->staff_name </td>";
    foreach ($staff_timings as $key => $stf_timngs){
       if($stf_timngs->saloon_staff_id == $value->id ){?>
       <td><?php echo $stf_timngs->start_time ?></td>
   <?php }  }
   echo"</tr>";
 } ?>

Hope it helps!.. if you want to do with joins refer the @sajeevan answer

just use INNER JOIN for join both table and display the data appropriate columns by use condition

SELECT ss.*,sst.* FROM `Saloon_staff` INNER JOIN `Saloon_Staff_timings` AS sst ON sst.`saloon_staff_id`= ss.`saloon_profile_id`

Hi try with the below snippet. It should work.

<?php 

  foreach ($saloon_staff as $key => $value) {

  echo"<tr>";
  echo"<td> $value->staff_name </td>";

  foreach ($staff_timings as $key => $stf_timngs) {

     if($value->id  == $stf_timngs->saloon_staff_id ){
          echo  "<td>".$stf_timngs->start_time."</td>";
      }

    } 

   echo"</tr>";

 } 

In your code, you were just printing out trimmings irrespective of which user timings. so I just added a check against user id in first foreach loop with staff profile id in timings table. which should result in the trimmings related to a particular user.

You need to check ID of $saloon_staff id and $staff_timings id. The reason why it prints all because we are not checking it

Here is the code:

foreach ($saloon_staff as $key => $value){
    echo '<tr>';
    echo '<td>' . $value->staff_name . '</td>';
    foreach ($staff_timings as $key2 => $stf_timngs) {
        // Check ID if equal
        if($value->id == $stf_timings->saloon_staff_id){
            echo '<td>' . $stf_timngs->start_time . '</td>';
        }
    } 
    echo"</tr>";
} 

I also clean code. This is much easier to read since I included the html tags in echo rather than separating the html.

Related