4 Replies - 221 Views - Last Post: 06 September 2012 - 03:02 PM Rate Topic: -----

#1 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 210
  • Joined: 28-March 12

Question: Unexpected T_Else......New set of eyes!

Posted 06 September 2012 - 11:10 AM

Hey guys! I am having a bit of a problem! I am currently debugging my code and I ran into Parse error: syntax error, unexpected T_ELSE on line 4 ............I have tried taking away the beginning curly brace.....I have tried } ELSE { on the same line....and I don't know what else to try in order to get my code to work! I think that maybe an extra set of eyes can spot what I am doing wrong. Any input would be greatly appreciated!! Here is my code...

//connection closing
                mysql_close($xxxxx);
        } 
            else {
            echo $fname. ' ('. $fsize. ' kb) was successfully uploaded';
          }
        }
        else {
          echo 'The file '. $fname. ' exceeds the maximum permitted size, '. $max_size. ' KB';
        }
      }
      else {
        echo $fname. ' - invalid file type';
        }
      }
    }
  }
}
else echo 'Invalid form data';
?>

This post has been edited by BarNunBoi: 06 September 2012 - 11:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question: Unexpected T_Else......New set of eyes!

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,573
  • Joined: 08-June 10

Re: Question: Unexpected T_Else......New set of eyes!

Posted 06 September 2012 - 11:31 AM

An else clause must follow an if or an else if block. Make sure all your brackets line up properly and then make sure that is the case.

If you can't spot it, post the whole code here. We can't really see the cause of the error without seeing the whole structure leading up to it.
Was This Post Helpful? 0
  • +
  • -

#3 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 210
  • Joined: 28-March 12

Re: Question: Unexpected T_Else......New set of eyes!

Posted 06 September 2012 - 11:37 AM

@ATLI thanks man!! I don't have an if statement before my else clause! SHAME ON ME!!! SO SIMPLE!!! I am going back to the drawing board and tweak my code! Thanks Again!

This post has been edited by BarNunBoi: 06 September 2012 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 210
  • Joined: 28-March 12

Re: Question: Unexpected T_Else......New set of eyes!

Posted 06 September 2012 - 02:06 PM

I have been playing with my code and I cant seem to pinpoint whats going on.I am still getting the unexpected T_ELSE error on line 71. Whats am I doing wrong or not doing at all?

<?php
function processSpreadsheet() {
$updir = 'uploads/';       // sets the folder where the uploaded files are copied
$max_size = 500;         // sets maximum file size allowed (in KB)
$allowtype = array('xls');  // file types allowed

// Declares PHPExcelReader Function
function phpexcelreader() {
     

require_once 'PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load('.');
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // 
    $highestColumn      = $worksheet->getHighestColumn(); // 
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo "<br>The worksheet ".$worksheetTitle." has ";
    echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
    echo ' and ' . $highestRow . ' row.';
    echo '<br>Data: <table border="1"><tr>';
    for ($row = 1; $row <= $highestRow; ++ $row) {
        echo '<tr>';
        for ($col = 0; $col < $highestColumnIndex; ++ $col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getValue();
            $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
            echo '<td>' . $val . '<br></td>';
          }
           echo '</tr>';
        }
      echo '</table>';
    
}


// Check if file that is received a valid file from the 'Coverage' form field
if (isset($_FILES['Coverage'])) {
  // check for errors
  if ($_FILES['Coverage']['error'] > 0) {
    echo 'Error: '. $_FILES['Coverage']['error']. '<br />';
  }
  else {
    // get the name, size (in kb) and type (the extension) of the file
    $fname = $_FILES['Coverage']['name'];
    $fsize = $_FILES['Coverage']['size'] / 1024;
    $ftype = end(explode('.', strtolower($fname)));

    // checks if the file already exists
    if (file_exists($updir. $fname)) {
      echo 'The file: '. $fname. ' already exists';
  }
    else {
      // if the file does not exist, check its type (by extension) and size-
      if (in_array($ftype, $allowtype)) {
        echo 'Please Upload The Correct File!';
        // check the size
      if (array("$fsize" => "$max_size")) {
        echo 'Please Upload Smaller File!'; 

         } 
            else {
            echo $fname. ' ('. $fsize. ' kb) was successfully uploaded';
          }
        }
        else {
          echo 'The file '. $fname. ' exceeds the maximum permitted size, '. $max_size. ' KB';
        }
      }
      else {
        echo $fname. ' - invalid file type';
        }
      }
    }
  }
}
else echo 'Invalid form data';
        

This post has been edited by BarNunBoi: 06 September 2012 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3052
  • View blog
  • Posts: 4,573
  • Joined: 08-June 10

Re: Question: Unexpected T_Else......New set of eyes!

Posted 06 September 2012 - 03:02 PM

The main problem with that code is the lack of coherent indentation. It's hiding what would otherwise be an obvious error. In your code, the else on line #71 appears to be closing the if on line #59. But if you look in between those two lines, you'll find two other else commands, but only one other if command. Which means that the else on line #71 doesn't belong to any if command.

See what happens if I apply a proper style to your code:
<?php

function processSpreadsheet()
{
	$updir = 'uploads/'; // sets the folder where the uploaded files are copied
	$max_size = 500;   // sets maximum file size allowed (in KB)
	$allowtype = array('xls');  // file types allowed
	// Declares PHPExcelReader Function

	function phpexcelreader()
	{
		require_once 'PHPExcel/IOFactory.php';
		$objPHPExcel = PHPExcel_IOFactory::load('.');
		foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
			$worksheetTitle = $worksheet->getTitle();
			$highestRow = $worksheet->getHighestRow(); // 
			$highestColumn = $worksheet->getHighestColumn(); // 
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
			$nrColumns = ord($highestColumn) - 64;
			echo "<br>The worksheet " . $worksheetTitle . " has ";
			echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
			echo ' and ' . $highestRow . ' row.';
			echo '<br>Data: <table border="1"><tr>';
			for ($row = 1; $row <= $highestRow; ++$row) {
				echo '<tr>';
				for ($col = 0; $col < $highestColumnIndex; ++$col) {
					$cell = $worksheet->getCellByColumnAndRow($col, $row);
					$val = $cell->getValue();
					$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
					echo '<td>' . $val . '<br></td>';
				}
				echo '</tr>';
			}
			echo '</table>';
		}


		// Check if file that is received a valid file from the 'Coverage' form field
		if (isset($_FILES['Coverage'])) {
			// check for errors
			if ($_FILES['Coverage']['error'] > 0) {
				echo 'Error: ' . $_FILES['Coverage']['error'] . '<br />';
			}
			else {
				// get the name, size (in kb) and type (the extension) of the file
				$fname = $_FILES['Coverage']['name'];
				$fsize = $_FILES['Coverage']['size'] / 1024;
				$ftype = end(explode('.', strtolower($fname)));

				// checks if the file already exists
				if (file_exists($updir . $fname)) {
					echo 'The file: ' . $fname . ' already exists';
				}
				else {
					// if the file does not exist, check its type (by extension) and size-
					if (in_array($ftype, $allowtype)) {
						echo 'Please Upload The Correct File!';
						// check the size
						if (array("$fsize" => "$max_size")) {
							echo 'Please Upload Smaller File!';
						}
						else {
							echo $fname . ' (' . $fsize . ' kb) was successfully uploaded';
						}
					}
					else {
						echo 'The file ' . $fname . ' exceeds the maximum permitted size, ' . $max_size . ' KB';
					}
				}
				else {
					echo $fname . ' - invalid file type';
				}
			}
		}
	}
}
else {
	echo 'Invalid form data';
}


Now it's obvious that you've got one else clause following directly after another. Also, this reveals another similar error: the last else clause in the code is following a function declaration.


On more thing to note there is the second function declaration. Did you mean for it to be inside the first function? Defined like this the second function won't exist until the first function is called, and you will not be able to call the first function more than once without getting an error when it tries to re-declare the second function.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1