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