我有简单的循环函数将数组值输出到网页。如果我从cms3\index.php
使用它,它的效果非常好。但如果我从cms34\ww.admin\pages\menu.php
使用它,那么它只能部分地工作(第一轮)。
我正在翻阅这本书: " CMS设计使用PHP和jQuery", 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';
生成超链接并且循环功能正在运行。
但是,如果我尝试使用来自CMS的cms34\ww.admin\pages\menu.php
中的代码,则无法使用。
答案 0 :(得分:-1)
它没有任何理由开始工作。不知道为什么,但它昨天没有用。如果有人能够解释可能的原因以及如何比等待一天更快地解决它,我会非常感激。