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?
See Question&Answers more detail:
os