Display table line following condition – codeigniter

All we need is an easy explanation of the problem, so here it is.

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>

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply