Skip to content
Advertisement

Amateur trying to fix dropdown menu from SQL

I’m trying to help a friend with a website. I usually don’t work with PHP, jquery.

The dropdown has 4 levels. The first level has 4 points. The 4 points have their own sub-levels, different for each other.

I’m trying to find them and then make them display in a dropdown, directly from the database.

And I’m stuck at the second level, with this error

Notice: Trying to get property ‘subcategorie’ of non-object in …..

What I’ve managed to do, until now:

  <?php   $categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
           
             ?>
             
             <?php
             
             $subcategorie = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
           
             ?>
             
             
            <div class="container">    
            
            <?php          
            foreach($categorii as $categorie) {
                
                 ?>
                
            
                <ul>
                    <li>


                <a <?php if(isset($_GET['categorie']) && $categorie->categorie==$_GET['categorie']){ echo "btn-success";}else{ echo "btn-info";};?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode($categorie->categorie); ?>"> <i class="fa fa-caret-down"></i>
                <?php echo $categorie->categorie  ; ?> </a>
                     
                     <ul class="">
                         <li>                  <a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode($_GET['categorie']); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>"  class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> "><?php echo $subcategorie->subcategorie; ?> </a>



                         </li>
                         
                     </ul>
                     
                </li>
                     
                     
                </ul>     
                     
                </a>

Advertisement

Answer

Hi You need to do a foreach loop for $subcategorieas you did for $categorii. Because you are getting an array and not a object as you got for $categorii. Here I set it around the li because it seems to be the best.

<?php
    $categorii = $wpdb->get_results("SELECT DISTINCT categorie FROM catalog_rural");
?>

<?php
    $subcategories = $wpdb->get_results("SELECT DISTINCT subcategorie FROM catalog_rural WHERE categorie = 'Afaceri'");
?>


<div class="container">
    <?php          
        foreach( $categorii as $categorie ) {
    ?>
    <ul>
        <li>
            <a
                <?php
                    #### What are these? Are they classnames? ####
                    if( isset( $_GET['categorie'] ) && $categorie->categorie==$_GET['categorie'] ){
                        echo "btn-success";
                    }else{ 
                        echo "btn-info";
                    };
                ?> href="<?php site_url(); ?>catalog-rural/?categorie=<?php echo urlencode( $categorie->categorie ); ?>">
                <i class="fa fa-caret-down"></i>
                <?php echo $categorie->categorie; ?>
            </a>
            
            <ul class="">
              <?php foreach( $subcategories as $subcategorie ) { ?>
                <li>
                
                    <a href="<?php echo site_url(); ?>/catalog-rural/?categorie=<?php echo urlencode( $_GET['categorie'] ); ?>&subcategorie=<?php echo $subcategorie->subcategorie; ?>"  class="list-group-item <?php if($subcategorie->subcategorie==$_GET['subcategorie']){ echo "active";};?> ">
                        <?php echo $subcategorie->subcategorie; ?>
                    </a>
                </li>
               <?php } ?>
            </ul>
        </li>
    </ul>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement