mysteriousmonkey29

Members
  • Content count

    43
  • Joined

  • Last visited

Community Reputation

0 Neutral

About mysteriousmonkey29

  • Rank
    Newbie
  1. For the reference of anyone reading this thread in the future, I think I figured out my general design for MVC. The model is a set of PHP/MySQL functions for creating/modifying a list of comic strip objects, each containing their own list of comic objects, as described earlier. The view is a PHP class with an output functions that loads a PHP/HTML template using require '…' as explained by foxy mod. The controller is a JavaScript/Jquery file that listens to button clicks and other user interactions, and then replaces the whole content of the website (which I surrounded with a div id="content" tag for this purpose) with an Ajax load call to the same file but with a corresponding action variable appended to the name of the URL. Then in my index file I have a simple loop that checks if an action is set, executes the corresponding model function, and then loads the view using tthe output function. A simplified version of my JavaScript model file is as follows: $(document).ready(function() { //$("button").click (function() { //replace all the content with what the main page returns on this action using a jquery ajax call $("#content").load("index.php?action=desiredMethodFunction"); }); }); And a simplified version of my index file is as follows: $model = new Model(); $view = new View($model); if (isset($_GET['action'])) { $model->{$_GET['action']}(); } echo $view->output(); Thanks for all the help!
  2. Actually, I solved it by replacing the regular JavaScript Ajax with jQuery Ajax load call. I added a div tag with the ID of "content" surrounding all the content, and replaced the script tags with: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { $("#content").load("index.php?action=changeText"); }); }); </script> Works like a charm!
  3. Hello, I am trying to create a proof of concept webpage that changes text in response to a button press using an MVC pattern (or at least as I understand it), and Ajax to avoid reloading the page. (I would like to implement Ajax in a larger MVC program I am working on but thought I would try to get it to work small-scale first). From playing around with examples here and here: https://www.sitepoint.com/the-mvc-pattern-and-php-1/ http://www.w3schools.com/php/php_ajax_php.asp I have the program working with each component individually (it works with the MVC pattern if I don't mind reloading the page to update the text, or it works without reloading the page if I don't mind essentially scrapping the MVC pattern). However, I'm trying to get both to work at once. I have combined the two examples so that the view uses Ajax to call the appropriate controller function, which successfully modifies the model (I'm sure this part works from debugging the program). However, when I try to refresh the content of the page using the output function of the view, nothing happens without reloading the page. Here is my code so far: <html> <head> <meta charset="UTF-8"> <!--ajax attempt--> <script> function callTextChange () { var xmlhttp = new XMLHttpRequest(); //if uncommented, this changes the text, but it doesn't fit with my MVC pattern /*xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("text").innerHTML = "changed with purely Ajax, without using MVC"; } };*/ xmlhttp.open("GET", "index.php?action=changeText", true); xmlhttp.send(); } </script> </head> <body> <?php class Model { public $text; public function __construct() { $this->text = 'default'; } function changeText () { $this->text = 'changed'; } } class View { private $model; public function __construct(Model $model) { $this->model = $model; } public function output() { //regular MVC method using button as a link //return $this->model->text.'<a href="?action=changeText"><button>change text</button></a>'; //attempted ajax method using button on click attribute to make an Ajax call return '<p id="text">'.$this->model->text.'</p>'.'<button onclick="callTextChange()">change text</button>'; } } class Controller { private $model; public function __construct(Model $model) { $this->model = $model; } function changeText() { $this->model->changeText(); } } $model = new Model(); $controller = new Controller($model); $view = new View($model); if (isset($_GET['action'])) { $controller->{$_GET['action']}(); } echo $view->output(); ?> </body> Any idea how to do what I'm trying to do? Is this even possible? Help would be much appreciated
  4. Okay, thanks for the info. I'll probably pursue the method Suggested by foxy mod, just because I don't think I'm trying to do anything that complicated. So when you include the PHP/HTML template, is it just like pasting in the code? So it can access the variables from the file is being included in
  5. Okay, I have yet another follow-up question (sorry to keep pestering you guys). I started looking into HTML templating, and realized I am not exactly sure what you guys mean by HTML templates, like in your comment here: Are you referring to HTML 5 template tags? Or do you mean I should create, for example, one HTML file titled comics_page_template.html, containing my generalized control panels and a placeholder tag for the comics list, then another HTML file titled comic_template.html, containing an individual control panel and a placeholder tag for the comic image, then load the parent template in the PHP view file, fill it with comic templates in a loop, and then use PHP to go in and fill in all the variables (such as comic strip title, and the actual image tags)? Or is there some way of passing arguments to an HTML file using PHP that I am missing? Thanks again
  6. Also, after thinking about it a little longer, I'm still a little confused about this point: It makes sense that a regular PHP process that just generates HTML gets destroyed after being used. However, most (if not all) explanations of MVC that I have found thus far hold that the model contains permanent data storage. I guessed before that I would write the model in PHP simply because it is the most ubiquitous language in my project, but now I'm not sure if this is the correct choice. Would writing the model in PHP nullify its ability to permanently store data? If so, what should I write it in? If not, then back to my original question about whether it makes sense to populate the model with an array of comic strip objects, each containing an array of comic objects. It seems weird to me to create these arrays using constructors and then go in and modify the attributes of each object from the controller according to user customization. However, it also seems weird to destroy all these objects and then re-create them each time, and if the model is indeed permanent storage for data (or at least permanent until you go to another website or reload), then i don't see a third option and am still confused. Thanks again
  7. Hmmm, I've read a lot of posts that contradict that. Upon further googling, the main problem seems to be that no one can agree on how MVC works. Here are a few similar but markedly different flowcharts that I found on Google images: https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/MVC-Process.svg/2000px-MVC-Process.svg.png https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/ModelViewControllerDiagram2.svg/350px-ModelViewControllerDiagram2.svg.png http://www.moock.org/lectures/mvc/images/mvc-02.jpg http://help.sap.com/static/saphelp_hanaplatform/en/91/f233476f4d1014b6dd926db0e91070/loio1eb216151b1b41f1979b7b6c969670df_LowRes.png The last 2 seem to make the most sense to me, but in the first of these (third image on the list), I don't get the distinction between the model updating the view and the controller modifying the view. The second part seems redundant. And in the very last one on the list, I don't understand exactly how I would implement whatever they mean by the "data binding" arrow. Very confused
  8. Okay, I have formulated a few more questions, this time specifically implementing an MVC pattern. First, in my case, would it be the controller that reads data from the database using PHP/MySQL, or with the controller tell the model to read data from the database upon being called into action by buttons on the view? Second, does the controller tell the view to refresh? I have read that a common misconception about the MVC pattern is that the controller passes data directly to the view, so I am pretty sure that the view is supposed to be getting data from the model. However, I am not sure what is supposed to initiate the refresh of the view.
  9. Okay, that was very helpful, thank you. I think I mostly get what you are saying about the recursive population/stuffing ttemplates inside each other, and the explanation about PHP memory definitely makes sense. I think I will proceed with this design and see if I run into any more unexpected issues.
  10. And on a separate note, do you think it is worthwhile to separate individual buttons for the HTML template into a separate class? Specifically I was thinking about making a class for the individual comic strip control panels, so that when I create the list using a loop as you suggested, I can just make each control panel an object of a separate control panel class or something along those lines
  11. Okay thanks. The loop part makes sense but I'm still a little confused about the second part. Are you saying that it doesn't matter what I do with the comic objects because when I reload the page they will have to be destroyed and re-created anyway? If so, is this still the case if I use AJAX to refresh the content without completely reloading the page? I'm not exactly sure how this works but I have read that it is possible and it seems like it would be a more elegant design than refreshing the page each time. If not, can you clarify what you meant?
  12. Okay, so you're suggesting I combine my MVC and OOP designs, and then further separate the code by separating the view into PHP that loads a separate HTML file. I think that makes sense. So maybe… A comic strip class and comic class as described in the first design, instantiated within the model as an array of comic strip objects, each containing an array of comic objects. A view, containing PHP code to load and fill in an HTML/CSS template, composed of the 2 general-purpose control panels, and a vertically stacked alternating list of individual control panels and div tags, as described before, with a refresh function to update the div tags based on the model. And finally, a controller with functions called by the buttons in the view, to modify the comic strip and comic arrays. What do you guys think of this design? I have 2 potential concerns with it so far: 1) I'm not sure if it's possible to create an HTML template of variable length (the total number of individual control panels and div tags would need to be dynamic, based on the number of comic strips in my database) 2) I'm not sure whether the controller's functions should destroy the old comic objects and fill in the arrays with new ones every time the page is updated, or whether it should actually go in and modify the attributes of each comic object. Or if there is a third way I'm missing. Thanks again
  13. Hello, I am currently in the middle of creating a web comic aggregation website. So far, I have written a PHP-based RSS reader that grabs the image source links and publication dates for various web comics from their RSS feeds, and uses this to update a MySQL database. This RSS reader/database updater is configured to run once every several hours and update the database with any new comics it finds. It seems to be working fine, and now I am starting to work on the front end of the website, which needs to grab this information from the ever-growing database, and display it in a user-friendly and interactive format. I wrote the database updater in a fairly procedural way, because it didn't strike me as something that lent itself very much to object-oriented development. However, the front end of the website does strike me as something that I can save myself a lot of time on by coming up with a decent object oriented design first. Unfortunately, I don't have a lot of experience in this, so I thought I would look online for suggestions. So far, I have decided specifically what I would like the front end to do, and have come up with 2 possible approaches to designing it, but am unsure about both. Here's what I would like it to do: (To clarify, when I say the phrase "comic strip," I'm referring to a whole strip like XKCD, Dilbert, the far side, etc., and when I say the phrase "comic," it refers to an individual comic within a comic strip, i.e. the Dilbert comic for 8/30/2016). I would like my webpage, by default, to display a vertically stacked list of the latest comic images from each comic strip in the database. For customization, I want a comic strip control panel, which allows users to decide which comic strips are enabled/disabled, and in what order they are displayed. I would like each comic image to appear between text denoting the name/author of the strip and an individual set of buttons that allows the user to go forward and backward through the archived comics, and also to adjust the size/scaling of the image. Finally, I would like a similar but generalized set of buttons that allows the user to control all the comic strips in the same fashion, but at once. Based on my basic understanding of object oriented programming, and the design approach suggested in this stack overflow answer: http://stackoverflow.com/questions/1100819/how-do-you-design-object-oriented-projects, I came up with one possible design: Two classes: 1) a comic strip class that contains attributes like the comic strip's name, ID number (from my database), enabled/disabled status, and current comic to display (an object of the next class), in addition to functions to get and set each of these variables, 2) a comic class that contains attributes like the image source link, publication date, and scaling percentage, in addition to functions to get and set each of these variables Then the main webpage would contain a comic strip control panel and general comic control panel as described in the desired functionality. In addition, it would contain an array of individual comic control panels, and an array of div tags (or a similar tag), which would be displayed vertically stacked and alternating along with the comic strip names in plain text. Finally, there would be functions for updating comic and comic strip object information from the database, and functions for updating the div tag content for comic display based on the comic object information. All the functions would be called using JavaScript/Ajax from the appropriate buttons. However, I was unsure about this design, so I asked a friend, who suggested that I use a model-view-controller design pattern, which I guess is popular in web development because its potential for separating all the various languages. After reading about this online, I have come up with a possible alternative design: Model: PHP, an array of comic strips, including name and ID number, active/inactive status, and current comic image URL/publishing date View: PHP generated HTML/CSS, as described before, a comic strip control panel for enabling/disabling and ordering, a vertically stacked list of comic strip names, constituent comic images, and individual control buttons, and a general set of buttons for controlling all enabled comics at once Controller: PHP/MySQL, a function to update order and active/inactive comic strip status, a function to update a given comic strip's current comic information, and a function to update all comic strips' current comic information at once As I understand it, the controller functions would be called upon user input into the view (clicking the buttons), which would then update the information stored in the model, and then somehow that would trigger a refresh of the view based on the new model information. I am worried I may have missed something in each of these designs (especially the first one), and unsure about the potential merits of one versus another. I was also wondering if it might be possible to combine the two somehow. Does anyone have any suggestions? Help would be much appreciated
  14. Hello, I am currently working on a PHP program that uses an RSS reader to update a MySQL database with comic information from various web comics. After some research, it seems like it would be a better idea to do this using a Cron job or something similar, as opposed to just a sleep loop within PHP. However, my script needs to pass information to itself from the last time it was run. I want it to compare information taken from each RSS page from the last time it ran to see if the page is updated, then only to add information to the database if it has indeed been updated. Unfortunately, I'm not sure how to do this/if this is even possible using a Cron job or Windows task scheduler (I am currently developing on Windows). Any idea how to solve this problem? I suppose I could just use a sleep loop as I was originally considering, but I have read in various locations that this is apparently a very glitchy and unreliable way of doing what I want to do, so I would like to avoid it if possible. Thanks in advance