[eluser]Tomer Simis[/eluser]
Hello, Im facing some problems with codeigniter and I need some help
I have a function in my controller (categories/update) and this function checks if the id is empty, in case of false it loads a view, in case of true it loads the default view (where I select the category I want to update)
In this view there is a form, and here where the problem begins.
When I submit the form, it goes back to (categories/update) and because the id is not set, it loads the default view. The point is: the form validation doesn't work and neither the model's function "save_changes"
Here are the codes:
Controller:
Code:
function update($id = ''){
$this->load->library('form_validation');
$this->form_validation->set_message('required','O campo <strong> %s </strong> é obrigatório');
$this->form_validation->set_error_delimiters('<li>', '</li>');
if(!empty($id) && is_numeric($id)){
$data['titulo'] = 'BSS Direct - Alterando categoria';
$data['categoria'] = $this->CategoriesModel->selectData($id);
$this->load->view('header',$data);
if($this->form_validation->run() == FALSE){
$this->load->view('categories/form');
}else{
$this->CategoriasModel->save_changes($id,$_POST['nome']);
redirect('index');
}
}else{
$data['titulo'] = 'BSS Direct - Alterar categoria';
$data['categorias'] = $this->CategoriesModel->selectAll();
$this->load->view('header',$data);
$this->load->view('categories/default');
}
}
Model:
Code:
function selectData($id){
$qr = mysql_query("SELECT id,nome FROM `bss`.`bss_categoria` WHERE id='$id'");
return mysql_fetch_array($qr);
}
function save_changes($id,$nome){
$qr = mysql_query("UPDATE `bss`.`bss_categoria` SET nome='$nome' WHERE id='$id'");
}
View:
form
Code:
</head>
<body id="edit">
<div id="topbar"> <!-- Topbar -->
<p>Seja bem-vindo <strong><?php echo $this->session->userdata('nome'); ?> </strong> | <a href="http://localhost/BSS DIRECT/backend.php/index/deslogar">Sair</a></p>
</div> <!-- Topbar -->
<div id="header"> <!-- Header -->
<img src="images/logo.jpg" alt="Home" />
</div> <!-- Header -->
<div id="left">
<?php
$err = validation_errors();
if(!empty($err)){
echo '<div id="aviso">
<h3>Atenção !</h3><ul>
'.$err.'</ul>
</div>';
}
echo form_open('categories/update');
?>
<fieldset name="geral" id="geral">
<div class="box-left">
<div class="box-tb">
<img src="images/upd.jpg" alt="Alterar" />
<h2>Alterando categoria</h2>
</div>
<ul>
<li class="darker">
<label for="nome">Nome da categoria</label>
<input type="text" name="nome" value="<?php echo $categoria['nome']; ?>" />
</li>
</ul>
</div>
</fieldset>
<input type="submit" value="Alterar categoria" />
<br style="clear:both;" />
</form>
</div>
<div id="rodape">
<p>Todos os direitos reservados <br />
<strong>BSS Direct</strong></p>
</div>
</body>
</html>
default
Code:
</head>
<body id="edit">
<div id="topbar"> <!-- Topbar -->
<p>Seja bem-vindo <strong><?php echo $this->session->userdata('nome'); ?> </strong> | <a href="http://localhost/BSS DIRECT/backend.php/index/deslogar">Sair</a></p>
</div> <!-- Topbar -->
<div id="header"> <!-- Header -->
<img src="images/logo.jpg" alt="Home" />
</div> <!-- Header -->
<div id="left">
<div class="box-left">
<div class="box-tb">
<img src="images/upd.jpg" alt="Adicionar" />
<h2>Remover categoria</h2>
</div>
<table width="100%" cellspacing="2" border="1">
<thead>
<tr class="darkerTR">
<td>Id</td>
<td>Nome da categoria</td>
<td>Produtos</td>
<td></td>
</tr>
</thead>
<tbody>
<?php
$i = 0;
while($cat = mysql_fetch_array($categorias)){
$i++;
$num = $this->CategoriasModel->countProdutos($cat['id']);
if($i % 2 == 0){
echo '<tr class="darkerTR">
<td>'.$cat['id'].'</td>
<td>'.$cat['nome'].'</td>
<td>'.$num.'</td>
<td class="last">'.anchor('categories/update/'.$cat['id'],'<img src="images/updateDark.jpg"/>').'</td>
</tr>';
}else{
echo '<tr>
<td>'.$cat['id'].'</td>
<td>'.$cat['nome'].'</td>
<td>'.$num.'</td>
<td class="last">'.anchor('categories/update/'.$cat['id'],'<img src="images/update.jpg" />').'</td>
</tr>';
}
}
?>
</tbody>
</table>
</div>
<br style="clear:both;" />
</div>
<div id="rodape">
<p>Todos os direitos reservados <br />
<strong>BSS Direct</strong></p>
</div>
</body>
</html>
Please guys, help me. I'm getting crazy :ahhh:
Cheers!