index.php

Go to the documentation of this file.
00001 <?php 
00015 define('EXAMPLES_DIR','/home/saurabh/Desktop/courses/2011/monsoon/scripting_and_computer_environment/lectures/source_code/11-php/11-php-examples');
00016 
00017 
00021 define('BASE_URL','http://10.5.1.222/php/');
00022 
00023 
00024 
00031 function read_example_dir()
00032 {
00033     global $example_array;
00034 
00035     //Opening example dir to read its contents
00036     $example_dir = opendir(EXAMPLES_DIR);
00037     if($example_dir == false)
00038     {
00039         die('Cannot open dir ' . EXAMPLES_DIR . ' for reading');
00040     }
00041 
00042     $example_count=0;
00043 
00044     //looping through all example dir contents ignoring ., .. and files
00045     $current_file=readdir($example_dir);
00046     while($current_file != false)
00047     {
00048         if($current_file != '.' && $current_file != '..' && is_dir($current_file))
00049         {
00050             $example_array[$example_count]=$current_file;
00051             $example_count++;
00052         }
00053         $current_file=readdir($example_dir);
00054     }
00055 
00056     //It is important to close dir opened using opendir().
00057     //Such habits help in minimizing RAM usage while script is running
00058     closedir($example_dir);
00059 
00060     //Sorting folders alphabetically using PHP sort() function
00061     if($example_count > 1)
00062         sort($example_array, SORT_STRING);
00063 }
00064 
00065 
00066 
00073 function display_index_page()
00074 {
00075     global $example_array;
00076 
00077     //Reading example dir and sorting all dir entries alphabetically
00078     //in global array $example_array
00079     read_example_dir();
00080 
00081     ?>
00082     <html>
00083         <head>
00084             <title>PHP examples</title>
00085         </head>
00086         <body>
00087             <h3>PHP examples:</h3>
00088             <ul>
00089                 <?php
00090                     //Although count is already available in $example_count we are still
00091                     //using count function just to demonstrate how to find number of
00092                     //elements in array.
00093                     $limit=count($example_array);
00094 
00095                     //Displaying link for each folder stored in example array               
00096                     for($counter1=0; $counter1<$limit; $counter1++)
00097                     {
00098                         echo '<li><a href="' . BASE_URL . $example_array[$counter1] . '">' . 
00099                                 $example_array[$counter1] . '</a></li>' . "\n";
00100                     }
00101                 ?>
00102             </ul>
00103         </body>
00104     </html>
00105     <?php
00106 }
00107 
00108 
00109 
00115 function main()
00116 {
00117     //Calling function display_index_page which displays index page
00118     display_index_page();
00119 }
00120 
00121 
00122 
00123 //Calling main() to execute functionality desired from this program.
00124 main();
00125 ?>

Generated on Fri Nov 4 14:16:54 2011 for PHP example documentation by  doxygen 1.4.7