Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [CI] użycie własnego helpera do kreacji menu na stronie
john_doe
post
Post #1





Grupa: Zarejestrowani
Postów: 873
Pomógł: 25
Dołączył: 24.07.2005

Ostrzeżenie: (0%)
-----


Mam jeszcze starą funkcję do tworzenia wielopoziomowego dropdown menu pobieranego z bazy.

  1. function display_children($parent, $level, $dbh) {
  2.  
  3. $sql = "SELECT a.id id, a.label label, a.link link, Deriv1.Count Count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent;
  4.  
  5. echo "<ul id='nav'>";
  6.  
  7. foreach($dbh->query( $sql ) as $row)
  8. {
  9.  
  10. if ($row['Count'] > 0)
  11. {
  12.  
  13. echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
  14.  
  15. display_children($row['id'], $level + 1, $dbh);
  16.  
  17. echo "</li>";
  18.  
  19. } elseif ($row['Count']==0) {
  20.  
  21. echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
  22. //echo "<li class='level".$level."'><a class='level".$level."' href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
  23.  
  24. } else;
  25. }
  26.  
  27. echo "</ul>";
  28.  
  29. }


podpowiedzcie mi jak to ubrać w CI w helpera.
Funkcja jest rekurencyjna. W helperze nie wychodzi mi db->select(zapytanie).
W helperze uruchamiać metodę z modelu? zagubiłem się ;/


Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
john_doe
post
Post #2





Grupa: Zarejestrowani
Postów: 873
Pomógł: 25
Dołączył: 24.07.2005

Ostrzeżenie: (0%)
-----


Witam,
stworzyłem coś takiego ( narazie roboczo -> html pójdzie do widoku )

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. class Mymenu {
  4.  
  5. function __construct()
  6. {
  7. parent::__construct();
  8. }
  9.  
  10. public function create_menu($parent, $level)
  11. {
  12.  
  13. $query = $this->db->select("SELECT a.id id, a.label label, a.link link, Deriv1.Count count FROM `menu` a LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
  14.  
  15. $menu .= "<ul id='nav'>";
  16.  
  17.  
  18. foreach($query as $position)
  19. {
  20. if($position->count > 0) {
  21.  
  22. $menu .= "<li><a href='" . $position->link . "'>" . $position->label . "</a>";
  23.  
  24. create_menu($position->id, $level + 1);
  25.  
  26. $menu .= "</li>";
  27.  
  28. }elseif($position->count == 0) {
  29.  
  30. $menu .= "<li><a href='" . $position->link . "'>" . $position->label . "</a></li>";
  31.  
  32. }else;
  33.  
  34. }
  35.  
  36. $menu .= "</ul>";
  37.  
  38. return $menu;
  39. }
  40. }
  41.  
  42. /* End of file Mymenu.php */


następnie $this->load->library('mymenu');

dostaję notice
Message: Undefined property: Mymenu::$db
oraz
Fatal error: Call to a member function select() on a non-object


Gdy spróbowałem użyć super obiektu

Fatal error: Cannot access parent:: when current class scope has no parent

  1. function __construct()
  2. {
  3. parent::__construct();
  4. $CI = get_instance();
  5. }

a potem $this->CI->db->select

Fatal error: Cannot access parent:: when current class scope has no parent

Ten post edytował john_doe 15.08.2011, 12:23:53
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 27.09.2025 - 22:56