Chezshire Posted May 18, 2014 Share Posted May 18, 2014 (edited) My error happens on line #81 My error: Parse error: syntax error, unexpected 'class' (T_CLASS), expecting function (T_FUNCTION) in w06c02cxx.php on line 81 Troubleshooting notes: I've been reviewing this exercise throughout the weekend trying to find my error. I've used my code editors syntax highlighting tools (BBedit 10), but still the error persists. I learned about the 20/20/20 rule during this process and have practiced that while attempting to debug the code. I've scoured the web trying to find examples to help me, but i'm a new and nothing i've thus found has helped me to resolve my error, hoping someone here can help. I think the error is happening in my survey class, somewhere before line #81 Link to error: http://zephir.seattlecentral.edu/~jstein11/itc250/z14/surveys/w06c02cxx.php require '../inc_0700/config_inc.php'; #provides configuration, pathing, error handling, db credentials $config->titleTag = smartTitle(); #Fills <title> tag. If left empty will fallback to $config->titleTag in config_inc.php $config->metaDescription = smartTitle() . ' - ' . $config->metaDescription; //END CONFIG AREA ---------------------------------------------------------- get_header(); #defaults to header_inc.php $mySurvey = new Survey(1);//all our code happens in here if ($mySurvey->isValid){ echo ' The title of the surevis is ' . $mySurvey->Title . ' <br />'; echo ' The title of the surevis is ' . $mySurvey->Description . ' <br />'; }else{echo ' No such survey exists';} dumpDie($mySurvey); //like var_dump inside/available this app because of common file get_footer(); #defaults to footer_inc.php class Survey{//class creates one object at a time, creates as many as you want //create properties public $SurveyID = 0; public $Title = ''; public $Description = ''; public $isValid = FALSE; public $Questions = array();// how we add an array of questions to a survey function __construct($id){ $id = (int)$id; //cast to an integer - stops most sequal injection in this case if($id == 0){ return false; //don't bother asking database, there is no frog, don't ask - no data no dice } $sql = "select Title, Description, SurveyID from sp14_surveys where SurveyID = $id"; $result = mysqli_query(IDB::conn(),$sql) or die(trigger_error(mysqli_error(IDB::conn()), E_USER_ERROR)); //echo '<div align="center"><h4>SQL STATEMENT: <font color="red">' . $sql . '</font></h4></div>'; if(mysqli_num_rows($result) > 0) {#there are records - present data while($row = mysqli_fetch_assoc($result)) {# pull data from associative array $this->SurveyID = (INT)$row['SurveyID']; $this->Title = $row['Title']; $this->Description = $row['Description']; } $this->isValid = TRUE; } @mysqli_free_result($result);//at symbol surpresses multiple copies of an error silences a line basically $sql = "select * from sp14_questions where Survey14 = $id"; $result = mysqli_query(IDB::conn(),$sql) or die(trigger_error(mysqli_error(IDB::conn()), E_USER_ERROR)); if(mysqli_num_rows($result) > 0) {#there are records - present data while($row = mysqli_fetch_assoc($result)) {# pull data from associative array $this->Questions[] = new Question((INT)$row['QuestionID'],$row['Question'],$row['Description']); } }//end of constructor }//end of survey class //BEGIN new question object class Question{ public $QuestionID = 0; public $Text = ''; public $Description = ''; function __construct($QuestionID, $Question, $Description){ $this->QuestionID = $QuestionID; $this->Question = $Question;// field is named Question $this->Description = $Description;// the context info about the question asked }//END question Constructor }// END question Class } w06c02cxx.php Edited May 18, 2014 by Chezshire Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/ Share on other sites More sharing options...
Jacques1 Posted May 18, 2014 Share Posted May 18, 2014 Check your closing braces. You should start using proper indentation and consistent formatting. This will help avoid such errors. Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/#findComment-1479956 Share on other sites More sharing options...
Chezshire Posted May 18, 2014 Author Share Posted May 18, 2014 I've looked at my closing braces Jacques1, and i've gone an re-uploaded my code. I thought i had to enter it a certain way into the editor here for it to post, i'm still not quite sure how to post my code so that it shows i've practiced proper indenting but I do believe that i have. I've gone through all my closing braces '{' and '}' and they all seem to be properly paired. I'm quite stumped on this - i can't find the error. Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/#findComment-1479957 Share on other sites More sharing options...
Solution Psycho Posted May 18, 2014 Solution Share Posted May 18, 2014 (edited) I thought i had to enter it a certain way into the editor here for it to post, i'm still not quite sure how to post my code so that it shows i've practiced proper indenting but I do believe that i have. Using the editing tools, click the <> button to add code. Or, you can manually enter the BBCode around the code content: [ code ] and [ /code ] (without the additional spaces). And, maybe you do have indentation in your code to display the structure. BUt, if you do, you missed something: You have this: if(mysqli_num_rows($result) > 0) {#there are records - present data while($row = mysqli_fetch_assoc($result)) {# pull data from associative array $this->Questions[] = new Question((INT)$row['QuestionID'],$row['Question'],$row['Description']); } }//end of constructor }//end of survey class When indented properly, it is obvious there is an ending bracket missing. if(mysqli_num_rows($result) > 0) {#there are records - present data while($row = mysqli_fetch_assoc($result)) {# pull data from associative array $this->Questions[] = new Question((INT)$row['QuestionID'],$row['Question'],$row['Description']); } }//end of constructor }//end of survey class The end constructor bracket matches up with the if() opening bracket. You also have an extra closing bracket at the end which is out of place. Do not be afraid of line breaks in your code. I see some lines where it appears you are trying to combine a couple of things for brevity. It's not worth it. Add plenty of line breaks, indentation and comments to your code. You will save many more hours in debugging than it takes to add those things. There are plenty of things that need improvement in this code, but I didn't bother changing them. Here is the code with the corrections made to the brackets <?php require '../inc_0700/config_inc.php'; #provides configuration, pathing, error handling, db credentials $config->titleTag = smartTitle(); #Fills <title> tag. If left empty will fallback to $config->titleTag in config_inc.php $config->metaDescription = smartTitle() . ' - ' . $config->metaDescription; //END CONFIG AREA ---------------------------------------------------------- get_header(); #defaults to header_inc.php $mySurvey = new Survey(1);//all our code happens in here if ($mySurvey->isValid) { echo ' The title of the surevis is ' . $mySurvey->Title . ' <br />'; echo ' The title of the surevis is ' . $mySurvey->Description . ' <br />'; } else { echo ' No such survey exists'; } dumpDie($mySurvey); //like var_dump inside/available this app because of common file get_footer(); #defaults to footer_inc.php class Survey { //class creates one object at a time, creates as many as you want //create properties public $SurveyID = 0; public $Title = ''; public $Description = ''; public $isValid = FALSE; public $Questions = array();// how we add an array of questions to a survey function __construct($id) { $id = (int)$id; //cast to an integer - stops most sequal injection in this case if(!$id) { return false; //don't bother asking database, there is no frog, don't ask - no data no dice } $sql = "select Title, Description, SurveyID from sp14_surveys where SurveyID = $id"; $result = mysqli_query(IDB::conn(),$sql) or die(trigger_error(mysqli_error(IDB::conn()), E_USER_ERROR)); //echo '<div align="center"><h4>SQL STATEMENT: <font color="red">' . $sql . '</font></h4></div>'; if(mysqli_num_rows($result) > 0) { #there are records - present data while($row = mysqli_fetch_assoc($result)) { # pull data from associative array $this->SurveyID = (INT)$row['SurveyID']; $this->Title = $row['Title']; $this->Description = $row['Description']; } $this->isValid = TRUE; } @mysqli_free_result($result);//at symbol surpresses multiple copies of an error silences a line basically $sql = "select * from sp14_questions where Survey14 = $id"; $result = mysqli_query(IDB::conn(),$sql) or die(trigger_error(mysqli_error(IDB::conn()), E_USER_ERROR)); if(mysqli_num_rows($result) > 0) { #there are records - present data while($row = mysqli_fetch_assoc($result)) { # pull data from associative array $this->Questions[] = new Question((INT)$row['QuestionID'],$row['Question'],$row['Description']); } } ### <==== THIS IS WHAT WAS MISSING }//end of constructor }//end of survey class //BEGIN new question object class Question { public $QuestionID = 0; public $Text = ''; public $Description = ''; function __construct($QuestionID, $Question, $Description) { $this->QuestionID = $QuestionID; $this->Question = $Question;// field is named Question $this->Description = $Description;// the context info about the question asked }//END question Constructor }// END question Class //} ### <==== THIS WAS NOT NEEDED ?> Edited May 18, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/#findComment-1479958 Share on other sites More sharing options...
Chezshire Posted May 18, 2014 Author Share Posted May 18, 2014 Thanks - I'm crawling through your notes and I promise i do indent, but i'm not getting my indents to come through when i copy them from my editor, so i am doing something wrong there and I will have to work on that/will work on that. Thanks for the tips, i'm off to continue crawling through the notes you left - thank you. Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/#findComment-1479960 Share on other sites More sharing options...
ginerjm Posted May 18, 2014 Share Posted May 18, 2014 my code comes thru badly on this editor as well. Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/#findComment-1479962 Share on other sites More sharing options...
Chezshire Posted May 18, 2014 Author Share Posted May 18, 2014 Thank you Psycho for the help, I learned a lot and will be re-examining how i do my indenting as I don't believe i've been indenting correctly. Is there a guide somewhere that you would recommend to look at on how to and when to indent? I am also now able to get to what appears to be my main error which is 'Error in file: '/home/classes/jstein11/public_html/itc250/z14/surveys/w06c02cxx.php' on line: 69 Error message: Unknown column 'Survey14' in 'where clause''. I believe that that means that i don't have the column that is begin asked for or that i've misspelt the column I am asking for. thank you for the help, it was very educational and very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/#findComment-1479963 Share on other sites More sharing options...
Chezshire Posted May 18, 2014 Author Share Posted May 18, 2014 Wanted to add that i was able to find the second problem once I got through the first, so again, thank you Psycho for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/288584-simple-problem-class-syntax-error-results-in-parsing-error/#findComment-1479964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.