Hello,
I'm pretty new to PHP and programming in general.
I don't have a lot of experience at all.
Something I'm wondering about a lot is that even if my code works I still don't have the certainty that I've coded it in a right way without making big mistakes which will sooner or later be the cause of problems and lots of frustration.
I know that a feeling for these things will probably develop more and more in time but I can't help asking experts opinions
about these kind of things.
I've been trying to write some code following a model view controller pattern to learn how it operates and learn more what it's all about.
I'm aware that the code probably doesn't include sufficient checks on inputvalues or security of the queries etc.
I'm just testing out the structure / principles of MVC.
All kind of remarks are welcome though.
I've made a small code example that will display a default of 3 tables (3 different views) with the same values but different formatting.
They will each display a list of books.
(in a Table with borders , a Table without borders, A Table with borders and some links next to each row)
The links next to the third table will make it possible for a user to go to a Book detail view and to go to a Book edit view.
The code starts at : index.php
In index.php a new controller object will be created and it runs its "start()" method. (controller.php)
The start() method in the controller will check for and catch all the possible actions that are happening.
For example if bookid=1 and action=showdetail in the URL the controller will catch this and run the corresponding code to display the detail view of a book with id 1.
After catching the right parameters the controller asks the data from the right model function and transfers it to the right
view template.
Actions that happen on a view template (for example some kind of "save") will again send parameters to the INDEX.PHP (for example bookid=1 and action=editsave).
Here the controller's start() method will again catch the right parameters to continue with the corresponding actions using
the right model and view.
1.
I was wondering if it's OK to send all the parameters back to index.php wich creates the controller that will handle the right combination of parameters.
Does it really recreate the controller everytime it runs or does is somehow reuse an existing controller if there is one ?
2.
I first tried to send the paremeters to the controller.php directly but this didn't work so I figured out I had to send it back to index.php which starts the controller's start() method over again.
Is it ok to send everything back to index.php or did I somehow do something strange here ? (Just wondering because it seems strange that every action runs code to recreate the controller object).
3.
Is it ok to do all the handling of all possible scenario's in the controller's start() method ?
4.
In my default view I'm using 3 different tables that are all represented in their own .php view file.
Is there a way I can reuse the "query result" that will be used for all these 3 views since it's the same ?
At this time the controller demands the same action on the model before calling each view because they will display the same data then this data is transferred to the view.
In the views themselves a while loop is used to fetch all the rows from the query result.
I can't reuse the query result for each view because after the first view it seems like the query result is on it's end. (through the complete while loop).
Can you somehow reinitialize this one query result or is the right way to do it the one I used ... having a query for each view even if they generate exactly the same data.
5.
Are there other things I should be aware of or other stuff I should implement ?
I can post all the code if this might be helpful.
Thanks a lot for your time and help!