0

EDIT: My below example is more of a post-redirect-get than a MVC

I am reading a lot about correct structure for my page, MVC pattern, frameworks etc. and yet I am confused which parts of my page best fit under the description of model, of view and which of controller. Now before you downvote I did a lot of research already to separate my logic and make my simple page, I just need a confirmation that I am doing it right, what to fix/separate, which page is what according to MVC and where would I link or include index.php? I am not asking much I hope just for a quick glance at my code.

I will provide 3 different pages I built in order they are initialized as an example:

html form, also displays processed data user starts here:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Vaški gozd</title>
<link href="../html/css/base.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <?php if(!$_POST and $poskodbe != '0') { ?>
    <p>Presenetil<?php text($spol); ?> te je <?php text($monster); ?> !</p>
<form action='../php/gozd.php' method='post'>
    <input type='submit' name='action' value='Napadi' /> ali 
    <input type='submit' name='action' value='Pobegni' />
            <input type='hidden' name='monster' value= '<?php text($monster); ?>' />
</form>
    <?php } 

    else if ($poskodbe == '0'){text($moznost); ?> 
    <p><a href='../php/start.php'>Odpravi se proti domu</a></p> 
    <?php } 

    else { ?>        
    <ul><?php foreach ($combat as $turns => $i) { ?>
        <li> <p><strong><?php text($i['napadalec']); ?></strong>
             <?php text(' napade '); ?><strong><?php text($i['branilec']); ?></strong>
             <?php text(' in mu napravi poškodbe za ') ?><strong><?php text($i['damage']); ?></strong>
             <?php text(' točk zdravja '); } ?> </p></li> 
    </ul>

    <?php if(isset ($zmaga)) { ?> 
    <p>Pregnal si <strong><?php text($monster_ime); ?></strong>! 
       V naglici je za seboj pustil <strong><?php text($cekini); ?></strong> cekinov, ki jih seveda pobereš.</p>
    <p><a href='../php/gozd.php'>Raziskuj dalje</a></p>
    <?php } ?>

    <?php if(isset ($zguba)) { ?>
    <p>Podlegel si poškodbam <strong><?php text($monster_ime); ?></strong>.</p>
    <?php } ?>

    <p><a href='../php/start.php'>Odpravi se proti domu</a></p>
    <?php } ?>        

</body>

</html>

php that processes data and returns results:

<?php

session_start();
include 'config.php';
include 'stats.php';

$igralec_ime = $_SESSION['username'];
$_SESSION['poskodbe'] = ($poskodbe = prikazi_stat('curhp', $igralec_ime));

if ($poskodbe == '0') {$_SESSION['moznost'] = ($moznost = 'Tvoje zdravje je resno ogroženo, vrni se domov!');}

else {


if ($_POST) {


if($_POST['action'] == 'Napadi') {


    $igralec = array (
                      'ime'         => $igralec_ime,
                      'napad'   => prikazi_stat('ofe',$igralec_ime),
                      'obramba' => prikazi_stat('def',$igralec_ime),
                      'curhp'       => prikazi_stat('curhp',$igralec_ime)
                     );

    $monster_ime = $_POST['monster'];
    $monster = array (
                      'ime'         => $monster_ime,
                      'napad'   => prikazi_monster_stat('ofe',$monster_ime),
                      'obramba' => prikazi_monster_stat('def',$monster_ime),
                      'curhp'       => prikazi_monster_stat('maxhp',$monster_ime)
                     );       


    $combat = array();
    $turns = 0;     
    while($igralec['curhp'] > 0 && $monster['curhp'] > 0) {


        if($turns % 2 != 0) {
            $napadalec = &$monster;
            $branilec = &$igralec; } 
        else {
            $napadalec = &$igralec;
            $branilec = &$monster; }


        $damage = 0;    
        if($napadalec['napad'] > $branilec['obramba']) {
            $damage = $napadalec['napad'] - $branilec['obramba']; }


        $branilec['curhp'] -= $damage;
        $combat[$turns] = array(
            'napadalec' =>  $napadalec['ime'],
            'branilec'  =>  $branilec['ime'],
            'damage'    =>  $damage
                               );


        $turns++; }


    update_stat('curhp',$igralec_ime,$igralec['curhp']);

    if($igralec['curhp'] > 0) {
        update_stat('cek',$igralec_ime,prikazi_stat('cek',$igralec_ime)+ prikazi_monster_stat('cek',$monster_ime)); 
        $zmaga = 1;
        $cekini = prikazi_monster_stat('cek',$monster_ime); }

    else {
    if ($igralec['curhp'] <0) {update_stat('curhp', $igralec_ime, '0'); }
    $zguba = 1; } }

else {

    header('Location:../php/start.php');
    exit; 
    } 

} 

else {

$query = sprintf("SELECT ime, spol FROM monsters ORDER BY RAND() LIMIT 1");
$result = mysql_query($query);
list($monster, $spol) = mysql_fetch_row($result); 

}   

} 

$_SESSION['moznost'] = $moznost;
$_SESSION['monster'] = $monster;
$_SESSION['spol'] = $spol;
$_SESSION['poskodbe'] = $poskodbe;
$_SESSION['combat'] = $combat;
$_SESSION['turns'] = $turns;
$_SESSION['zmaga'] = $zmaga;
$_SESSION['zguba'] = $zguba;
$_SESSION['monster_ime'] = $monster_ime;
$_SESSION['cekini'] = $cekini;
$_SESSION['post'] = $_POST;

header('Location:../php/gozd_kontroler.php',true,303);
exit; 
?>

php page that has included html page from earlier and to which my data manipulating php script redirects to display results:

<?php

session_start();

include 'razno.php';


$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$poskodbe = $_SESSION['poskodbe'];
$moznost = $_SESSION['moznost'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$monster_ime = $_SESSION['monster_ime'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];

include '../html/gozd.html';
?>

Which page is which acording to model-view-controller? Am I doing it right at all? Where would I link or include index.php?

Matic
  • 39
  • 1
  • 6
  • 1
    I would really start your foray into MVC by looking at an existing system. What you have above is not MVC for a variety of reasons. Maybe check out CodeIgniter, they have some good tutorials. – zajd Aug 05 '13 at 18:57
  • @zjd reason I don't use existing frameworks is because it would be an overkill for my simple project and a lot more work getting used to them and understanding them. By doing my own simple MVC pattern I am also learning the basics. IMH I only need MVC so that my view is not located in my model and doesn't resend data on refresh and such – Matic Aug 05 '13 at 19:10
  • [tereško](http://stackoverflow.com/users/727208/teresko) has [provided some helpful links](http://stackoverflow.com/questions/16356420/mvc-for-advanced-developers/16356866#16356866) to help people get started with MVC. – Benny Hill Aug 05 '13 at 19:11

1 Answers1

2

Which page is which according to model-view-controller?

A page is a page. It isn't part of MVC, it is built using MVC.

The view is the class that (given some data) generates whatever is sent to the client (usually HTML).

The model is the class that operates on the data. It talks to your database or other data store.

The controller is the class that looks at the URL, decides which models and views are right for it and exchanges data between the submitted data, the models and the view.

Am I doing it right at all?

No

Where would I link or include index.php?

Your index page should just bootstrap your controller class.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks for the answer, looks like I won't need to implement MVC for my page because it would be an overkill, I will just build simple post-redirect-get pages. Though what can I do with index.php if I will be using only such pages? – Matic Aug 05 '13 at 20:34
  • @Quentin Good simple answer that's really readable mate :) +1 – Fluffeh Aug 06 '13 at 09:21