Oke, ik zit heir al wel 3 dagen mee te proberen. ik heb dus een treemenu gemaakt met ongelimiteerd subs maken. Alleen omdat dit per laadpoging ongelovelijk veel tijd kost om de hele tijd query's te maken wil ik hem cachen. Daarom heb ik het geheel omgebouwd in een class.
In de array staat dit:
Echter, nu krijg ik dus doordat ik recursie gebruik de menu's 3x te zien.
Dat komt door dit:
Immers, die build_menu geeft een array door, zo niet dan maakt die een nieuwe array aan. Dit heb ik geprobeerd dooor het te onderdrukken om te kijken of $this->sFormatted leeg is, maar dat werkt dus nog steeds niet.
!is_array($aCached) && empty($this->sFormatted)
Kortom, ik weet niet wat ik fout doe, want als ik dat weghaal en het cache bestand verwijder dan werkt het nog steeds niet.
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
| <?php class menu { private $oDb, $sFormatted; protected $aMenuCache; function __construct() { global $oDb; $this->oDb = $oDb; $this->sFormatted = ''; if ( !$this->cached() ) { $this->write_cache(); } } function build_menu( $aCached=null ) { if ( !is_array($aCached) ) { $this->get_cache(); $aCached = $this->aMenuCache; } $this->sFormatted .= '<ul>'; foreach ( $aCached AS $aCache ) { $this->sFormatted .= '<li><a href="'.$aCache['url'].'">'.htmlentities($aCache['title']).'</a></li>'; if ( isset($aCache['_subs']) ) { $this->sFormatted .= '<li>'.$this->build_menu( $aCache['_subs'] ).'</li>'; } } $this->sFormatted .= '</ul>'; return $this->sFormatted; } function cached () { return file_exists( CACHE_PATH.'/cache/menu.txt' ); } function get_cache() { if ( $sCache = @file_get_contents( CACHE_PATH.'/cache/menu.txt' ) ) { $this->aMenuCache = unserialize( $sCache ); return true; } else { trigger_error( 'Cannot load menu cache, please renew the cache in the admin panel.' ); return false; } } function write_cache() { if ( $rHandle = @fopen( CACHE_PATH.'/cache/menu.txt', 'w' ) ) { fwrite( $rHandle, serialize( $this->menu_cache() ) ); fclose( $rHandle ); } else { return trigger_error( 'Cannot create a cache file, please make shure the cache dir has chmod 777 and safe mode is turned off', E_USER_ERROR ); } } function menu_cache( $iParent = 0, $sParent=ROOT_PATH ) { if ( $this->oDb->count( 'categories', 'parent = ?', $iParent ) > 0 ) { $aMenu = array(); $aResult = $this->oDb->query( 'SELECT * FROM categories WHERE parent = ? ORDER BY niv ASC', $iParent ); foreach ( $aResult AS $aData ) { $aMenuCache = menu_cache( $aData['id'], $sParent.$aData['title_url'].'/' ); if ( is_array($aMenuCache) ) { $aMenu[] = array( 'title' => $aData['title'], 'url' => $sParent.$aData['title_url'], '_subs' => $aMenuCache ); } else { $aMenu[] = array( 'title' => $aData['title'], 'url' => $sParent.$aData['title_url'] ); } } return $aMenu; } else { return false; } } } ?> |
In de array staat dit:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| Array ( [title] => Computers [url] => /computers [_subs] => Array ( [0] => Array ( [title] => Hardware [url] => /computers/hardware ) [1] => Array ( [title] => Software [url] => /computers/software [_subs] => Array ( [0] => Array ( [title] => Microsoft software [url] => /computers/software/microsoft-software ) ) ) ) ) |
Echter, nu krijg ik dus doordat ik recursie gebruik de menu's 3x te zien.
Dat komt door dit:
code:
1
2
3
4
5
| if ( !is_array($aCached) ) { $this->get_cache(); $aCached = $this->aMenuCache; } |
Immers, die build_menu geeft een array door, zo niet dan maakt die een nieuwe array aan. Dit heb ik geprobeerd dooor het te onderdrukken om te kijken of $this->sFormatted leeg is, maar dat werkt dus nog steeds niet.
!is_array($aCached) && empty($this->sFormatted)
Kortom, ik weet niet wat ik fout doe, want als ik dat weghaal en het cache bestand verwijder dan werkt het nog steeds niet.