How to hide repeating part of the retrieved data values using php and codeigniter

Viewed 2647

I have obtained following table as a result of this MySQL statement

SELECT * FROM expenses, income 
WHERE expenses.projectname=income.projectname 
AND expenses.task=income.task

enter image description here These are the fields in my project table

enter image description here

These are the fields of my task table enter image description here

In this table, one project has many tasks. Therefore project, client, project start and end date column repeat meaninglessly. How can I show them only once for all tasks? How can I apply PHP hide logic here? Following diagram shows what I need to achieve. Data are retrieved through MySQL query. But how can I hide unnecessary values repeating enter image description here This is the CodeIgniter view page

<table class="table table-lg">
<thead >
    <tr class="filters">

       <th><input type="text" class="form-control" placeholder="Project" disabled></th>
        <th><input type="text" class="form-control" placeholder="Employee" disabled></th>
        <th><input type="text" class="form-control" placeholder="Task" disabled></th>
        <th><input type="text" class="form-control" placeholder="Expense" disabled></th>
        <th><input type="text" class="form-control" placeholder="Amount" disabled></th>
        <th><input type="text" class="form-control" placeholder="Paid/Not" disabled></th>
        <th><input type="text" class="form-control" placeholder="Client" disabled></th>
        <th><input type="text" class="form-control" placeholder="Cost" disabled></th>
        <th><input type="text" class="form-control" placeholder="Income " disabled></th>
        <th><input type="text" class="form-control" placeholder="Date" disabled></th>

    </tr>
</thead>
<tbody>
<?php
if(isset($view_data1) && is_array($view_data1) && count($view_data1)): $i=1;
foreach ($view_data1 as $key => $data) { 
?>

<tr <?php if($i%2==0){echo 'class="even"';}else{echo'class="odd"';}?>>

    <td><?php echo $data['projectname']; ?></td> 
    <td><?php echo $data['employee']; ?></td> 
    <td><?php echo $data['task']; ?></td> 
    <td><?php echo $data['ExpenseName']; ?></td> 
    <td><?php echo $data['ExpenseAmount']; ?></td>
    <td><?php echo $data['pn']; ?></td>  
   <td><?php echo $data['cname']; ?></td>
   <td><?php echo $data['taskcost']; ?></td> 
   <td><?php echo $data['amount']; ?></td> 
   <td><?php echo $data['datetimepicker_mask']; ?></td> 
</tr>
<?php
    $i++;
      }
    else:
?>
<tr>
    <td colspan="7" align="center" >No Records Found..</td>
</tr>
<?php
    endif;
?>
</tbody>                
</table>
10 Answers

This is my way of dealing with it. I hope this is the same as the problem you encounter :)

*important, variables $dcost and $cd are variables that I set myself, please adjust it to what you have

before before after after before code

foreach(your sql variable){
echo '<tr><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['destination'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timeout'].'</td>';
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="mday">'.$cd['mday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-mrate="'.$cd['mrate'].'" class="mrate">'.rupiah($cd['mrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tm"></td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="lday">'.$cd['lday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-lrate="'.$cd['lrate'].'" class="lrate">'.rupiah($cd['lrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tl"></td></tr>';}

after code

foreach(your sql variable){
echo '<tr>';
if($i - 1 == 1){
    $i = 0;
echo '  <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['destination'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timein'].'</td>
        <td style="border-right:1px solid #000;text-align:center;vertical-align:middle;">'.$dcost['timeout'].'</td>';
}else{
echo '  <td colspan="5" style="border-right:1px solid #000;">&nbsp;</td>';
}
echo '<td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="mday">'.$cd['mday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-mrate="'.$cd['mrate'].'" class="mrate">'.rupiah($cd['mrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tm"></td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="lday">'.$cd['lday'].'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" data-lrate="'.$cd['lrate'].'" class="lrate">'.rupiah($cd['lrate']).'</td><td style="border-right:1px solid #000;text-align:center;vertical-align:middle;" class="tl"></td></tr>';}
Related