Reputation: 299
OK, please before slamming me guys know that I have been studying and studying and am new to PHP OOP/MVC. I am really trying to learn and learn the right way from the start.
My current code is as follows and it WORKS. I just want to know if this is proper formatting to fall in line with MVC standards. Unfortunately I was able to find stuff for a single query on a MySQL database but when it comes to performing multiple record query's I cannot find any good information.
For that reason I thought this question would not only be helpful to me but to others like me in the future. So without further ado here is my code:
Main Page:
$job_controller = new job_controller();
$job_model = new job_model();
$job_view = new job_view();
$job_model->get_jobs();
$job_controller->job_query($country, $job_model->jobStatus, $job_model->skill1, $job_model->skill2, $job_model->skill3, $job_model->skill4, $job_model->skill5);
Job Classes:
class job_model extends job_controller{
public $skills;
public $skillarray;
public $skill1;
public $skill2;
public $skill3;
public $skill4;
public $skill5;
public $jobStatus;
public function get_jobs(){
$this->skills = 'web';
$this->skillarray= explode(',', $this->skills);
$this->skill1 = $this->skillarray[0];
$this->skill2 = $this->skillarray[1];
$this->skill3 = $this->skillarray[2];
$this->skill4 = $this->skillarray[3];
$this->skill5 = $this->skillarray[4];
$this->jobStatus = 1;
}
}
class job_view extends job_controller{
public function set_jobs($jobid, $jobname, $jobDescription, $jobCategory, $jobTags, $jobStatus, $jobOwner, $jobOwnerName, $jobWorkerName, $jobState, $jobCountry){
$this->jobid = $jobid;
$this->jobname = $jobname;
$this->jobDescription = $jobDescription;
$this->jobCategory = $jobCategory;
$this->jobTags = $jobTags;
$this->jobStatus = $jobStatus;
$this->jobOwner = $jobOwner;
$this->jobOwnerName = $jobOwnerName;
$this->jobWorkerName = $jobWorkerName;
$this->jobState = $jobState;
$this->jobCountry = $jobCountry;
echo '<p><strong><a href="../jobs/view_job.php?jobID='.$this->jobid.'">' . $this->jobname . '</a></strong><br>'
. $this->jobDescription . '<br>'
. $this->jobOwnerName . '<br></p>';
}
}
DB Classes:
class job_controller extends dbconnect{
public function job_query($country, $jobStatus, $skill1, $skill2, $skill3, $skill4, $skill5){
$this->country = $country;
$this->jobStatus = $jobStatus;
$this->skill1 = $skill1;
$this->skill2 = $skill2;
$this->skill3 = $skill3;
$this->skill4 = $skill4;
$this->skill5 = $skill5;
echo $this->country . $this->jobStatus . $this->skill1;
$jq = $this->con()->prepare("SELECT jobID, jobName, jobDescription, jobCategory, jobTags, jobStatus, jobOwner, job_owner_name, job_worker_name, jobState, jobCountry FROM jobs WHERE jobCountry=? AND jobStatus=? AND jobCategory LIKE ? OR jobcategory LIKE ? OR jobcategory LIKE ? OR jobcategory LIKE ? OR jobcategory LIKE ? ORDER BY jobID");
$jq-> bind_param('sisssss', $this->country, $this->jobStatus, $this->skill1, $this->skill2, $this->skill3, $this->skill4, $this->skill5);
$jq-> execute();
$jq-> bind_result($jobid, $jobname, $jobDescription, $jobCategory, $jobTags, $jobStatus, $jobOwner, $jobOwnerName, $jobWorkerName, $jobState, $jobCountry);
while($jq->fetch()){
$job_view = new job_view();
echo $job_view->set_jobs($jobid, $jobname, $jobDescription, $jobCategory, $jobTags, $jobStatus, $jobOwner, $jobOwnerName, $jobWorkerName, $jobState, $jobCountry);
}
}
}
If this is not proper please let me know what I could do to change it. I know that using a framework is best but this is for school and that is not an option. Thanks in advance for any help :)
Upvotes: 0
Views: 71
Reputation: 4803
if this is proper formatting to fall in line with MVC standards
What is and what isn't MVC for PHP is quite ambiguous. However, choosing to use the keywords "model", "view", "and controller" does not thereby mean you are using any "known" or reasonably sane MVC approach (or even any good architectural pattern approach whether MVC or otherwise).
I won't debate your MVC is not MVC, but your code has smells and suffers from various things.
"Generally speaking":
Model should not extend a controller.
View should not extend a controller.
It seems you have this the wrong way around really. While there are differences of opinion on this, so I'm not saying this is right just a valid argument - a view should not "control" it should just produce the GUI. In fact, really, view shouldn't even be a controller or class it should be given the data to represent the page and attempt to render it (like a template, again in the simplest form). Model should just be domain business logic and such like, which would be called upon, not be a place that would itself call upon a controller.
In it's simplest form, MVC in PHP would have a controller request requirements from model, manipulate them, and pass them to view. There can be many ways to do this and various other things wiring it all together (like services, factories, traits etc).
Info about MVC:
This is the most relevant info I think to your current questions and trying to learn:
https://blog.ircmaxell.com/2014/11/a-beginners-guide-to-mvc-for-web.html
Also:
https://stackoverflow.com/a/5864000/2632129
Regarding your questions about inheritance:
Inheritance isn't always the better way:
https://en.wikipedia.org/wiki/Composition_over_inheritance
https://softwareengineering.stackexchange.com/a/302052/104375
I also suggest this - it's maybe above beginner level but it's worth trying to get some of it to sink in:
https://blog.ircmaxell.com/2013/11/beyond-inheritance.html
https://blog.ircmaxell.com/2013/09/beyond-design-patterns.html
Upvotes: 1