How to loop a data in php [duplicate]

人走茶凉 提交于 2020-01-25 06:51:33

问题


I want to loop my data Like this

Like this

Not like this

My Controller:

function bills($patient_id){
    $data['payments'] = $this->model_patient->getpayments($patient_id);
    $this->load->view('includes/header');
    $this->load->view('billing', $data);
    $this->load->view('includes/footer');
}

My Model: // joining my 3 tables, i want to display the procedures made by this patients and all the payments made.

function getpayments($patient_id){
    $this->db->select('*');
    $this->db->from('treatmentrecord as tr');
    $this->db->join('patientprofile as pr','pr.patient_id = tr.patient_id');
    $this->db->join('billedrecord as br','br.treatment_id = tr.treatment_id');
    $this->db->where('tr.patient_id', $patient_id);

    $query = $this->db->get();

    return $query->result();
}

My View:

<table>
<thead>
    <tr>
        <td>Procedures</td>
        <td>Payments</td>
    </tr>
</thead>
<?php foreach ($payments as $payments): ?>

<tbody>
    <tr>
        <td><?php echo $payments->procedures; ?></td>
        <td><?php echo $payments->bill_paid; ?></td>
    </tr>
</tbody>
<?php endforeach ?>

</table>

I hope someone can help me with this.

OP: Array( [0] => stdClass Object ( [treatment_id] => 1 [procedures] => ProcedureName1 [patient_id] => 1 [bill_id] => 3 [bill_paid] => 500 )

[1] => stdClass Object
    (
        [treatment_id] => 5
        [procedures] => ProcedureName3
        [patient_id] => 1
        [bill_id] => 4
        [bill_paid] => 2323
    )

[2] => stdClass Object
    (
        [treatment_id] => 1
        [procedures] => ProcedureName1
        [patient_id] => 1
        [bill_id] => 5
        [bill_paid] => 2555
    )

[3] => stdClass Object
    (
        [treatment_id] => 6
        [procedures] => ProcedureName2
        [patient_id] => 1
        [bill_id] => 6
        [bill_paid] => 2555
    )

[4] => stdClass Object
    (
        [treatment_id] => 1
        [procedures] => ProcedureName1
        [patient_id] => 1
        [bill_id] => 7
        [bill_paid] => 23232
    )

[5] => stdClass Object
    (
        [treatment_id] => 5
        [procedures] => ProcedureName3
        [patient_id] => 1
        [bill_id] => 8
        [bill_paid] => 2323
    )

)


回答1:


  • First sort the $payments with respect to procedures like below:

Snippet:

usort($payments,function($a,$b){
    return strcmp($a->procedures,$b->procedures);
});
  • Now, you can take help of the index of the element while looping. So if it's first index or name doesn't match with previous index, just print it, else print an empty string.

Snippet:

<table>
<thead>
    <tr>
        <td>Procedures</td>
        <td>Payments</td>
    </tr>
</thead>
<?php foreach ($payments as $index => $payment): ?>


<tbody>
    <tr>
        <td><?= $index === 0 || $payment->procedures != $payments[$index - 1]->procedures  ? $payment->procedures : ''; ?></td>
        <td><?= $payment->bill_paid; ?></td>

    </tr>


</tbody>
<?php endforeach; ?>



回答2:


controller

function bills($patient_id){
    $payments = $this->model_patient->getpayments($patient_id);

    $result = array();
        foreach ($payments as $element) {
            $result[$element->treatment_id][] = $element;
        }

    $data['payments'] = $result;
    $this->load->view('includes/header');
    $this->load->view('billing', $data);
    $this->load->view('includes/footer');
}

view

<table>
        <thead>
            <tr>
                <td>Procedures</td>
                <td>Payments</td>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach ($payments as $index => $payment) {
                foreach ($payment as $key => $pay) {
                    ?>
                    <tr>
                        <td>
                            <?php
                            if ($key == 0) {
                                echo $pay->procedures;
                            }
                            ?>
                        </td>
                        <td><?php echo $pay->bill_paid; ?></td>
                    </tr>
                    <?php
                }
            }
            ?>
        </tbody>
    </table>


来源:https://stackoverflow.com/questions/58889416/how-to-loop-a-data-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!