I am looking through the book:
"CMS Design Using PHP and jQuery",
https://www.packtpub.com/web-develop...php-and-jquery
On chapter three they give simple CMS example, but it is not working.
When i copied the code to the index.php, it worked. But when i used it from CMS - it did not. It is a simple code, which has to generate hyperlinks to the array items.
The script below should read database and generate hyperlinks to two webpages.
Expand|Select|Wrap|Line Numbers
// cms34\ww.admin\pages\menu.php
<?php
echo '<div id="pages-wrapper">';
$rs=dbAll('select id,type,name,parent from pages order by ord,name');
$pages=array();
foreach($rs as $r){
if(!isset($pages[$r['parent']]))$pages[$r['parent']]=array();
$pages[$r['parent']][]=$r;
}
function show_pages($id,$pages){
if(!isset($pages[$id]))return;
echo '<ul>';
foreach($pages[$id] as $page){
echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
echo '<ins> </ins>'.htmlspecialchars($page['name']).'</a>';
show_pages($page['id'],$pages);
echo '</li>';
}
echo '</ul>';
}
show_pages(0,$pages);
echo '</div>';
Expand|Select|Wrap|Line Numbers
This is the same script with added print_r for debugging.
<?php
echo '<div id="pages-wrapper">';
$rs=dbAll('select id,type,name,parent from pages order by ord,name');
print_r('<br> in menu.php rs=').print_r($rs);
$pages=array();
foreach($rs as $r){
print_r('<br> in menu.php before if r=').print_r($r);
if(!isset($pages[$r['parent']]))$pages[$r['parent']]=array();
$pages[$r['parent']][]=$r;
print_r('<br> in menu.php after if r=').print_r($r).print_r(' ;;r[parent][0]=').print_r($r['parent'][0]);
}
function show_pages($id,$pages){
print_r('<br> in menu.php function show_pages, $id =').print_r($id);
print_r('<br> in menu.php function show_pages, $pages =').print_r($pages);
print_r('<br> in menu.php function show_pages, $pages[$id] =').print_r($pages[$id]);
if(!isset($pages[$id]))return;
echo '<ul>';
foreach($pages[$id] as $page){
print_r('<br> in menu.php foreach ').print_r(htmlspecialchars($page['name']));
echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
echo '<ins> </ins>'.htmlspecialchars($page['name']).'</a>';
show_pages($page['id'],$pages);
echo '</li>';
}
echo '</ul>';
}
show_pages(0,$pages);
echo '</div>';
This is how webpage looks:
Expand|Select|Wrap|Line Numbers
in menu.php rs=Array ( [0] => Array ( [id] => 24 [type] => 0 [name] => Home [parent] => 0 ) [1] => Array ( [id] => 35 [type] => 0 [name] => Home2 [parent] => 0 ) [2] => Array ( [id] => 36 [type] => 0 [name] => test [parent] => 24 ) [3] => Array ( [id] => 38 [type] => 0 [name] => test3 [parent] => 24 ) [4] => Array ( [id] => 25 [type] => 0 [name] => Second Page [parent] => 0 ) )
in menu.php before if r=Array ( [id] => 24 [type] => 0 [name] => Home [parent] => 0 )
in menu.php after if r=Array ( [id] => 24 [type] => 0 [name] => Home [parent] => 0 ) ;;r[parent][0]=0
in menu.php before if r=Array ( [id] => 35 [type] => 0 [name] => Home2 [parent] => 0 )
in menu.php after if r=Array ( [id] => 35 [type] => 0 [name] => Home2 [parent] => 0 ) ;;r[parent][0]=0
in menu.php before if r=Array ( [id] => 36 [type] => 0 [name] => test [parent] => 24 )
in menu.php after if r=Array ( [id] => 36 [type] => 0 [name] => test [parent] => 24 ) ;;r[parent][0]=2
in menu.php before if r=Array ( [id] => 38 [type] => 0 [name] => test3 [parent] => 24 )
in menu.php after if r=Array ( [id] => 38 [type] => 0 [name] => test3 [parent] => 24 ) ;;r[parent][0]=2
in menu.php before if r=Array ( [id] => 25 [type] => 0 [name] => Second Page [parent] => 0 )
in menu.php after if r=Array ( [id] => 25 [type] => 0 [name] => Second Page [parent] => 0 ) ;;r[parent][0]=0
in menu.php function show_pages, $id =0
in menu.php function show_pages, $pages =Array ( [0] => Array ( [0] => Array ( [id] => 24 [type] => 0 [name] => Home [parent] => 0 ) [1] => Array ( [id] => 35 [type] => 0 [name] => Home2 [parent] => 0 ) [2] => Array ( [id] => 25 [type] => 0 [name] => Second Page [parent] => 0 ) ) [24] => Array ( [0] => Array ( [id] => 36 [type] => 0 [name] => test [parent] => 24 ) [1] => Array ( [id] => 38 [type] => 0 [name] => test3 [parent] => 24 ) ) )
in menu.php function show_pages, $pages[$id] =Array ( [0] => Array ( [id] => 24 [type] => 0 [name] => Home [parent] => 0 ) [1] => Array ( [id] => 35 [type] => 0 [name] => Home2 [parent] => 0 ) [2] => Array ( [id] => 25 [type] => 0 [name] => Second Page [parent] => 0 ) )
As you can see, data are fetched from the database. But the recurrent function "show_pages($id,$pages)" is not executed the second round. I conclude this, because this line
Expand|Select|Wrap|Line Numbers
print_r('<br> in menu.php foreach ').print_r(htmlspecialchars($page['name']));
does not show up on webpage output.
I mean this part of show_page function is not executed:
Expand|Select|Wrap|Line Numbers
foreach($pages[$id] as $page){
print_r('<br> in menu.php foreach ').print_r(htmlspecialchars($page['name']));
echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
echo '<ins> </ins>'.htmlspecialchars($page['name']).'</a>';
show_pages($page['id'],$pages);
echo '</li>';
}
I do not understand where is the error?
When i copied the code above to the separate file, it worked. But when i used it from CMS - it did not.
In CMS i go to:
http://localhost/cms34/ww.admin/index.php
which requires 'pages.php';
which requires 'header.php' and 'pages/menu.php';
the header.php requires 'admin_libs.php',
which requires require $_SERVER['DOCUMENT_ROOT'].'/cms34/ww.incs/basics.php';
which has __autoload, and DB connection functions.
'pages/menu.php' is shown in the question above. It gets data about webpages from the table:
$rs=dbAll('select id,type,name,parent from pages order by ord,name');
and than executes recurrent function to generate hyperlinks.
If i copy everything from 'pages/menu.php' to index.php
and prepend line
Expand|Select|Wrap|Line Numbers
require $_SERVER['DOCUMENT_ROOT'].'/cms34/ww.incs/basics.php';
The hyperlinks are generated and recurrent function is working.
But if i try to use 'pages/menu.php' from CMS, it is working only partialy.
Expand|Select|Wrap|Line Numbers
//this line retrieves webpages from the database
$rs=dbAll('select id,type,name,parent from pages order by ord,name');
Expand|Select|Wrap|Line Numbers
// function show_pages($id,$pages) outputs only first round. It does not execute :
foreach($pages[$id] as $page){
print_r('<br> in menu.php foreach ').print_r(htmlspecialchars($page['name']));
echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
echo '<ins> </ins>'.htmlspecialchars($page['name']).'</a>';
show_pages($page['id'],$pages);
echo '</li>';
}