jueves, 4 de octubre de 2012

Archivos MVC

La ingeniera nos dejo archivos los cuales deben ser pegados en

C.\xampp\htdocs\xampp

para que puedan ser visualizados despues en el navegador colocando

localhost\index

loas archivos son:
 INDEX:  Este llama a la funcion model.php y ejecuta el view.php


<?php
// Requiring the model
require_once('model.php');

// Retrieving the list of posts
$posts = getAllPosts();

// Requiring the view
require('view.php');
?>

INDEX2

Este lo que hace es guardar el nombre , año en la tabla correspondiente a la Base de datos y lo muestra en View.php


<?php

$nombre = $_GET["fname"];

$id = $_GET["age"];
$accion = $_GET["accion"];
echo $nombre;
echo "<BR>";
echo $id;
echo "<BR>";
echo $accion;
echo "<BR>";

require_once('model.php');
 GuardarDatos($nombre, $id);

// Retrieving the list of posts
$posts = getAllPosts();


// Requiring the view
require('view.php');
?>

MODELO:

Este tien funciones de conectarce a la base de datos y de mandar los datos a l funcion Guardardatos


<?php
function conexion()
{
  $link = mysql_connect('localhost', 'root', '');
  mysql_select_db('prueba', $link);
return $link;
}
function getAllPosts()
{
  // Connecting, selecting database
  $link = conexion();

  // Performing SQL query
  $result = mysql_query('SELECT id, descripcion FROM noticias', $link);

  // Filling up the array
  $posts = array();
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  {
     $posts[] = $row;
  }

  // Closing connection
  mysql_close($link);

  return $posts;
}
function GuardarDatos($nombre, $id)



VISTA:

Aca se la parte visual para el usuario donde se muestra en la pantalla
<html>
  <head>
    <title>List of Posts</title>
  </head>
  <body>
    <h1>List of Posts</h1>
    <table>
      <tr><th>id</th><th>Descripcion</th></tr>
<?php foreach($posts as $post): ?>
    <tr>
        <td><?php echo $post ['id'] ?></td>
        <td><?php echo $post ['nombre'] ?></td>
    </tr>
<?php endforeach;?>
    </table>

<form action="index2.php" method="get">
Nombre: <input type="text" name="fname" />
id: <input type="text" name="age" />
<input type="hidden" name="accion" value="Guardar" />
<input type="submit" />
</form> 
  </body>
</html>

CONTROLADOR:
Lo que hace es verificar el usuarioy o password ingresados para conectarce a la base de datos

<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);
 
// Performing SQL query
$result = mysql_query('SELECT date, title FROM post', $link);
 
// Filling up the array for the view
$posts = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
  $posts[] = $row;
}
 
// Closing connection
mysql_close($link);
 
// Requiring the view
require('view.php');


 EJEMPLO: 
Aca presentamos el ejemplo que dejo la ingeniera donde se esta llamando a los archivos modelo, vista,controlador, index1 e index2.

<?php
// Connecting, selecting database
$link = mysql_connect('localhost', 'root', 'rootpass');
mysql_select_db('prueba', $link);
 
// Performing SQL query
$result = mysql_query('SELECT id,nombre FROM persona', $link); 
?>

<html>
  <head>
    <title>Lista de Personas</title>
  </head>
  <body>
   <h1>Lista de Personas (Ejercicio 1)</h1>
   <table>
     <tr><th>Date</th><th>Title</th></tr>
<?php

// Printing results in HTML
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "<tr>";
    printf("<td> %s </td>", $row['id']);
    printf("<td> %s </td>", $row['nombre']);
    echo "</tr>";
}
?>

    </table>
  </body>
</html>
<?php

// Closing connection
mysql_close($link);
?>

No hay comentarios:

Publicar un comentario