Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Iai galera tudo bem?
estou com um probleminha aqui, estou fazendo um sisteminha em que o cliente possa criar categorias e sub-Categorias, com base no tutorial do blog do Beraldo ESSE AQUI, até ai tudo bem, o banco de dados está dessa forma:
**mysql> select * from categorias;**
+----+--------+----------------------+
| id | id_pai | nome |
+----+--------+----------------------+
| 1 | 0 | A Empresa |
| 2 | 1 | Sobre Nós |
| 3 | 1 | Objetivos |
| 4 | 3 | Objetivo dos nossos |
| 5 | 0 | Contato |
| 6 | 0 | Produtos |
+----+--------+----------------------+
6 rows in set (0,00 sec)
Quem possui id_pai = 0 são as principais categorias.
Fiz um form onde o cliente poderá criar a categoria desejada, para criar a categoria principal ta ok, só que preciso criar as sub-categorias.
eu parei no meio do caminho praticamente, não sei como criar a sub resgatando o id_pai da categoria principal.
meu form está assim:
<?php
include_once('conexao.php');
$nome = isset($_POST['nome']) ? $_POST['nome'] : false;
if(isset($_POST['cadastro']) && $_POST['cadastro'] == 'add'){
if($nome == ""){
echo '<script>alert("Preencha o Nome")</script>';
}else{
if(isset($_POST['cadastro']) && $_POST['cadastro'] == 'add'){
$sql = "INSERT INTO categorias VALUES (null, 0, '$nome')";
$query = mysql_query($sql) or die ("Erro na Insercao ao bando de dados".mysql_error());
if ($query == true) {
echo '<script>alert("Categoria cadastrada!")</script>';
}else{
echo '<script>alert("Ocorreu algum erro ao cadastrar a Categoria!")</script>';
}
}
}
}
//SQL para listar os albuns
$query = mysql_query("SELECT * FROM categorias ORDER BY nome") or die(mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Cadastrar Albuns</title>
<style type="text/css">#corpo{
margin:0 auto;
text-align:center;
}
.titulo{
font-family:Verdana, Geneva, sans-serif;
font-size:20px;
background-color:#09F;
color:#FFF;
text-align:left;
padding:10px;
}
.descricoes {
font-family: verdana;
font-size: 16px;
color:#333333;
letter-spacing:1px;
}
.botao {
background-color: #333333;
height: 40px;
width: 150px;
font-family: verdana;
color: #FFFFFF;
border:0;
font-size: 16px;
cursor:pointer;
border-radius: 5px 5px 5px;
-moz-border-radius: 5px 5px 5px;
-webkit-border-radius: 5px 5px 5px;
float:right;
}background-color:#404040;
}
.textfild {
font-family: Verdana;
font-size: 14px;
font-weight:bold;
color: #333333;
height: 25px;
width: 400px;
border:1px solid #666666;
}
.textarea{
font-family: Verdana;
font-size: 14px;
color: #333333;
height: 150px;
width: 400px;
border:1px solid #666666;
}
.resumo{
width:400px;
font-family: Verdana;
font-size: 14px;
color: #333333;
}
.obs{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
color:#333;
}
.importante{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:14px;
color:#F00;
}border:0;
}
-->
</style>
</head>
<body>
<div id="corpo">
<h2 class="titulo">Cadastrar uma Categoria</h2>
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<label><span class="descricoes">Nome Categoria<br />
</span>
<input name="nome" type="text" class="textfild" /></label>
<br />
<br />
<label><br /></label>
<!-- Indica o tipo que será, no se for album ou Imagem -->
<input type="hidden" name="cadastro" value="add" />
<br />
<input type="submit" class="botao" value=" Categoria" />
</fieldset>
</form>
<br /><br />
<h2 class="titulo">Criar sub-categoria</h2>
<form action="" method="post" enctype="multipart/form-data">
<span class="importante"> <?php
if(isset($msgFoto)) {
printf('<p>%s</p>', $msgFoto);
}
?>
</span>
<fieldset>
<label><span class="descricoes"><br />
Escolha uma Categoria</span>
<select name="id_album">
<option value="" selected="selected">Categorias</option>
<?php
while($row = mysql_fetch_assoc($query)) {
printf('<option value="%u">%s</option>', $row['id_pai'], $row['nome']);
}
?>
</select>
</label>
<br />
<br />
<br />
<label><span class="descricoes">Nome da Sub-Categoria<br />
</span></label>
<label for="nome2"></label>
<input name="nome2" type="text" class="textfild" id="nome2" />
<label><span class="descricoes"> </span></label>
<label for="nome"></label>
<br /> <input type="hidden" name="cadastro" value="add" />
<input type="submit" class="botao" value="Sub-Categoria" />
</fieldset>
</form>
</div>
</body>
</html>
Desde já agradeço e até mais!
Carregando comentários...