1

I have this table:

table with records example

I am trying show a hierarchical combo something like this:

combo

It is posible to do this using a select statement or how I can implement this via PHP? I was googling and I found that it is posible using recursive functions but it is new for me and I can not implement this. May be somebody give me an idea?

Update:- I finally solved my problem. I create a recursive function based on @user2433317 code, first I create and initialized a variable to control the amount of blank spaces that I need in each level

$n=-3;

next I write the recursive function

function ShowSubCats($id){
global $cn, $n;
$sql = "SELECT * FROM menus WHERE idPadre = '$id';";
$r = mysqli_query($cn, $sql);       
if (mysqli_num_rows($r) > 0) {
    $n+=3;
    while ($row = mysqli_fetch_object($r)) {
        echo "<option value='$row->id'>".space($n).$row->titulo."</option>" ;
        ShowSubCats($row->id);                  
    }
        $n-=3;
    }
}
        

and call it with 0 argument that mean idPadre=0 don't have parent. Finally this is the space function

function space($n){
$str = "";
for ($i=0; $i<$n; $i++) { 
    $str .= "&nbsp;";
}
return $str;

}

and the result is showed here

enter image description here

thank you for your answers and sorry for my english is not very good

2 Answers 2

4

Something like this should do the trick, get parent categories, then call a rescursive function to get all childs.

$sql = "SELECT * FROM table WHERE idParent = 0 ";
$results = mysqli_query($sql);
while ($row = mysqli_fetch_array($results)) {
    echo '<li>' . $row['title']) . '</li>';
    ShowSubCats($row['id']);
}


function ShowSubCats($categoryid) {

    $sql = "SELECT * FROM table WHERE idparent='$categoryid';";
    $results = mysqli_query($sql);

    if (mysqli_num_rows($results) > 0) {
        while ($row = mysqli_fetch_array($results)) {
            echo '<li>' . $row['title'] . '</a>';
            ShowSubCats($row['idparent']);
            echo '</li>';
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

First make a function called has_parent():

function has_parent( $id )
{
    global $connection;
    $sql = "SELECT parent FROM table WHERE id = {$id} LIMIT 1";
    $results = mysqli_query($sql);
    $row = mysqli_fetch_assoc($results);

    if ( $row == 0 )
    {
        return false;
    } else {
        return true;
    }
}

Make it perfect yourself name the fields etc. and warning I didn't test it. It'll basically return true if the given id has a parent and false elsewise.
Next Loop through all the entries in the database:
echo ''; while( $row = mysqli_fetch_assoc($results_of_mysqli_for_all_results) ){

   ?>

        <?php if (!has_parent( $row['id'] )): ?>
            <li><?php echo $row['yourField']; ?></li>
        <?php else: ?>
            <li><?php echo $row['yourField'] ?>
                <?php
                    echo '<ul>';
                    // loop through all the entries with the parent = $row['id'] ( I think you can make this yourself )
                    echo '</ul>';
                ?>
            </li>
        <?php endif ?>
   <?php  

}
echo '</ul>';

EDIT:
If you want select menu see this question.
EDIT:
Ask me if you have any questions or if it didn't work ( I didn't test it myself though )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.