Current location: Hot Scripts Forums » Programming Languages » PHP » Closing nested statements


Closing nested statements

Reply
  #1 (permalink)  
Old 07-25-07, 11:09 PM
Andy128 Andy128 is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Closing nested statements

I have had several people help me with coding and I understand it a fair bit. However, I cannot seem to wrap my brain around the logic behind closing multiple statements. For example:
PHP Code:

<?php

//Now open file
if(!file_exists("test_form_db.txt"))
{                 
//A bracket
echo"<table width=550 align=center>";
echo
"<tr>";
echo
"<td class='th tc ol fw fc'>NO RECORDS AT THIS TIME</td>";
echo
"</tr>";
echo
"<tr>";
echo
"<td class='th tc ol fw fc'>CHECK BACK LATER</td>";
echo
"</tr>";
echo
"</table>";
exit;
}                  
//THIS CLOSES A
else
{                 
//B bracket
echo"<table width=550 align=center>";
echo
"<tr>";
echo
"<th class='th tc ol fw fc'>Test Form Page</th>";
echo
"</tr>";
//
$openedfile fopen"test_form_db.txt"'r' );
if(
$openedfile)
{                         
//D bracket
while (!feof$openedfile ) )
    {                     
//E bracket
        
$line trim(fgets$openedfile ));
        if ( !empty( 
$line ) )
        {                
//F bracket
            
list( $test1,$test2,$test3$test4$test5) = explode"|"$line );
/////////////////////////////////////////////////////////////////////////////////
//Display in a table
//
echo"<tr>";
echo
"<td class='td tc w2 fw ol ff'>$test1</td>";
echo
"</tr>";
echo
"<tr>";
echo
"<td class='td tc w2'>$test2</td>";
echo
"</tr>";
echo
"<tr>";
echo
"<td class='td tc w2 fw fc'>$test3</td>";
echo
"</tr>";
echo
"<tr>";
echo
"<td class='td tc w2'>$test4</td>";
echo
"</tr>";
echo
"<tr>";
echo
"<td class='td tc w2'>$test5</td>";
echo
"</tr>";
}
}
fclose$openedfile );
}                     
//THIS CLOSES B bracket
else
{                     
//C bracket 
echo"<tr>";
echo
"<td class='th w2'>Cannot open file!</td>";
echo
"</tr>";
echo
"</table>";
}                    
//THIS CLOSES C bracket
}
?>
Which ones close D,E,F?

Any helpful lessons appreciated.

Andy

Last edited by Andy128; 07-25-07 at 11:13 PM.
Reply With Quote
  #2 (permalink)  
Old 07-26-07, 02:17 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
My advice: Write your code cleaner, and you won't need comments to know which bracket closes which statement.

Personally, I indent all code inside a statement by one, like this:
PHP Code:

<?php

//Now open file
if (!file_exists("test_form_db.txt"))
{                 
//A bracket
    
echo"<table width=550 align=center>";
    echo
"<tr>";
    echo
"<td class='th tc ol fw fc'>NO RECORDS AT THIS TIME</td>";
    echo
"</tr>";
    echo
"<tr>";
    echo
"<td class='th tc ol fw fc'>CHECK BACK LATER</td>";
    echo
"</tr>";
    echo
"</table>";
    exit;
}                  
//THIS CLOSES A
else
{                 
//B bracket
    
echo"<table width=550 align=center>";
    echo
"<tr>";
    echo
"<th class='th tc ol fw fc'>Test Form Page</th>";
    echo
"</tr>";
    
//
    
$openedfile fopen"test_form_db.txt"'r' );
    
    if(
$openedfile)
    {                         
//D bracket
        
while (!feof$openedfile ) )
        {                     
//E bracket
            
$line trim(fgets$openedfile ));
            
            if ( !empty( 
$line ) )
            {                
//F bracket
                
list( $test1,$test2,$test3$test4$test5) = explode"|"$line );
                
/////////////////////////////////////////////////////////////////////////////////
                //Display in a table
                //
                
echo"<tr>";
                echo
"<td class='td tc w2 fw ol ff'>$test1</td>";
                echo
"</tr>";
                echo
"<tr>";
                echo
"<td class='td tc w2'>$test2</td>";
                echo
"</tr>";
                echo
"<tr>";
                echo
"<td class='td tc w2 fw fc'>$test3</td>";
                echo
"</tr>";
                echo
"<tr>";
                echo
"<td class='td tc w2'>$test4</td>";
                echo
"</tr>";
                echo
"<tr>";
                echo
"<td class='td tc w2'>$test5</td>";
                echo
"</tr>";
            }
        }
        
        
fclose$openedfile );
    }                     
//THIS CLOSES B bracket
    
else
    {                     
//C bracket 
        
echo"<tr>";
        echo
"<td class='th w2'>Cannot open file!</td>";
        echo
"</tr>";
        echo
"</table>";
    }                    
//THIS CLOSES C bracket
}
?>
It's much easier to read and follow.
Reply With Quote
  #3 (permalink)  
Old 07-26-07, 08:31 AM
Andy128 Andy128 is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Thaks Nico-

I appreciate the tip. How ever, the logic behind the closing is what escapes me. For instance. The "D" bracket is the very last one closed before the closing tag while "E" and "F" were closed directly following the table and before the closing of the file. Is there a rule to this that I am missing or simply the logic itself.

And by cleaner, I take it that you simply meant the indentations not the logic or syntax it self of the script presented.

Andy
Reply With Quote
  #4 (permalink)  
Old 07-26-07, 10:32 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Andy128 View Post
Thaks Nico-

I appreciate the tip. How ever, the logic behind the closing is what escapes me. For instance. The "D" bracket is the very last one closed before the closing tag while "E" and "F" were closed directly following the table and before the closing of the file. Is there a rule to this that I am missing or simply the logic itself.
This is because "bracket D" is your loop, and you need to have things happen in your loop. E and F happen in your loop and will need to be closed inside of D because you started them in D, understand?
Quote:
And by cleaner, I take it that you simply meant the indentations not the logic or syntax it self of the script presented.
I'm not going to comment on the logic, but the syntax could use some work, try making your code so a majority of it is run before any HTML is output to the web browser. This can be done in your case by putting all of that echoed html into 1 string var, then echoing that string 1 time. instead of calling echo X number of times.
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #5 (permalink)  
Old 07-26-07, 12:33 PM
Andy128 Andy128 is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Nova-
Quote:
try making your code so a majority of it is run before any HTML is output to the web browser. This can be done in your case by putting all of that echoed html into 1 string var, then echoing that string 1 time. instead of calling echo X number of times.
If you have the time, could you provide a small snippet of example on this?

Thanks again-

Andy
Reply With Quote
  #6 (permalink)  
Old 07-26-07, 12:59 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?php
$table 
'';
// Start to build table
$table .= '<table>';
for(
$i=0;$i<10;$i++)
    {
    
$table .='<tr>';
    for(
$j=0;$j<10;$j++)
        {
        
$table .='<td>';
        
$table .= 'Content for this cell of this row';
        
$table .='</td>';
        }
    
$table .='</tr>';
    }
$table .= '</table>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>pre-php</title>
    </head>

    <body>
        <p><?php echo $table?></p>
    </body>

</html>
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #7 (permalink)  
Old 07-26-07, 01:07 PM
Andy128 Andy128 is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Nova- I will sink my teeth into this straight away. Why is it that these cool examples are not covered in most books?

Any reference material you would suggest to pick up the tricks of the trade?

Andy
Reply With Quote
  #8 (permalink)  
Old 07-26-07, 02:02 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Best book I ever read on PHP, covers it all.

http://www.amazon.com/PHP-MySQL-Deve...5476502&sr=8-1
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
nested loops.. need some help with printing asterisks please.. zayin Everything Java 4 04-27-10 12:53 AM
nested query help adubb Database 1 07-08-07 08:46 PM
Closing an application Steelman Visual Basic 3 04-24-06 08:56 AM
Nested Query in PHP/MYSQL truesilentassassin PHP 2 07-27-04 07:50 PM
Socket closing after long periods of time 2uantuM Everything Java 2 05-05-04 04:56 PM


All times are GMT -5. The time now is 06:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.