dongshen3352 2016-05-06 22:27
浏览 56

Codeigniter - 数组和foreach循环

A litle over three weeks ago I could barely tell what php was, so please dont be too harsh on me if the answer should be obvious :s

Anyway, I had to get a database for some work stuff, and as per suggestion from a friend of mine I started making it as a website, using mysql and the codeigniter framework. So far so good, I've got prety much everything up and running, but today I felt like changing something on one of the views and I'm not being able to figure out how to do it in what I think it would be the most elegant way.

This is a view for a table showing the results of different lab analysis for a single main entry reccord, independently of the different labs it was run through:

Controller

function ver()
{
    if ($this->session->userdata('id_user') != '') {
        $id_geral = $this->uri->segment(3);
        $data['i'] = 1;
        $this->load->model('entradanimal_model');
        $data['main_content'] = 'descritivo';
        $data['analises'] = $this->entradanimal_model->get_descritivo_analises($id_geral);
        $data['labs'] = $this->entradanimal_model->get_labs();
        $this->load->view('includes/template', $data);}
    else {
            redirect('login');
        };

Model:

function get_descritivo_analises($id_geral)
{
    $query = $this->db->query("SELECT resultados.*, analises.* FROM resultados, analises WHERE resultados.id_geral='" . $id_geral . "' AND resultados.id_analises=analises.id_analises ORDER BY cast(substring_index(resultados.numerointerno, '/', - 1) AS signed INTEGER), cast(substring_index(resultados.numerointerno, '/', 1) AS signed INTEGER) ASC");
    return $query->result();
}
function get_labs()
{
    $query = $this->db->query("SELECT * FROM labs ORDER BY nome_lab");
    return $query->result();
}

View:

<table border="1|0" class="table-hover" align="center">
    <?php foreach ($analises as $s) {
        echo '<thead>
    <tr>
        <th width="150" class="text-center" style="vertical-align: middle">Nº</th>
        <th width="150" class="text-center" style="vertical-align: middle">Nº Interno</th>
        <th width="150" class="text-center" style="vertical-align: middle">Data da Análise</th>
        <th width="250" class="text-center" style="vertical-align: middle">Análise</th>
        <th width="150" class="text-center" style="vertical-align: middle">Técnico Responsável</th>
        <th width="50" class="text-center" style="vertical-align: middle">Ver</th>
        <th width="50" class="text-center" style="vertical-align: middle">Editar</th>
        <th width="50" class="text-center" style="vertical-align: middle">Lâminas</th>

    </tr>
    </thead>
    <tbody>

            <td rowspan="4" class="center" style="...">'. $i++ .'</td>
            <td class="text-center">'.$s->numerointerno.'</td>
            <td class="text-center">'.$s->datadiagnostico.'</td>
            <td class="text-center">'.$s->metodo.'</td>
            <td class="text-center">'.$s->tecnicoresponsavel.'</td>
            <td rowspan="4" class="center" style="vertical-align: middle;"><a class="glyphicon glyphicon-search" style="..." href="/descritivo_lab/ver/'.$s->id_resultados.'"><i class="fa fa-edit"></i></a></td>
            <td rowspan="4" class="center" style="vertical-align: middle;"><a class="glyphicon glyphicon-pencil" style="..." href="/update_consulta/edit_lab/'.$s->id_resultados.'"><i class="fa fa-edit"></i></a></td>
            <td rowspan="4" class="center" style="vertical-align: middle;"><a class="glyphicon glyphicon-plus" style="..." href="/entrada_laminas/add_laminas/'.$s->id_resultados.'/'.$r->id_geral.'"><i class="fa fa-edit"></i></a></td>
        </tr>
        <tr>
        <th class="center" style="...">Resultado Lupa</td>
        <td colspan="3" class="center">'.$s->lupa.'</td>
        </tr>
        <tr>
        <th class="center" style="...">Resultado Macro</td>
        <td colspan="3" class="center">'.$s->macro.'</td>
        </tr>
        <tr>
        <th class="center" style="...">Resultado Micro</td>
        <td colspan="3" class="center">'.$s->micro.'</td>
        </tr>';
    } ?>
    </tbody>
</table>

What I wanted was to get this, but then separate it on the view into different tabs, according to the different labs. I can do this if I create a different array on the controller function for each lab:

$data['lab1'] = $this->entradanimal_model->get_descritivo_analises($id_geral, 1);

and changing the model to:

function get_descritivo_analises($id_geral, $id_labs){
    $query = $this->db->query("SELECT resultados.*, analises.* FROM resultados, analises WHERE resultados.id_geral='" . $id_geral . "' AND resultados.id_labs='" . $id_labs . "' AND resultados.id_analises=analises.id_analises ORDER BY cast(substring_index(resultados.numerointerno, '/', - 1) AS signed INTEGER), cast(substring_index(resultados.numerointerno, '/', 1) AS signed INTEGER) ASC");
    return $query->result_array();}
}

but I'd like to know if and how I could do this a bit more dynamically, any ideas?

EDIT:

What I mean is doing something like

$labs = $this->entradanimal_model->get_labs();

and then

foreach($labs['id_labs'] as $something) {
..... $this->entradanimal_model->get_descritivo_analises($id_geral, $something)

where the model function is something in the likes of what I wrote above:

function get_descritivo_analises($id_geral, $id_labs){
    $query = $this->db->query("SELECT resultados.*, analises.* FROM resultados, analises WHERE resultados.id_geral='" . $id_geral . "' AND resultados.id_labs='" . $id_labs . "' AND resultados.id_analises=analises.id_analises ");
    return $query->result_array();}
}

allowing me to differenciate the data I'm pasting to the view according to the $id_labs column, so I could then have the view generate the different tabs accordingly I'm sure I'm not being very explicit, but this is still a bit too confusing to me :s

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办