Jump to content

aaroncatolico1

New Members
  • Posts

    2
  • Joined

  • Last visited

aaroncatolico1's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Great response! I've definitely thought of just creating my own way & then improving on it later. Also, here's what the multilevel menu recursive function would look similar to: <?php $conn = new PDO('mysql:host=localhost; dbname=Mydb', 'MyUsername', 'MyPassword'); function left_nav($parent_id, $conn){ $sql = 'SELECT * FROM navs WHERE parent_id = ?'; $stmt = $conn->prepare($sql); $stmt->bindParam(1, $parent_id, PDO::PARAM_INT); $stmt->execute(); $data = $stmt->fetchAll(PDO::FETCH_ASSOC); if($data){ echo "<ul>\n"; foreach ($data as $row) { echo "<li><a href='#'>{$row['nav_item_name']}</a>"; # Recursive (function calls itself in loop) call for sub-items left_nav($row['id'], $conn); echo "</li>\n"; } echo "</ul>"; } } # Start 'left_nav()' function to build navigation from root level (parent_id = 0) left_nav(0, $conn);
  2. I'm trying not to break the main MVC architecture of my custom PHP MVC framework when generating multi-level navigation menus and a commenting system. In which part of the MVC files should I create recursive functions to generate multilevel navigation menus or if I wanted to generate a commenting system that requires recursion? Here's part of the main MVC folder/path structure that I'm currently using: This is the Core Model "Core/Model.php" file: <?php class Model { private $host = DBHOST; private $user = UN; private $pw = PW; private $dbname = DBNAME; private $dbh; private $stmt; private $error; public function __construct() { $dsn = "mysql:host=$this->host; dbname=$this->dbname"; $options = [ PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]; try { $this->dbh = new PDO($dsn, $this->user, $this->pw, $options); #echo 'Successfully connected to the database using PDO!'; } catch(PDOException $e) { $this->error = $e->getMessage(); echo $this->error; } } public function query($sql){ $this->stmt = $this->dbh->prepare($sql); } #Bind the values: *NOTE: Bind only when selecting by passing a property argument through a param. 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->stmt->bindValue($param, $value, $type); } public function execute(){ return $this->stmt->execute(); } public function resultSet(){ $this->execute(); return $this->stmt->fetchAll(PDO::FETCH_OBJ); } public function single(){ $this->execute(); return $this->stmt->fetch(PDO::FETCH_OBJ); } } Here's the Models directory "models/PagesModel.php" file: <?php class PagesModel { private $db; public function __construct(){ $this->db = new Model(); } #Top Navigation: public function top_nav(){ $this->db->query('SELECT * FROM navs WHERE level = 1'); return $this->db->resultSet(); } } This is the controllers "controllers/Pages.php" file: <?php class Pages extends Controller { #Load the homepage model data and view: public function homepage(){ $topnav = $this->model('PagesModel')->top_nav(); // Call a view helper function to render multi-level menu $data = [ 'topnav' => $topnav, ]; $this->view('pages/index', $data); } } This is the Core "core/Controller.php" file: <?php class Controller { public function model($model){ if(file_exists('../app/models/'. $model . '.php')){ require_once '../app/models/'. $model . '.php'; return new $model(); } } public function view($view, $data = []){ if(file_exists('../app/views/' . $view . '.php')){ require_once '../app/views/' . $view . '.php'; } else { die('View file does NOT exist.'); } } } I can share more of the framework files if needed, but what I'm asking is how would I implement the multilevel navigation menus that require recursive functions without breaking the main architecture of the overall framework? Is this supposed to be done in the helper files or which would you recommend I try? Any support is appreciated.
×
×
  • 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.