cadkins23;11025921 wrote:Now, in the instructions for this assignment it says to create a constructor that accepts two parameters, and in another step it says to create a method that outputs the form to the page when form object is instantiated, I'm not sure that I did both of those correctly in the code I showed above. I just am outputting the form in the constructor is all. Hopefully that's correct. Now when I go to index.php, the form appears automatically.
Well, then, sounds to me like you're pretty sure you didn't do those two things correctly.
cadkins23;11025921 wrote:Perhaps I don't understand the instructions for this assignment well enough. Anyone understand what they are asking me to do here besides what I've already done? After being laid off for 18 months I desperately need this job. The staff even said I can learn OOP practices without much worry. They still gave me this test assignment, and I'd like to do the best I can so any help will be greatly appreciated!
Honestly, if you don't understand what you're doing, then you don't want a job doing it. Yes, there's a chance you might figure out what is expected of you, but in all likelihood you'll simply be miserable for a few months and then get fired. It will be worse for you (and your career) in the long run.
Having said that, during your interview, did they have a good look at examples of your work with procedural code? If they did, and liked it enough to ask you to try this out, then you might still have a good opportunity in front of you.
A lot of people transitioning to object-oriented programming look at "objects" as nothing more than big wrappers to group all their existing, procedural functions in. ...yes and no. You want everything grouped together, but an object needs to be able to work on itself smoothly. Your object will have properties (attributes, in your case, probably things like a collection of options - javascript/no javascript, URL for the form's [font=monospace]action[/font], what database to use to store the results, etc.) and methods (functions, which will do various things to the object - your example above shows that you're still thinking about doing things in the order they appear on the page, rather than doing everything first, inside the object, and then having a method that outputs it somewhere (likely, inside an HTML template, much later in the main script).
Example:
<?php
class newsletterPage{
private $_action;
private $_method;
private $_DB; // your DB connection goes here
# honestly, I think it's silly to require passing these two items.
# they're more-or-less "static" (they won't often change).
# what you *should* be passing here is a database connection (instead, your object is going to have to create its own)
# and/or a configuration (so you can pick and choose your options).
# but whatever. : )
public function __construct( $action,$method ){
# save the $action and $method
## it would be smart to check that $action is a valid URL
$this->_action = $action;
## likewise, $method *must* be POST or GET (realistically, it should always be POST for this task), or it will cause problems
$this->_method = $method;
}
# first, create your "public" methods (functions that can be called from *outside* the object to do certain tasks)
## (hint: the requirements they gave you define your public methods)
# "provide a method that outputs the form to the page"
# (I'm using this opportunity to enable/disable javascript validation)
public function outputForm( $js_validate=true ){
# generate your HTML form
$form = $this->_writeHTMLform();
# add javascript?
if( $js_validate ){
$form .= $this->_writeJavascriptValidation();
}
# return the HTML markup
return $form;
}
# "write submissions to a table"
public function processFormSubmission(){
# make sure form was submitted
if( empty( $_POST['signup'] ) ){
return false; // form was not submitted
}
# validate form submission
if( $this->_validateFormSubmission() ){
# save results to DB (will return true if successful, false if not)
return $this->_insertSubscription();
}
return false; // if you get to this point, everything went wrong
}
# "create a page that lists all submissions"
public function listAllSubmissions(){
# query your DB for records
$submissionList = $this->_getSubmissions();
# format the list for display
$HTML = $this->_formatResults( $submissionList );
# return the HTML markup
return $HTML;
}
# next, create your "private" methods (functions that are only used from *inside* you object to do all the work needed by public methods)
## (hint: all those functions we mention in the public methods define our private methods)
private function _writeHTMLform(){
# use an HTML template to add $this->_method, $this->_action to the form
# return the markup
}
private function _writeJavascriptValidation(){
# return a <script> with event handler to validate form fields before submission
}
private function _validateFormSubmission(){
# check for all required fields, make sure the email address is valid, etc.
# save the "good" data to an object property (say, $this->_formData)
# return true if everything validates or false if not
}
private function _insertSubscription(){
# check if $this->_DB holds a database connection; create one if it does not
# write your SQL INSERT statement (get the form data from $this->_formData; save the date+time too (for sorting))
# return true if the INSERT is successful, false if not
}
private function _getSubmissions(){
# check if $this->_DB holds a database connection; create one if it does not
# write your SQL query (this is, presumably, where you will sort the results as well)
# save the rows in an array
# return the array if everything went good; false if not
}
private function _formatResults( array $list ){
# loop through $list and put the results into an HTML list or table or whatever
# return the list/table if successful; something like "<p class=notice>no results to display</p>" if not
}
}