Jump to content

[SOLVED] switch not working properly please help....


redarrow

Recommended Posts

Advance thank you...

 

switch not working on FALSE1

 

anybody.....

 

<?php

if(isset($_POST['submit'])){

$new_doc=trim($_POST['new_doc']);

$file_name=trim($_POST['file_name']);

$cheek=$_POST['cheek'];

$error=$_POST['error'];

if( empty($new_doc) && empty($file_name) ){

     $error='FALSE1';
}

$cheek=substr($file_name,-4);

if($cheek !='.doc')	{

$error='FALSE2';

}

$x=glob("*.*");

if(in_array($file_name,$x)){

$error='FALSE3';
}


$fh=@fopen($file_name, 'w+');

@fwrite($fh, $new_doc);

@fclose($fh);

}


switch($error){


CASE "FALSE1":

echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
<br><br>
Make sure you fill in all the form. 
<br><br> 
</center>";

BREAK;


CASE "FALSE2":

echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
<br><br> 
Make sure the name of the file is a .doc. 
<br><br> 
</center>";

BREAK;

CASE "FALSE3":

echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
<br><br> 
Sorry we have that file name!. 
<br><br> 
</center>";

BREAK;
}
?>

<center> 

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">

<br> <br>

Please name the file .doc

<br> <br>

<input type="text" name="file_name">

<br> <br>

Please write your text

<br> <br>

<textarea name="new_doc" cols="30" rows="20"> </textarea>

<br> <br>
<input type="submit" name="submit" value="Export to Word">

<br><br>

</form> 

</center>

Link to comment
Share on other sites

code all works except the first swich condition that it........

 

<?php

if(isset($_POST['submit'])){

$new_doc=trim($_POST['new_doc']);

$file_name=strtolower(strtoupper(trim($_POST['file_name'])));

$cheek=$_POST['cheek'];

$error=$_POST['error'];

if( empty($new_doc) && empty($file_name) ){

     $error='FALSE';
}

$cheek=substr($file_name,-4);

if($cheek !='.doc')	{


$error='FALSE2';

}

$x=glob("*.*");

if(in_array($file_name,$x)){

$error='FALSE3';
}

if(!$error){

$fh=@fopen($file_name, 'w+');

@fwrite($fh, $new_doc);

@fclose($fh);


echo "<center>THANK YOU FILE CREATED AS A WORD DOCUMENT!</center>";
}
}

switch($error){


CASE "FALSE":

echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
<br><br>
Make sure you fill in all the form. 
<br><br> 
</center>";

BREAK;


CASE "FALSE2":

echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
<br><br> 
Make sure the name of the file is a .doc. 
<br><br> 
</center>";

BREAK;

CASE "FALSE3":

echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
<br><br> 
Sorry we have that file name!. 
<br><br> 
</center>";

BREAK;
}
?>

<center> 

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">

<br> <br>

Please name the file .doc

<br> <br>

<input type="text" name="file_name">

<br> <br>

Please write your text

<br> <br>

<textarea name="new_doc" cols="30" rows="20"> </textarea>

<br> <br>
<input type="submit" name="submit" value="Export to Word">

<br><br>

</form> 

</center>

 

Link to comment
Share on other sites

LoL how about instead of agreeing and saying your sorry you actually tell us what the problem is? Is it not working because none of the cases are being triggered (nothing being echoed from any of them)?  Is it not working because the wrong one is being triggered?  Some kind of syntax error? Any error messages being displayed at all?  Come on man...

Link to comment
Share on other sites

I think your problem may be that you have an extra "}" before the switch statement. If you would indent properly, things like this can be avoided.

 

I took the liberty of re-writing your code to get rid of the switch statement. My code uses arrays to hold the error messages and implode() to output them.

 

<?php
$err_msgs = array('Make sure you fill in all the form.','Make sure the name of the file is a .doc.','Sorry we have that file name!.');
$error = array();
if(isset($_POST['submit'])){
$new_doc=trim($_POST['new_doc']);
$file_name=strtolower(strtoupper(trim($_POST['file_name'])));
$cheek=$_POST['cheek'];
$error=$_POST['error'];
if( empty($new_doc) && empty($file_name) ){
     $error[] = $err_msgs[0];
}
$cheek=substr($file_name,-4);
if($cheek !='.doc')	{
	$error[] = $err_msgs[1];
}
$x=glob("*.*");
if(in_array($file_name,$x)){
	$error[] = $err_msgs[2];
}
if(empty($error)){
	$fh=fopen($file_name, 'w+');
	fwrite($fh, $new_doc);
	fclose($fh);
	echo "<center>THANK YOU FILE CREATED AS A WORD DOCUMENT!</center>";
} else {
	$tmp = array();
	$tmp[] = '<span style="text-align:center">Please try again, there was a problem:<br><br>';
	$tmp[] = implode("<br>\n",$error) . '<br><br></span>';
	echo implode("\n",$tmp);
}
}
?>

 

Ken

Link to comment
Share on other sites

Nice work, But your code dosent order the errors correctly aswell,

 

if you post the form with no values brings the error message of

two arrays,

 

error[0] and error[1]

 

but only error[0] should be shown when there no values in the form..

 

i got the same problam with my oregianal code useing a switch.....

 

any idears.....

Link to comment
Share on other sites

 

WAS ALL FOR FUN AND LEARNING PURPOSE.....

 

SOLVED my code switch way to make a .word doc........

<?php

if(isset($_POST['submit'])){
   
$new_doc=trim($_POST['new_doc']);

$file_name=strtolower(strtoupper(trim($_POST['file_name'])));

$cheek=$_POST['cheek'];

$error=$_POST['error'];

if(empty($_POST['new_doc']) || empty($_POST['file_name'])){
   
     $error='FALSE';
}else{

$cheek=substr($file_name,-4);

if($cheek !='.doc')   {
   

   $error='FALSE2';

}
}
$x=glob("*.*");

if(in_array($file_name,$x)){
   
   $error='FALSE3';
}

if(!$error){

$fh=@fopen($file_name, 'w+');

@fwrite($fh, $new_doc);

@fclose($fh);


echo "<center>THANK YOU FILE CREATED AS A WORD DOCUMENT!</center>";
}
  }

switch($error){
   
   
CASE "FALSE":
   
   echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
   <br><br>
   Make sure you fill in all the form. 
   <br><br> 
   </center>";

BREAK;


CASE "FALSE2":
   
   echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
   <br><br> 
   Make sure the name of the file is a .doc. 
   <br><br> 
   </center>";
   
   BREAK;
   
CASE "FALSE3":
   
   echo "<center>PLEASE TRY AGIN THERE WAS A PROBLAM! 
   <br><br> 
   Sorry we have that file name!. 
   <br><br> 
   </center>";
   
   BREAK;
}
?>

<center>

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">

<br> <br>

Please name the file .doc

<br> <br>

<input type="text" name="file_name">

<br> <br>

Please write your text

<br> <br>

<textarea name="new_doc" cols="30" rows="20"> </textarea>

<br> <br>
<input type="submit" name="submit" value="Export to Word">

<br><br>

</form>

</center>

 

 

array way solved aswell to make a word.doc

<?php
$err_msgs = array('Make sure you fill in all the form.','Make sure the name of the file is a .doc.','Sorry we have that file name!.');

$error = array();

if(isset($_POST['submit'])){

$new_doc=trim($_POST['new_doc']);
    
$file_name=strtolower(strtoupper(trim($_POST['file_name'])));
    
$cheek=$_POST['cheek'];
    
$error=$_POST['error'];
   
    if( empty($new_doc) && empty($file_name) ){
        $error[] = $err_msgs[0];
   
    }else{
   
   $cheek=substr($file_name,-4);
   
   if($cheek !='.doc')   {

   	$error[] = $err_msgs[1];
   }
   }
   $x=glob("*.*");
   
   if(in_array($file_name,$x)){

   	$error[] = $err_msgs[2];
   }
   
   if(empty($error)){
      
   	$fh=fopen($file_name, 'w+');

   	fwrite($fh, $new_doc);

   	fclose($fh);
      
      echo "<center>THANK YOU FILE CREATED AS A WORD DOCUMENT!</center>";
   
   } else {

   	  $tmp = array();
      $tmp[] = '<span style="text-align:center">Please try again, there was a problem:<br><br>';
      $tmp[] = implode("<br>\n",$error) . '<br><br></span>';
      echo implode("\n",$tmp);
   }
}
?>



<center>

<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">

<br> <br>

Please name the file .doc

<br> <br>

<input type="text" name="file_name">

<br> <br>

Please write your text

<br> <br>

<textarea name="new_doc" cols="30" rows="20"> </textarea>

<br> <br>
<input type="submit" name="submit" value="Export to Word">

<br><br>

</form>

</center>

Link to comment
Share on other sites

little add on, to see the files .doc ur creating, and see what in the current files....

 

add it as is under the form.......

<?php

echo"<center>	CURRENT FILES, PLEASE PRESS LINK TO SEE WHAT WRITTEN IN THEM!<BR><BR>";
$show=glob("*.doc*");

foreach($show as $show_files){

echo"<a href=' ".$_SERVER['PHP_SELF']."?cmd=$show_files'>$show_files</a><br>";


if($_GET['cmd']=="$show_files"){


$myFile = $show_files;

$fh = fopen($myFile, 'r');

$theData = fread($fh, filesize($myFile));

fclose($fh);

echo " <br>$myFile<br> $theData<br><br>";
}
}
?>

 

 

Link to comment
Share on other sites

How can i get the file to open in word on the fly with no download....

 

if the user press the relevent file if makes word open it ..

 

 

I tried this with no joy........

<?php

echo"<center>OPEN UP YOUR WORD DOC IN MS WORD NOW!</center>!<BR><BR>";

$show=glob("*.doc*");

foreach($show as $show_files){

echo"<a href=' ".$_SERVER['PHP_SELF']."?cmd=$show_files'>$show_files</a><br>";


if($_GET['cmd']=="$show_files"){
   

header("Content-Type: application/msword; charset=ISO-8859-1");

header("Content-Disposition: attachment; filename=\"$show_file\"" ); 

}
}
?>

Link to comment
Share on other sites

this should work dosent..

 

error

 

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Invalid syntax ' in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\simple_forum\word4.php:2 Stack trace: #0 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\simple_forum\word4.php(2): com->com('word.applicatio...') #1 {main} thrown in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\sim\word4.php on line 2

 

<?php
$word = new COM("word.application") or die ("Could not initialise MS Word object.");

echo"<center>OPEN UP YOUR WORD DOC IN MS WORD NOW!</center>!<BR><BR>";

$show=glob("*.doc*");

foreach($show as $show_files){

echo"<a href=' ".$_SERVER['PHP_SELF']."?cmd=$show_files'>$show_files</a><br>";


if($_GET['cmd']=="$show_files"){


$word->Documents->Open(realpath("".$_SERVER['DOCUMENT_ROOT']."?$show_files"));

echo $word;

}
}

// Extract content.
$content = (string) $word->ActiveDocument->Content;

echo $content;

$word->ActiveDocument->Close(false);

$word->Quit();

$word = null;

unset($word);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.