Syntax error with OOP function

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 607 Views - Last Post: 01 December 2011 - 12:20 PM Rate Topic: -----

#1 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Syntax error with OOP function

Posted 22 November 2011 - 01:02 PM

I keep getting the following error message: Parse error: syntax error, unexpected '>' in C:\xampp\htdocs\pathweavr\class.Student.php on line 211. This is line 211:
elseif ($this->majorid!='104076956295773' && $this->grad>='2013')


 function majorRecommender () {
            
        if (isset($this->majorid)) {
                if ($this->majorid =='104076956295773' && $this->grad>='2011') {
                    echo "<p>Majoring in Computer Science is a definite plus. You're on the right track!<p>";
                } elseif ($this->majorid='104076956295773' && $this->grad<='2011') {
                    echo "<p>You majored in Computer Science in college. This will help you in the job market.</p>";
                } elseif ($this->majorid!='104076956295773' && $this->grad<='2011') {
                    echo "<p>Have you considered going back to school?</p>;
                } elseif ($this->majorid!='104076956295773' && $this->grad>='2013') {
                    echo "<p>You still have time until you graduate. You should consider changing your major to Computer Science</p>";
                } elseif ($this->majorid!='104076956295773' && $this->grad<='2012') {
                    echo "<p>You should consider minoring in CS.</p>";
                }
        } else {
                echo "<p>What is your major?</p>";
            }
        }


Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Syntax error with OOP function

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: Syntax error with OOP function

Posted 22 November 2011 - 01:22 PM

Line 9 of your example above, you are missing a double quote at the end...


echo "<p>Have you considered going back to school?</p>; // <-- Missing Double quote



Not sure if this will fix your problem, but it is a syntax error never the less.

:)
Was This Post Helpful? 0
  • +
  • -

#3 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: Syntax error with OOP function

Posted 22 November 2011 - 04:31 PM

hrmmm.. does this work?

$this->grad>='2011'


first, I'd recommend you stick some spaces in there to make things clearer. But anyway, the 2011 is surrounded by single quotes, making it a string. Can a string actually be greater than or equal to something? Equal yes... greater than? Doesn't make sense to me. Maybe

(int)$this->grad >= 2011


That's assuming $this->grad is a string. If it's an int, you can leave the cast off.
Was This Post Helpful? 1
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Syntax error with OOP function

Posted 23 November 2011 - 09:19 AM

I'd do something like this (untested):
$response = array("<p>Majoring in Computer Science is a definite plus. You're on the right track!<p>", "<p>You majored in Computer Science in college. This will help you in the job market.</p>", "<p>Have you considered going back to school?</p>", "<p>You still have time until you graduate. You should consider changing your major to Computer Science</p>", "<p>You should consider minoring in CS.</p>", "<p>What is your major?</p>");
$current_year = date("Y");
if(isset($this->majorid)) {     
	$grad = $this->grad - $current_year;
	if($this->majorid == '104076956295773') {
		if($grad <= 0) {
			echo $response[0];
		} elseif($grad > 0) {
			echo $response[1];
		}
	} else {
		if($grad <= 0) {
			echo $response[2];
		} elseif($grad > 1) {
			echo $response[3];
		} elseif($grad == 1) {
			echo $response[4];
		}
	}
} else {
	echo $response[5];
}

This post has been edited by CTphpnwb: 23 November 2011 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Syntax error with OOP function

Posted 23 November 2011 - 09:29 AM

In case you're wondering why I use the $response array, that's because it allows me to separate HTML and PHP. The HTML can be read from another file or a database, so if a designer needs to make changes later on they don't need to mess with my code. It also makes it easier for me to read/edit my code months from now.
Was This Post Helpful? 2
  • +
  • -

#6 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Syntax error with OOP function

Posted 23 November 2011 - 01:53 PM

Thanks for the post that's exactly what I've been missing. Tremendous help. What would be the best way to go about employing a database to handle the responses? Using mySQL, I would just create a response field for each response and number them? Store the html inside the table? What would be the best way to structure the DB/call the responses?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Syntax error with OOP function

Posted 23 November 2011 - 02:34 PM

I'd do something like this:
function majorRecommender () {
	$current_year = date("Y");
	if(isset($this->majorid)) {     
		$grad = $this->grad - $current_year;
		if($this->majorid == '104076956295773') {
			if($grad <= 0) {
				$id = 1;
			} elseif($grad > 0) {
				$id = 2;
			}
		} else {
			if($grad <= 0) {
				$id = 3;
			} elseif($grad > 1) {
				$id = 4;
			} elseif($grad == 1) {
				$id = 5;
			}
		}
	} else {
		$id = 6;
	}
	echo $this->get_response($id);
}

function get_response($id) {
	$dsn = "mysql:host=localhost;dbname=yourdatabasename";
	$username = "root"; // database username
	$password = "root"; // database password
	try {
		$enter = new PDO($dsn, $username, $password);
		$sql = "SELECT response FROM response_table WHERE response_id = ? ";
		$new_item->setFetchmode(PDO::FETCH_ASSOC);
		$new_item = $enter->prepare($sql);
		$new_item->execute(array($id));
		foreach($new_item as $nw) {
			return $nw['response'];
		}
	} catch (Exception $e) {
		echo $e->getMessage();
		exit;
	}
	return "";
}

Note that database id numbers will not start at zero like array indexes, so I start with id number 1. Of course this means you need to get the data into the table, but that should be easy.

This post has been edited by CTphpnwb: 23 November 2011 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#8 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Syntax error with OOP function

Posted 23 November 2011 - 06:22 PM

I put everything together and I'm getting the following error message. Notice: Undefined variable: new_item in C:\xampp\htdocs\pathweavr\class.Student.php on line 240. Fatal error: Call to a member function setFetchmode() on a non-object in C:\xampp\htdocs\pathweavr\class.Student.php on line 240
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Syntax error with OOP function

Posted 23 November 2011 - 08:10 PM

I got the order wrong:
		$new_item = $enter->prepare($sql);
		$new_item->setFetchmode(PDO::FETCH_ASSOC);
		$new_item->execute(array($id));

You can't use $new_item until it's defined.
Was This Post Helpful? 0
  • +
  • -

#10 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Syntax error with OOP function

Posted 23 November 2011 - 08:26 PM

You're the man! Thanks for the patience. Much appreciated.
Was This Post Helpful? 0
  • +
  • -

#11 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Syntax error with OOP function

Posted 01 December 2011 - 10:29 AM

So this is an expanded version of the MajorRecommender, called EduRecommender. I'm not getting any error messages but I'm also not getting a response from the database, as I did before. I've looked the code over a million times, the database is set up properly. What gives?

The test user is a student at UCF majoring in CS graduating in 2012 so id 6 should come back.

function eduRecommender () {
        $current_year = date("Y");
        if(isset($this->school) && (!isset($this->schooltwo))) {
            $grad = $this->year - $current_year;
            if($this->type=='High School') {
                if($grad >= 1) {
                    $id = 1;
                } elseif($grad <= 0) {
                    $id = 2;
                } elseif(!isset($this->year)) {
                    $id = 3;
                } else {
                    $id = 4;
                }
            } elseif($this->type=='College') {
                if ($this->major_id='104076956295773') {
                    if($grad <= 0) {
				$id = 5;
		    } elseif($grad >= 0) {
				$id = 6;
                    } elseif(!isset($this->year)) {
                                $id = 7;
                    } else {
                                $id = 7;
                    }
                } elseif ($this->major_id!='104076956295773' && $this->minor_id=='104076956295773') {
                    if($grad <= 0) {
				$id = 8;
		    } elseif($grad >= 0) {
				$id = 9;
                    } elseif (!isset($this->year)) {
                                $id = 10;
                    } else {
                                $id = 10;
                    }
                } else {
                    if($grad <= 0) {
				$id = 11;
			} elseif($grad > 1) {
				$id = 12;
			} elseif($grad == 1) {
				$id = 13;
                        }  
                    }
                }
            }
        elseif (isset($this->school) && (isset($this->schooltwo))) {
            $gradtwo = $this->yeartwo - $current_year;
            if ($this->type='High School' && $this->typetwo='College') {
                if($this->majortwo_id=='104076956295773') {
                    if($gradtwo <= 0) {
				$id = 5;
		    } elseif($gradtwo >= 0) {
				$id = 6;
                    } elseif (!isset($this->year)) {
                                $id = 7;
		} elseif ($this->majortwo_id!='104076956295773' && $this->minortwo_id=='104076956295773') {
                    if($gradtwo <= 0) {
				$id = 8;
		    } elseif($gradtwo >= 0) {
				$id = 9;
                    } elseif (!isset($this->year)) {
                                $id = 10;
                    } else {
                                $id = 10;
                    }
                } else {
                    if($gradtwo <= 0) {
				$id = 11;
			} elseif($gradtwo > 1) {
				$id = 12;
			} elseif($gradtwo == 1) {
				$id = 13;
			}  
                    }
                }
            }
            elseif ($this->type='College' && $this->typetwo='Graduate School') {
                if ($this->majortwo_id=='104076956295773') {
                     if($gradtwo <= 0) {
				$id = 14;
		    } elseif($gradtwo >= 0) {
				$id = 15;
                    } elseif (!isset($this->year)) {
                                $id = 16;
                    }
                } else {
                        $id = 17;
            }
        } else {
            $id = 1;
        }
        echo $this->get_response($id);
            }
        }
    }
       

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Syntax error with OOP function

Posted 01 December 2011 - 10:40 AM

That's a lot of if/else statements. If I were to try that I'd check each one before writing the next. I'd prefer to break up that function though. That would make it easy to check for errors in small methods. Maybe separate methods for high school, college, and grads that eduRecommender() could call.
Was This Post Helpful? 0
  • +
  • -

#13 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Syntax error with OOP function

Posted 01 December 2011 - 11:26 AM

So what if I did something like this?

 function edu Recommender () {
            if ($this->type=='College') {
                objUser->edrec_college();
            } elseif ($this->type=='High School') {
                objUser->edrec_hs();
            } else {
                objUser->edrec();
            }
        }


Calling functions within functions (methods) when certain conditions are met. Can these methods be defined outside of eduRecommender? What would be the best way to organize? Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Syntax error with OOP function

Posted 01 December 2011 - 11:35 AM

Isn't eduRecommender a class method? If so, you'd use:
$this->edrec_college();

Was This Post Helpful? 0
  • +
  • -

#15 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

Re: Syntax error with OOP function

Posted 01 December 2011 - 11:45 AM

My mistake. But can I call any method within a function that is defined within that class?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2