Skip to content
Advertisement

Display table line following condition – codeigniter

I have a table in our admin panel. The table display the coupon and the deal, but for a better reading we would like to have a split table. 1 for coupons and 1 for deals.

I can’t figure out how to display only the data according to the status if deal or coupons.

Here below is my loop to get all the data, and create the table.

<tbody>
    <?php 
    $this->load->helper('text');
    $count = 0;
    foreach ($result as $key => $value) {
        $count++;?>  
        <tr>
        <td><?php echo $count;?></td>
        <td><?php echo  wordwrap($value->deal_title,25,"<br>n");?></td>
        
        <td> <?php 
                if($value->deal_type == 0){
                    echo 'Deal';
                }else{
                    echo 'Coupon';
                }
            ?>  </td>
        
        <td> <?php 
                $ext = explode(' ', $value->created_at);
                echo $ext[0];
            ?>
            </td>
        <td>
            <?php 
                $user_details = get_likes($value->id);
                echo @$user_details[0]->likes; 
            ?>
        </td>
            <td>
            <?php 
                if($value->deal_status == 1){
                    echo 'Not published';
                }else{
                    echo 'Published';
                }
            ?>
        </td>                                
        
        </tr>
    <?php }?>
</tbody>

Advertisement

Answer

Whether you want two tables or an empty row in the middle of just one table, I strongly recommend that you prepare your data payload almost entirely in the controller layer and pass that data to your view. This means that you will need to manipulate your model data, call get_likes() in an iterated fashion, and potentially sort your data or group your results into a three level data set.

For grouping, the payload should be an associative array containing indexed arrays of objects.

Here is roughly what the view can look like without bouncing in and out of php tags. Remember that the responsibility is not to do data gathering or processing, but to present data.

$rowTemplate= <<<HTML
    <tr>
        <td>%d</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
    </tr>

HTML;

foreach ($groups as $groupName => $groupRows) {
    printf('<table id="%s">', $groupName);
        foreach ($groupRows as $index => $row) {
            printf(
                $rowTemplate,
                $index + 1,
                wordwrap($row->title, 25, "<br>n"),
                ucfirst($groupName),
                strstr($row->created_at, ' ', true),
                $row->user_likes,
                $row->status == 1 ? 'Not published' : 'Published'
            );
        }
    echo '</table>';
}

As for your controller, it should conclude with something like:

$groups = [
    'Deals' => [
        (object)[
            'title' => 'foo',
            'created_at'= '2022-02-14 15:35:56',
            'user_likes' => get_likes($value->id)[0]->likes ?? 0,
            'status' => 1
        ],
        (object)[
            ...
        ],
        ...
    ],
    'Coupons' => [
        (object)[
            ...
        ],
        ....
    ]
];

$this->load->view('part_1_3', ['groups' => $groups]);

Of course, my static declaration of $groups will need to be replaced with your calls to models and other preparations.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement