Jump to content

CMS CRUD tree/recursive menu


Go to solution Solved by QuickOldCar,

Recommended Posts

Hello my good people :)

 

I'm doing a content manager system in PHP and mySQLi, following a serie of video tutorials.

But in my project I am using a tree menu.

In the front office all works smoothly (the tree menu displays all results - menu and submenus - and each button carries the information of the respective page).
 
The problem is in the back office.
The menu is there, in the url appears its id page but the CRUD, I can not put it to work.
 
 
======= CODE START =======

<!-- Gestor de Conteúdos Start -->
  <div class="container-fluid">
    
    <!-- Conteúdo Fluido Start -->
    <div class="row-fluid row-offcanvas row-offcanvas-left">
        
    <?php
    // INSERT QUERY
    if(isset($_POST['enviado']) == 1) {
            
    $header = mysqli_real_escape_string($dbc, $_POST['header']);
    $url = mysqli_real_escape_string($dbc, $_POST['url']);
    $user = $_POST['user'];
    $idPai = $_POST['idPai'];
    $menuNomePT = mysqli_real_escape_string($dbc, $_POST['menuNomePT']);
    $conteudo_pagina_PT = mysqli_real_escape_string($dbc, $_POST['conteudo_pagina_PT']);
    $menuNomeEN = mysqli_real_escape_string($dbc, $_POST['menuNomeEN']);
    $conteudo_pagina_EN = mysqli_real_escape_string($dbc, $_POST['conteudo_pagina_EN']);
            
    $q = "INSERT INTO menuCAL (header, url, user, idPai, menuNomePT, conteudo_pagina_PT, menuNomeEN, conteudo_pagina_EN) VALUES ('$header', '$_POST[url]', $_POST[user], '$_POST[idPai],  '$menuNomePT,  '$conteudo_pagina_PT', '$menuNomeEN', '$conteudo_pagina_EN'";
    $r = mysqli_query($dbc, $q); ?>
   <div>
   <?php
     if($r) {
       $message = '<p>A página foi adicionada!</p>'; 
     } else {
       $message = '<p>A página não foi adicionada, devido ao seguinte erro: '.mysqli_error($dbc);
       $message .=  '<p>' .$q.'</p>';
     } ?>
            
    </div>
    <?php
    }
    ?>
         
    <!-- Menu CAL Start -->
    <div class="col-sm-3 sidebar-offcanvas">
      <?php
      //call the recursive function to print category listing
      category_tree(0);

      //Recursive php function
      function category_tree($menuPai){
      global $dbc;


      $q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'"; 
      $r = mysqli_query($dbc, $q);


      while($btnMenu = mysqli_fetch_assoc($r)):
      $i = 0;
      if ($i == 0) echo '<ul class="menuCAL">';
      echo '<li><a href="?page='.$btnMenu['id'],'">' . $btnMenu['GlyPrincipal'] . $btnMenu['GlySecundario'] . $btnMenu['menuNomePT'], '</a>';
      category_tree($btnMenu['id']);
      echo '</li>';
      $i++;
      if ($i > 0) echo '</ul>';
      endwhile;
    }
    ?>
    </div>
    
    <!-- Menu CAL End -->
    <!-- Conteúdo Start -->  
    <div class="span10">
             
      <div class="col-sm-12">
      <!-- Título Start -->
      <h1 class="page-header">
        <i class="fa fa-file"></i>
        Adicionar Página
      </h1>
      <ol class="breadcrumb">
        <li>
          <a href="#"><i class="fa fa-pencil"></i> Conteúdos</a>
        </li>
        <li class="active">
          <i class="fa fa-file"></i> Nova página
        </li>
      </ol>
      <!-- Título End -->
    </div>
             
    <!-- Textos & Formulários Start --> 
    <div class="row">
      <div class="col-lg-12">
                        
        <p><?php if(isset($message)) { echo $message; } ?></p>
                        
        <?php
        // SELECT QUERY
        if(isset($_GET['id'])) {
        $q = "SELECT * FROM menuCAL WHERE id = $_GET[id]";
        $r = mysqli_query($dbc, $q);


        $opened = mysqli_fetch_assoc($r); 
        }
        ?>
                        
        <!-- Formulário Start --> 
        <form action="adicionar_pagina.php" method="post" role="form">
                            
          <!-- Campo header Start -->
          <div class="form-group">
            <label for="header">Header:</label>
            <input type="text" class="form-control" name="header" id="header" value="<?php echo $opened['header']; ?>" placeholder="Texto descritivo a colocar no topo do website">
          </div>
          <!-- Campo header End -->
                            
          <!-- Campo Label Start -->
          <div class="form-group">
            <label for="url">URL:</label>
            <input type="text" class="form-control" name="url" id="url" value="<?php echo $opened['url']; ?>" placeholder="Texto a colocar na URL (SEO)">
          </div>
          <!-- Campo Label End -->
                            
          <!-- Campo User Start -->
          <div class="form-group">
            <label for="user">Administrador:</label>
            <select class="form-control" name="user" id="user">
              <option value="0">›› Nenhum administrador</option>
          <?php
          $q = "SELECT id FROM users ORDER BY first ASC";
          $r = mysqli_query ($dbc, $q);
          while ($user_list = mysqli_fetch_assoc($r)) { 
          $user_data = data_user($dbc, $user_list['id']);
          ?>
            <option value="<?php echo $user_data['id'] ?>" <?php if($user_data['id'] == $opened['id']) { echo 'selected';} ?>><?php echo $user_data['fullname']; ?></option>
          <?php } ?>
            </select>
           </div>
           <!-- Campo User Start -->
                            
           <!-- Campo ID Menu Pai Start -->
           <div class="form-group">
             <label for="idPai">Adicionar a:</label>
             <select class="form-control" name="idPai" id="idPai">
               <option value="0">›› Seleccione onde adicionar a nova página:</option>
               <?php
                 $q = "SELECT menuNomePT FROM menuCAL WHERE idPai = 0";
                 $r = mysqli_query ($dbc, $q);
  
                 while ($submenus = mysqli_fetch_assoc($r)) { ?>
<option value="<?php echo $submenus['idPai']; ?>"><?php echo $submenus['menuNomePT']; ?></option>
               <?php } ?>
               </select>
             </div>
             <!-- Campo ID Menu Pai Start -->
                            
             <!-- Campo menuNomePT Start -->
             <div class="form-group">
               <label for="menuNomePT">Título PT:</label>
               <input type="text" class="form-control" name="menuNomePT" id="menuNomePT" value="<?php echo $opened['menuNomePT']; ?>" placeholder="Insira o título em Português">
             </div>
             <!-- Campo menuNomePT End -->
                            
             <!-- Campo conteudo_pagina_PT Start -->
             <div class="form-group">
               <label for="conteudo_pagina_EN">Conteúdos PT:</label>
               <textarea class="form-control" name="conteudo_pagina_PT" rows="12" id="conteudo_pagina_PT" placeholder="Insira os textos em Português"><?php echo $opened['conteudo_pagina_PT']; ?></textarea>
             </div>
             <!-- Campo conteudo_pagina_PT End -->
                            
             <!-- Campo menuNomeEN Start -->
             <div class="form-group">
               <label for="menuNomeEN">Título EN:</label>
               <input type="text" class="form-control" name="menuNomeEN" id="menuNomeEN" value="<?php echo $opened['menuNomePT']; ?>" placeholder="Insira o título em Inglês">
             </div>
             <!-- Campo menuNomeEN End -->
                            
             <!-- Campo conteudo_pagina_EN Start -->
             <div class="form-group">
               <label for="conteudo_pagina_EN">Conteúdos EN:</label>
               <textarea class="form-control" name="conteudo_pagina_EN" rows="12" id="conteudo_pagina_EN" placeholder="Insira os textos em Inglês"><?php echo $opened['conteudo_pagina_PT']; ?></textarea>
             </div>
             <!-- Campo conteudo_pagina_EN End -->
               <button type="submit" class="btn btn-default adic_concluir">Gravar</button>
               <input type="hidden" name="enviado" value="1">
                        
           </div>
         </form>
         <!-- Formulário End -->
                        
       </div>
                    
       <!-- Debug Panel Start -->    
       <?php if($debug == 1) { include('widgets/debug.php'); } ?>
       <!-- Debug Panel End -->
                
     </div>
     <!-- Textos & Formulários End -->
   </div>
   <!-- Conteúdo End --> 
        
 </div>
 <!-- Conteúdo Fluido Start -->
      
</div>
<!-- Gestor de Conteúdos End -->

======= CODE END =======

 

 

I can not do the INSERT or UPDATE query, using the form so that the "user" and the "idPai" (a category id), can be created or changed in the database. I tried to echo the database result, but nothing append :( And in the second query (in red in the code), gives me an error (Undefined variable: opened in …). Obviously something is wrong, but i can't see what! :/
 
The database:
post-173766-0-72895200-1421187828_thumb.jpg
 
Can Someone help me? Please !! :)

 

Thank U
Edited by jmlab
Link to comment
https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/
Share on other sites

Your query is failing because of quotes missing

 

You need to concatenate those like below or wrap them with curly braces. You omitted quotes for the key variables and also missing end parenthesis

$q = "INSERT INTO menuCAL (header, url, user, idPai, menuNomePT, conteudo_pagina_PT, menuNomeEN, conteudo_pagina_EN) VALUES ('".$header."', '".$_POST['url']."', '".$_POST['user']."', '".$_POST['idPai']."',  '".$menuNomePT."',  '".$conteudo_pagina_PT."', '".$menuNomeEN."', '".$conteudo_pagina_EN."')";
Edited by QuickOldCar

I also wanted to add you don't even check if all the post values are set and the expected content

 

If assign a variable why aren't you using it in the query?

Make sure every input is escaped going into your query

$user = $_POST['user'];
$idPai = $_POST['idPai'];

 

Adding some error checking in this could help as well, don't do any mysql queries if any errors

$errors = array();
if(isset($_POST['user']) && trim($_POST['user']) !=''){
    $user = mysqli_real_escape_string($dbc, trim($_POST['user']));
} else {
    $errors[] = "No user";
}
//and the rest of your POST values


if(!empty($errors)){
    //do the mysql query
} else {
    foreach($errors as $error){
        echo "$error <br />";
}
}

As for the error, error (Undefined variable: opened in …) does not help us at all, show the full error

 

But is most likely you are using a variable that was never defined and does not exist.

 

 

Am I not creating it here?

<?php
// SELECT QUERY
if(isset($_GET['id'])) {
$q = "SELECT * FROM menuCAL WHERE id = $_GET[id]";
$r = mysqli_query($dbc, $q);


$opened = mysqli_fetch_assoc($r); 
}
?>

What is line 199 and 206 in index.php

 

You can try adding this top of index page and get different error messages that may help.

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');
Edited by QuickOldCar

 

What is line 199 and 206 in index.php

 

You can try adding this top of index page and get different error messages that may help.

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

 

Line 199 and 206 are the title and content in Portuguese (same for title and content in English).

I already have that code in the index. Nothing happened.

 

This is my debug panel:

backofficeDebugPanel.jpg

This is the recursive menu function.

Shouldn't be a function? Is because of this, that we can't "$_GET['id']?

How can I solved this?

<?php
//call the recursive function to print category listing
category_tree(0);

//Recursive php function
function category_tree($menuPai){
global $dbc;


$q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'"; 
$r = mysqli_query($dbc, $q);


while($btnMenu = mysqli_fetch_assoc($r)):
$i = 0;
if ($i == 0) echo '<ul class="menuCAL">';
echo '<li><a href="?page='.$btnMenu['id'],'">' . $btnMenu['GlyPrincipal'] . $btnMenu['GlySecundario'] . $btnMenu['menuNomePT'], '</a>';
category_tree($btnMenu['id']);
echo '</li>';
$i++;
if ($i > 0) echo '</ul>';
endwhile;
}
?>
  • Solution

Think this out and look what it's doing.

 

$menuPai has no value unless define something right there or call on the function and insert it

function category_tree($menuPai){

 

still has no value unless was set

$q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'";

 

 

this could possibly use the function and have a value...the problem is you are within the same function you are trying to call upon

category_tree($btnMenu['id'])

Think this out and look what it's doing.

 

$menuPai has no value unless define something right there or call on the function and insert it

function category_tree($menuPai){

 

still has no value unless was set

$q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'";

 

 

this could possibly use the function and have a value...the problem is you are within the same function you are trying to call upon

category_tree($btnMenu['id'])

 

So, what is the best thing t do?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.