473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Php, recurrent function is not working

103 100+
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
  1. // cms34\ww.admin\pages\menu.php
  2. <?php
  3. echo '<div id="pages-wrapper">';
  4. $rs=dbAll('select id,type,name,parent from pages order by ord,name');
  5. $pages=array();
  6. foreach($rs as $r){
  7.     if(!isset($pages[$r['parent']]))$pages[$r['parent']]=array();
  8.     $pages[$r['parent']][]=$r;
  9. }
  10. function show_pages($id,$pages){
  11.     if(!isset($pages[$id]))return;
  12.     echo '<ul>';
  13.     foreach($pages[$id] as $page){
  14.         echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
  15.         echo '<ins>&nbsp;</ins>'.htmlspecialchars($page['name']).'</a>';
  16.         show_pages($page['id'],$pages);
  17.         echo '</li>';
  18.     }
  19.     echo '</ul>';
  20. }
  21. show_pages(0,$pages);
  22. echo '</div>';
  23.  

Expand|Select|Wrap|Line Numbers
  1.  This is the same script with added print_r for debugging.
  2. <?php
  3.  
  4. echo '<div id="pages-wrapper">';
  5. $rs=dbAll('select id,type,name,parent from pages order by ord,name');
  6. print_r('<br> in menu.php rs=').print_r($rs);
  7. $pages=array();
  8. foreach($rs as $r){
  9.         print_r('<br> in menu.php before if r=').print_r($r);
  10.     if(!isset($pages[$r['parent']]))$pages[$r['parent']]=array();
  11.     $pages[$r['parent']][]=$r;
  12.         print_r('<br> in menu.php after if r=').print_r($r).print_r('  ;;r[parent][0]=').print_r($r['parent'][0]);
  13. }
  14. function show_pages($id,$pages){
  15.     print_r('<br> in menu.php function show_pages, $id =').print_r($id);
  16.     print_r('<br> in menu.php function show_pages, $pages =').print_r($pages);
  17.     print_r('<br> in menu.php function show_pages, $pages[$id] =').print_r($pages[$id]);
  18.     if(!isset($pages[$id]))return;
  19.     echo '<ul>';
  20.     foreach($pages[$id] as $page){
  21.             print_r('<br> in menu.php foreach ').print_r(htmlspecialchars($page['name']));
  22.         echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
  23.         echo '<ins>&nbsp;</ins>'.htmlspecialchars($page['name']).'</a>';
  24.         show_pages($page['id'],$pages);
  25.         echo '</li>';
  26.     }
  27.     echo '</ul>';
  28. }
  29. show_pages(0,$pages);
  30. echo '</div>';
  31.  
This is how webpage looks:
Expand|Select|Wrap|Line Numbers
  1. 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 ) )
  2. in menu.php before if r=Array ( [id] => 24 [type] => 0 [name] => Home [parent] => 0 )
  3. in menu.php after if r=Array ( [id] => 24 [type] => 0 [name] => Home [parent] => 0 ) ;;r[parent][0]=0
  4. in menu.php before if r=Array ( [id] => 35 [type] => 0 [name] => Home2 [parent] => 0 )
  5. in menu.php after if r=Array ( [id] => 35 [type] => 0 [name] => Home2 [parent] => 0 ) ;;r[parent][0]=0
  6. in menu.php before if r=Array ( [id] => 36 [type] => 0 [name] => test [parent] => 24 )
  7. in menu.php after if r=Array ( [id] => 36 [type] => 0 [name] => test [parent] => 24 ) ;;r[parent][0]=2
  8. in menu.php before if r=Array ( [id] => 38 [type] => 0 [name] => test3 [parent] => 24 )
  9. in menu.php after if r=Array ( [id] => 38 [type] => 0 [name] => test3 [parent] => 24 ) ;;r[parent][0]=2
  10. in menu.php before if r=Array ( [id] => 25 [type] => 0 [name] => Second Page [parent] => 0 )
  11. in menu.php after if r=Array ( [id] => 25 [type] => 0 [name] => Second Page [parent] => 0 ) ;;r[parent][0]=0
  12. in menu.php function show_pages, $id =0
  13. 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 ) ) )
  14. 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
  1. 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
  1.     foreach($pages[$id] as $page){
  2.             print_r('<br> in menu.php foreach ').print_r(htmlspecialchars($page['name']));
  3.         echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
  4.         echo '<ins>&nbsp;</ins>'.htmlspecialchars($page['name']).'</a>';
  5.         show_pages($page['id'],$pages);
  6.         echo '</li>';
  7.     }
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
  1. 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
  1. //this line retrieves webpages from the database
  2. $rs=dbAll('select id,type,name,parent from pages order by ord,name');
Expand|Select|Wrap|Line Numbers
  1. // function show_pages($id,$pages) outputs only first round. It does not execute :
  2.     foreach($pages[$id] as $page){
  3.             print_r('<br> in menu.php foreach ').print_r(htmlspecialchars($page['name']));
  4.         echo '<li id="page_'.$page['id'].'"><a href="pages.php?id='.$page['id'].'">';
  5.         echo '<ins>&nbsp;</ins>'.htmlspecialchars($page['name']).'</a>';
  6.         show_pages($page['id'],$pages);
  7.         echo '</li>';
  8.     }
Oct 18 '15 #1
2 1265
gintare
103 100+
It started working without a reason. Do not know why, but it was not working yesterday.
Oct 19 '15 #2
gintare
103 100+
Still, could you please comment, why CMS files may stop working, and recover by themselves after some time. Thank you a lot.
Oct 19 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Andy Fish | last post by:
Hi I am using Xalan-J 2.4.1 and I am trying to get the EXSLT node-set function working. xalan:nodeset() works fine but when I try exslt:node-set I get this error: (Location of error...
0
by: adamsbarker | last post by:
i can't seem to get the "ssh2_exec" function working on Windows XP. the example in the manual (which is obviously linux biased) says: ssh2_exec($connection, '/usr/local/bin/php -i'); i have...
1
by: Julie May | last post by:
I have 90% of my function working and I know to get the next 10% it is justa matter of getting the quotations and the escaping quotations correct. Here is the portion that does work: <working...
4
by: Olex Malko | last post by:
How to manage FindWindow() function working properly in ASP.NET application? In Windows Forms application it works fine. But in ASP.NET it returns NULL :( Probalby this is because of different...
1
by: tkkarthik | last post by:
Hello everyone.. Let me tell where i am in my programming.. I have 3 command buttons in my form. say a, b,c When I press the Key A in keyboard, I need the color of my command buton A to...
3
by: dkyadav80 | last post by:
Hi sir, I'have written small function in javascript for date (month and year), this is good working in IE at onclick event and current month-8 , year-2008 are displaying but in firefox3,opera,&...
4
johny10151981
by: johny10151981 | last post by:
/*name of the built program is dmns*/ int main() { int i, j; i=fork(); if(i<0) return 1; printf("created pid=%d",i); while(1) {i=i;} //unlimited loop
1
by: Carrie Lennie | last post by:
I am using floats in my webpage layout to position controls relative to each other. I have the controlNewLine (always present to force the clear), mandatory indicator, label, input, and validator of...
15
Seth Schrock
by: Seth Schrock | last post by:
I have a report that I'm trying to build as a receipt for my customers. The important field names are ItemPrice, SalesTax, and TotalPrice. I have three textboxes that will contain the total amount...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.