I'm doing a forum with PHP and CodeIgniter , and each category should display its subcategories , however, all categories show all sub-categories, how can I make each sub-category appear below its category?
I do not have to do this with a WHERE, but I do not know how to retrieve the category id to do this.
Category Model:
<?php
class Categorias extends CI_Model{
public function findAll(){
$query = $this->db->get("categorias");
return $query->result();
}
}
Subcategory Model:
<?php
class Subcategorias extends CI_Model{
public function findByIdCategoria($id){
$this->db->where("id_categoria", $id);
$query = $this->db->get("subcategorias");
return $query->result();
}
}
Controller:
<?php
class Main extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->helper("url");
$this->load->model("categorias");
$this->load->model("subcategorias");
}
public function index(){
$data['categorias'] = $this->categorias->findAll();
$data['subcategorias'] = $this->subcategorias->findByIdCategoria(/*preciso pegar o id para passar para esse parametro */);
$this->load->view("home", $data);
}
}
View that displays all categories:
<html lang="pt-br">
<head>
<title>Fórum!</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="<?php echo base_url(); ?>estilo/reset.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>estilo/padrao.css" type="text/css" />
</head>
<body>
<div id="site">
<div id="topo"></div>
<div id="linha"></div>
<div id="principal">
<div id="sidebar">
<div class="box_titulo_pequeno"><span class="titulo">Menu</span></div>
<div class="box_conteudo_pequeno"><div class="dez_margin">Início</div></div>
<div class="box_titulo_pequeno"><span class="titulo">Facebook</span></div>
<div class="box_conteudo_pequeno"><div class="dez_margin">Curta nossa página!</div></div>
</div>
<div id="conteudo">
<?php
foreach($categorias as $row){
?>
<div class="box_titulo_grande"><span class="titulo"><?php echo $row->titulo; ?></span></div>
<div class="box_conteudo_grande">
<div class="dez_margin">
<?php
foreach($subcategorias as $row){
?>
<table width="100%" border="1" style="margin-top:5px; background:#E5E5E5;">
<tr>
<td rowspan="2" style="background:url(<?php echo base_url(); ?>estilo/imagens/<?php echo $row->imagem; ?>); width:86px; height:86px;"></td>
<td class="tdtitulo"><?php echo anchor("subcategoria/" . $row->id, $row->titulo); ?></td>
<td rowspan="2" class="tdtopicos">
<center>
<br />
<br />
3 tópicos
</center>
</td>
</tr>
<tr>
<td class="tddescricao"><?php echo $row->descricao; ?></td>
</tr>
</table>
<?php
}
?>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
</body>
</html>