PHP Syntax Error..Simple but I cannot find it

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 647 Views - Last Post: 29 October 2011 - 10:47 AM Rate Topic: -----

#1 tdeprato  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 11

PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 07:01 AM

I am getting the following error --

"T_ELSE in scorm_get_tracks.php on line 47"

In the code below. I have re-written the if-else a few times. This is a patch to a function for the Moodle platform and maybe I am just blind but I cannot find the solution it must be simple....


<?php
function scorm_get_tracks($scoid,$userid,$attempt='') {
/// Gets all tracks of specified sco and user
global $CFG;

if (empty($attempt)) {
if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) { $attempt = scorm_get_last_attempt($scormid,$userid); } else { $attempt = 1; }
}
$attemptsql = ' AND attempt=' . $attempt;
if ($tracks = get_records_select('scorm_scoes_track',"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
$usertrack->userid = $userid;
$usertrack->scoid = $scoid; 
// Defined in order to unify scorm1.2 and scorm2004
$usertrack->score_raw = '';
$usertrack->status = '';
$usertrack->total_time = '00:00:00';
$usertrack->session_time = '00:00:00';
$usertrack->timemodified = 0;
$usertrack->firsttimemodified = time(); // josef
foreach ($tracks as $track) {
$element = $track->element;
$usertrack->{$element} = $track->value;
switch ($element) {
case 'cmi.core.lesson_status':
case 'cmi.completion_status':
if ($track->value == 'not attempted') { $track->value = 'notattempted'; } 
$usertrack->status = $track->value;
break; 
case 'cmi.core.score.raw':
case 'cmi.score.raw':
$usertrack->score_raw = $track->value;
break; 
case 'cmi.core.session_time':
case 'cmi.session_time':
$usertrack->session_time = $track->value;
break; 
case 'cmi.core.total_time':
case 'cmi.total_time':
$usertrack->total_time = $track->value;
break; 
} 
if (isset($track->timemodified) && ($track->timemodified > $usertrack->timemodified)) { $usertrack->timemodified = $track->timemodified; } 
if (isset($track->timemodified) && ($track->timemodified < $usertrack->firsttimemodified)) {	// josef $usertrack->firsttimemodified = $track->timemodified;	 // josef } // josef
}
if (is_array($usertrack)) { ksort($usertrack); }
return $usertrack;
} else { return false; }
}
?>






Is This A Good Question/Topic? 0
  • +

Replies To: PHP Syntax Error..Simple but I cannot find it

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 07:18 AM

Perhaps if you had some sanity in your code indentation and formatting, it might be obvious! What the hell are you using to edit your code? Here's what it SHOULD look like (at least one minor variation):

<?php
function scorm_get_tracks($scoid,$userid,$attempt='') {
	/// Gets all tracks of specified sco and user
	global $CFG;

	if (empty($attempt)) {
		if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) { 
			$attempt = scorm_get_last_attempt($scormid,$userid); 
		} else { 
			$attempt = 1; 
		}
	}
	$attemptsql = ' AND attempt=' . $attempt;
	if ($tracks = get_records_select('scorm_scoes_track',
		"userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {
		$usertrack->userid = $userid;
		$usertrack->scoid = $scoid; 
		// Defined in order to unify scorm1.2 and scorm2004
		$usertrack->score_raw = '';
		$usertrack->status = '';
		$usertrack->total_time = '00:00:00';
		$usertrack->session_time = '00:00:00';
		$usertrack->timemodified = 0;
		$usertrack->firsttimemodified = time(); // josef
		foreach ($tracks as $track) {
			$element = $track->element;
			$usertrack->{$element} = $track->value;
			switch ($element) {
				case 'cmi.core.lesson_status':
				case 'cmi.completion_status':
					if ($track->value == 'not attempted') { 
						$track->value = 'notattempted'; 
					} 
					$usertrack->status = $track->value;
					break; 
				case 'cmi.core.score.raw':
				case 'cmi.score.raw':
					$usertrack->score_raw = $track->value;
					break; 
				case 'cmi.core.session_time':
				case 'cmi.session_time':
					$usertrack->session_time = $track->value;
					break; 
				case 'cmi.core.total_time':
				case 'cmi.total_time':
					$usertrack->total_time = $track->value;
					break; 
			} 
			if (isset($track->timemodified) && 
				($track->timemodified > $usertrack->timemodified)) { 
				$usertrack->timemodified = $track->timemodified; 
			} 
			if (isset($track->timemodified) && 
				($track->timemodified < $usertrack->firsttimemodified)) {
					// josef $usertrack->firsttimemodified = $track->timemodified;	 // josef } // josef
			}
			if (is_array($usertrack)) { 
				ksort($usertrack); 
			}
			return $usertrack;
		} else { 
			return false; 
		}
	}
?>


Was This Post Helpful? 1
  • +
  • -

#3 tdeprato  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 11

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 07:37 AM

I use Eclipse but I was working in GEDIT when I decided to paste the code in sorry. BUT
I still get the same error. I just used Eclipse and Jedit to check the bracket count. Still nothing.

Thanks for the feedback.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 07:40 AM

I didn't give you the answer, I formatted the code.

Check your brackets, look at the indentation. What bracket does the last } else { match up with?
Was This Post Helpful? 0
  • +
  • -

#5 tdeprato  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 11

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 07:57 AM

} else {


the { --- closes the for-each at the top

the { opens the else.

I did two brackets checks already with it is when I do php -l I get the syntax report.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 08:53 AM

Is there such a thing as a foreach-else loop?
Was This Post Helpful? 0
  • +
  • -

#7 tdeprato  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 11

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 08:56 AM

Yeah ForEach

View PostJackOfAllTrades, on 29 October 2011 - 08:53 AM, said:

Is there such a thing as a foreach-else loop?

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 09:40 AM

Dude...else matches up with ONE THING...if. Are you not getting that?
Was This Post Helpful? 0
  • +
  • -

#9 tdeprato  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 11

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 09:46 AM

I have worked it 3-5 different ways, this code is in a large repository for Moodle developers. It is for an older version of the platform. It, at one time, was deployed in probably 10000 installations world wide. Anyway thanks I will ask others...Have you ever used a FOREACH loop before?

View PostJackOfAllTrades, on 29 October 2011 - 09:40 AM, said:

Dude...else matches up with ONE THING...if. Are you not getting that?

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 10:03 AM

View Posttdeprato, on 29 October 2011 - 12:46 PM, said:

Have you ever used a FOREACH loop before?

All the time. How does that relate to your not knowing how to close braces?
Was This Post Helpful? 0
  • +
  • -

#11 tdeprato  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 11

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 10:11 AM

What are you talking about?

I closed the BRACES. I ran the code through a syntax checking/bracket counting program.

I can run individual pieces of it. I am obviously missing something and cannot find it. So if you can solve it, please help. If not then I will sort it out later.

I have written it 5 different ways, this is the original code that is why I posted it, since it was the piece of code used most often in the repository.

That is why I asked for HELP in figuring out a simple and obvious problem..which is must be.

If you do not want to help PLEASE do not keep replying.

View PostCTphpnwb, on 29 October 2011 - 10:03 AM, said:

View Posttdeprato, on 29 October 2011 - 12:46 PM, said:

Have you ever used a FOREACH loop before?

All the time. How does that relate to your not knowing how to close braces?

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 10:16 AM

Oh, sorry. I saw the missing brace and thought that was your only problem. As Jack has already pointed out, you can't put an 'else' with anything but an 'if'. Without giving you line numbers I don't see how it could be any clearer.
Was This Post Helpful? 0
  • +
  • -

#13 Boy67  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 48
  • Joined: 28-August 08

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 10:17 AM

ELSE can only be used with a IF statement like JackOfAllTrades said.

No-one can really go into much more depth to explain this. lol.
Was This Post Helpful? 0
  • +
  • -

#14 tdeprato  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-October 11

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 10:23 AM

Here is a version..notice the IF and ELSE are connected....

error still - unexpected T_ELSE

I am aware there is a structural mistake..can you find it? I can paste all the versions where the if-else are re-written etc...but I still get the same error. And the foreach seems closed ... and I cannot find any lines that have been incorrectly terminated.


<?php
function scorm_get_tracks($scoid,$userid,$attempt='') {

    /// Gets all tracks of specified sco and user

    global $CFG;

 
    if (empty($attempt)) {

        if ($scormid = get_field('scorm_scoes','scorm','id',$scoid)) {

            $attempt = scorm_get_last_attempt($scormid,$userid);

        } else {

            $attempt = 1;

        }

    }

    $attemptsql = ' AND attempt=' . $attempt;

    if ($tracks = get_records_select('scorm_scoes_track',

        "userid=$userid AND scoid=$scoid".$attemptsql,'element ASC')) {

        $usertrack->userid = $userid;

        $usertrack->scoid = $scoid;

        // Defined in order to unify scorm1.2 and scorm2004

        $usertrack->score_raw = '';

        $usertrack->status = '';

        $usertrack->total_time = '00:00:00';

        $usertrack->session_time = '00:00:00';

        $usertrack->timemodified = 0;

        $usertrack->firsttimemodified = time(); // josef

        foreach ($tracks as $track) {

            $element = $track->element;

            $usertrack->{$element} = $track->value;

            switch ($element) {

                case 'cmi.core.lesson_status':

                case 'cmi.completion_status':

                    if ($track->value == 'not attempted') {

                        $track->value = 'notattempted';

                    }

                    $usertrack->status = $track->value;

                    break;

                case 'cmi.core.score.raw':

                case 'cmi.score.raw':

                    $usertrack->score_raw = $track->value;

                    break;

                case 'cmi.core.session_time':

                case 'cmi.session_time':

                    $usertrack->session_time = $track->value;

                    break;

                case 'cmi.core.total_time':

                case 'cmi.total_time':

                    $usertrack->total_time = $track->value;

                    break;

            }

            if (isset($track->timemodified) &&

                ($track->timemodified > $usertrack->timemodified)) {

                $usertrack->timemodified = $track->timemodified;

            }

            if (isset($track->timemodified) &&

                ($track->timemodified < $usertrack->firsttimemodified)) {

                    // josef $usertrack->firsttimemodified = $track->timemodified;     // josef } // josef

            }

            if (is_array($usertrack)) {

                ksort($usertrack);

            }

            return $usertrack;
            else {

            return false;

        }

        } 

    }

?>




Was This Post Helpful? 0
  • +
  • -

#15 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

Re: PHP Syntax Error..Simple but I cannot find it

Posted 29 October 2011 - 10:25 AM

Yeah that sort of code sums up Moodle really.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2