我有一个简单的循环函数输出数组值到网页。如果我从cms3'index.php
使用它,它会完美地工作。但是如果我从cms34'ww.admin'pages'menu.php
中使用它,它只能部分地工作(第一轮)。
我正在翻阅这本书。《使用PHP和jQuery设计CMS》;https://www.packtpub.com/web-develop...php-and-jquery第三章给出了一个简单的CMS示例。
下面的脚本应该读取数据库并生成两个网页的超链接。
// 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>';
这是相同的脚本,增加了print_r
用于调试。
<?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>';
这是网页的样子:
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 ) )
可以看到,数据是从数据库中获取的。但循环函数show_pages($id,$pages)
不执行第二轮。我的意思是show_page
函数的这一部分没有执行:
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>';
}
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
如上题所示。它从表中获取有关网页的数据:
$rs=dbAll('select id,type,name,parent from pages order by ord,name');
,然后执行循环函数生成超链接。
如果我复制所有从pages/menu.php
到cms34'index.php
和前置行:
//this line included file for DB connention and data retrieving functions
require $_SERVER['DOCUMENT_ROOT'].'/cms34/ww.incs/basics.php';
生成超链接,循环函数正在工作。但是,如果我尝试使用代码从cms34'ww.admin'pages'menu.php
从CMS,它是不工作的。
它开始工作没有任何原因。不知道为什么,昨天它不工作了。如果有人能解释一下原因是什么,以及如何比等待一天更快地解决它,我将非常感激。