title: Trying to create a categories and sub categorise drop down menu
body: <p>I am trying to create a categories and sub categories drop down menu using PHP, I have created two tables = a table called categories which hold id of the categories and the name and I created a sub categories which hold the id of the sub categories. The id of the main categories and the name of the sub categories, but I am only getting the same sub categories names for all the main  categories and its not linking to the actual name that I set</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>&lt;?php
  class Post{
  
  private $db;
  public function __construct(){
    $this-&gt;db = new Database;
   }
  
   public function getcat(){
     $this-&gt;db-&gt;query('SELECT * FROM categoires');
  
     $results = $this-&gt;db-&gt;resultSet();
     return $results;
   }
  
  
  public function getsubcat(){
  
  $cat = $this-&gt;getcat();
  
  $id = $cat[0]-&gt;id;
  
  
  $this-&gt;db-&gt;query('SELECT * FROM sub_cat WHERE 
  cat_id =:cat_id');
  
  
  
  
   $this-&gt;db-&gt;bind(':cat_id', $id);
   $results = $this-&gt;db-&gt;resultSet();
   return $results;
  
  }
  
  }
  </code></pre>
  
  <p><strong>controller</strong></p>
  
  <pre><code>     public function index(){
  
  
  
  
  
       $cats = $this-&gt;postModel-&gt;getcat();
  
       $sub_cats = $this-&gt;postModel-&gt;getsubcat();
  
  
  
        $data = [
  
                 'cats'=&gt; $cats,
                 'sub_cats' =&gt; $sub_cats
                   ];
  
      $this-&gt;view('posts/index', $data);
    }
  </code></pre>
  
  <p><strong>view</strong></p>
  
  <pre><code>    &lt;?php foreach($data['cats'] as $cats) : ?&gt; 
  
  &lt;li&gt;
  
  
   &lt;?php echo $cats-&gt;cat_name; ?&gt;
  
  
       &lt;?php foreach($data['sub_cats'] as $sub_cats) : ?&gt; 
        &lt;ul&gt;
             &lt;li&gt;&lt;?php echo $sub_cats-&gt;sub_name; ?&gt;&lt;/li&gt;
         &lt;/ul&gt;
        &lt;?php endforeach; ?&gt;
  
  
  &lt;/li&gt;
  
  
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p><strong>Database class</strong></p>
  
  <p>I have created this class to bind value and create prepared statements</p>
  
  <pre><code>&lt;?php
  class Database {
  private $host = DB_HOST;
  private $user = DB_USER;
  private $pass = DB_PASS;
  private $dbname = DB_NAME;
  
  private $dbh;
  private $stmt;
  private $error;
  
  public function __construct(){
    // Set DSN
     $dsn = 'mysql:host=' . $this-&gt;host . ';dbname=' . $this-&gt;dbname;
      $options = array(
      PDO::ATTR_PERSISTENT =&gt; true,
      PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION
    );
  
    // Create PDO instance
    try{
      $this-&gt;dbh = new PDO($dsn, $this-&gt;user, $this-&gt;pass, $options);
    } catch(PDOException $e){
      $this-&gt;error = $e-&gt;getMessage();
      echo $this-&gt;error;
    }
  }
  
  // Prepare statement with query
  public function query($sql){
    $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($sql);
  }
  
  // Bind values
  public function bind($param, $value, $type = null){
    if(is_null($type)){
      switch(true){
        case is_int($value):
          $type = PDO::PARAM_INT;
          break;
        case is_bool($value):
          $type = PDO::PARAM_BOOL;
          break;
        case is_null($value):
          $type = PDO::PARAM_NULL;
          break;
        default:
          $type = PDO::PARAM_STR;
      }
    }
  
    $this-&gt;stmt-&gt;bindValue($param, $value, $type);
  }
  
  // Execute the prepared statement
  public function execute(){
    return $this-&gt;stmt-&gt;execute();
  }
  
  // Get result set as array of objects
  public function resultSet(){
    $this-&gt;execute();
    return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_OBJ);
  }
  
  // Get single record as object
  public function single(){
    $this-&gt;execute();
    return $this-&gt;stmt-&gt;fetch(PDO::FETCH_OBJ);
  }
  
  // Get row count
  public function rowCount(){
    return $this-&gt;stmt-&gt;rowCount();
  }
  }
  </code></pre>
  
  <p>So based on the code that i made it seems that its only getting the first id from the categories table and looping the same name</p>
  
  <p><a href="https://i.stack.imgur.com/RlnOd.png" rel="nofollow noreferrer">categoires Table</a></p>
  
  <p><a href="https://i.stack.imgur.com/IOy3A.png" rel="nofollow noreferrer">sub_cat table</a></p>
  
  <p><a href="https://i.stack.imgur.com/g1opI.png" rel="nofollow noreferrer">The output i get</a></p>