php loop within switch ??

Status
Not open for further replies.

thezone1

New Member
Messages
192
Reaction score
0
Points
0
hi im trying to get this script to work,

PHP:
<?php 
$view = ($_GET['view']);
$path = "html/";
$dir_handle = @opendir($path) or die ("ERROR");
 
    switch($view){
 
        //whileloop
          while ($file = readdir($dir_handle))
           {
             case:$file;
              include($file);
             break;
           }
             default:
              include("home.html");
             break;
     }
            
?>

Im not to new to php guys but i just cant get my head around this one ??
 
Last edited:

cowctcat

New Member
Messages
401
Reaction score
0
Points
0
Your switch is incomplete you nade to add in the case statements
 

thezone1

New Member
Messages
192
Reaction score
0
Points
0
I know but im trying to have the while loop add them for me ( i want it to list a case for each file found in a directory )
 

vTech

New Member
Messages
34
Reaction score
0
Points
0
have the address to it?maybe if u show us the page,we could find out what the outcome of the codes....
 

thezone1

New Member
Messages
192
Reaction score
0
Points
0
its on my local testing server, but the output is unexpected T_variable, expected T_CASE or T_default .......... so on
 

VPmase

New Member
Messages
914
Reaction score
1
Points
0
PHP:
<?php 
$view = ($_GET['view']);
$path = "html/";
$dir_handle = @opendir($path) or die ("ERROR");
 

        //whileloop
    while ($file = readdir($dir_handle))
           {
          switch($view){
 
             case:$file;
              include($file);
             break;
             default:
              include("home.html");
             break;
           }
     }
            
?>


All I did was switch the switch statement and while loop around. It should work like that.
 
Last edited:

vTech

New Member
Messages
34
Reaction score
0
Points
0
PHP:
<?php 
$view = ($_GET['view']);
$path = "html/";
$dir_handle = @opendir($path) or die ("ERROR");
 

        //whileloop
    while ($file = readdir($dir_handle))
           {
          switch($view){
 
             case:$file;
              include($file);
             break;
             default:
              include("home.html");
             break;
           }
     }
            
?>


All I did was switch the switch statement and while loop around. It should work like that.

that sounds more logical and less confusing for the server...hehe...
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
PHP:
 <?php  
$view = ($_GET['view']); 
$path = "html/"; 
$dir_handle = @opendir($path) or die ("ERROR"); 
  

        //whileloop 
    while ($file = readdir($dir_handle)) 
           { 
          switch($view){ 
  
             case:$file; 
              include($file); 
             break; 
             default: 
              include("home.html"); 
             break; 
           } 
     } 
             
?>

What do u pass in view means get variable...

I think pass variable is valid.
If in html folder any file then found and included.
 

LHVWB

New Member
Messages
1,308
Reaction score
0
Points
0
Here is an example of an easier way of doing the same thing using the scandir() function instead (php5 only, but x10 use php5 so it should work).

PHP:
<?php  
$view = ($_GET['view']); 
$path = "html/"; 
$include = "";

// Go through all of the files in the folder.
foreach(scandir($path) as $file)
{
    // Include the file only if it has been called be the $_GET['view'] variable.
    // Also make sure that we are not including more than one file.
    if(($view == $file) && !($include))
    {
         include($path.$file);
         $include = $file;
    }
}

// No current includes? Include the default page then.
if(!$include)
    include($path."index.html");
             
?>
 
Last edited:

woiwky

New Member
Messages
390
Reaction score
0
Points
0
If all files in the folder are accessible, which is what I believe since you have no conditions for excluding files in your loop, then it's probably better to just do something like this:

PHP:
<?php 
$path = 'html/';
$view = str_replace(array('\\', '/'), '', $_GET['view']);
include $path . ($view && file_exists($path.$view) ? $view : 'home.html');
?>
The string replacement just ensures that forward slashes and (if the code is ever used on a Windows server) backslashes were not placed in the string.
 
Status
Not open for further replies.
Top