Take value from a field in the database while in another "id"?

0

I want to show the categories in a menu, and subcategories with a drop-down. (In the image is wrong, should appear With engine and within Diesel and Gasoline )

With these values in the database

The 1st part of the if does its function, but when I get to the second part I do not know how to print the name of the id_padre .

The objective is to click on Con motor and subcategories appear.

        if ($row["id_padre"]==NULL){?>
          <a class="list-group-item" data-remote="true" href="#" id="<?php  echo $row["id"]; ?>" style="padding-left: 25px;">
            <span class="fa <?php echo $row['icon']; ?> fa-lg fa-fw"></span>
            <span style="margin-left: 25px;"><?php echo $row['nombre']; ?></span>
          </a> <?php
        }
        else{ ?>
          <a class="list-group-item" data-remote="true" href="#" id="<?php  echo $row['id']; ?>" data-toggle="collapse" data-parent="<?php  echo $row['id_padre']; ?>" style="padding-left: 25px;">
            <span class="fa fa <?php echo $row['icon']; ?> fa-lg fa-fw"></span>
            <span style="margin-left: 25px;"><?php echo $row['nombre']; ?></span>
            <span class="menu-ico-collapse"><i class="fa fa-chevron-down"></i></span>
          </a>

          <div class="collapse list-group-submenu" id="<?php  echo $row['id']; ?>">
            <a href="#" class="list-group-item sub-item" data-parent="#" style="padding-left: 78px;"><?php echo $row['nombre']; ?></a>
          </div> <?php
        }?>
    
asked by Enric 30.11.2017 в 16:57
source

1 answer

0

You can subquery the same table with the external reference, try this

SELECT 
  categorias.id_padre AS padre,
  categorias.*, 
  (SELECT nombre FROM categorias WHERE id = padre ) AS nombre_padre
FROM categorias
    
answered by 30.11.2017 в 17:16