Welcome Guest, Not a member yet? Register   Sign In
Connecting two tables
#1

I have two tables like this

1.jobseets

2.jobsheet_tasks

the table jobsheets stores some basic information like customer, date & the jobsheet_tasks only stores the tasks  related to each job sheet.

on "jobsheets_tasks" table. the id of the "jobsheets" that belongs tasks belongs it is storing for reference purpose

here are my tables

[Image: image.jpg]

there can be one more more tasks under a job sheet. 

[Image: image.jpg]

in order to get the information in both tables i have written the following query.

PHP Code:
public function get_jobsheets() {
$this->db->select('jobsheets.id, jobsheets.vehicle_number, jobsheets_tasks.task');  
$this
->db->from('jobsheets');
$this->db->join('jobsheets_tasks''jobsheets_tasks.jobsheet_id = jobsheets.id');
 
$query $this->db->get();


if(
$query->num_rows() > 0) {

 
 foreach ($query->result()as $row){

 
  $data[] = $row;

 
 }
return 
$data ;
 
 }





and i am showing the data in my view like this.

PHP Code:
<?php 
foreach($result as $jobsheet {

 
 ?>
        <tr>
          <td><?php echo  $customer->id;?></td>
          <td><?php echo  $customer->vehicle_number;?></td>
          <td><?php echo  $customer->task;?></td>
 <?php ?>   


and the output is like this

[Image: image.jpg]

the query works, but the thing is  as you can see in the above image the tasks named 1 & 2 are belongs to a one job sheetete which job sheet id is 61.

in the view the tasks belongs to same job sheets shown as separate rows, i want to show the data like this.

[Image: image.jpg]
tasks of same job sheet one after another.

please assist me to achieve this. please provide your solutions in code level 
Reply
#2

(This post was last modified: 08-17-2016, 06:20 PM by Joel Catantan. Edit Reason: correcting if condition )

I assume that the function in your post is withing your Model - I suggest that you must put a formatting of record within the controller.

public function jobsheets()
{
$data = array();
$result = $this->your_model->get_jobsheets();
$record_set_count = 0;

foreach($result as $index => $row)
{
// this will check if the next record of foreach (specifically in id) is the same
if(@$result[$index+1]->id == $row->id)
{
$data[$record_set_count]->task .= '<br>'. $row->task;
}
else
{
$data[] = $row;
$record_set_count++;
}
}

return $data;
}


Cheers! :)
[Just a programmer] Cool [/Just a programmer]
Reply
#3

(08-16-2016, 06:39 PM)Joel Catantan Wrote: I assume that the function in your post is withing your Model - I suggest that you must put a formatting of record within the controller.

public function jobsheets()
{
   $data = array();
   $result = $this->your_model->get_jobsheets();
   $record_set_count = 0;

   foreach($result as $index => $row)
   {
       // this will check if the next record of foreach (specifically in task) is the same
       if(@$result[$index+1]->task == $row->task)
       {
           $data[$record_set_count]->task .= '<br>'. $row->task;
       }
       else
       {
           $data[] = $row;
           $record_set_count++;
       }
   }

   return $data;
}


Cheers! Smile

Thanks, buddy. i think i have to write this in controller is it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB