Ver Mensaje Individual
  #6 (permalink)  
Antiguo 08/05/2008, 07:43
zsamer
 
Fecha de Ingreso: noviembre-2003
Mensajes: 798
Antigüedad: 21 años, 3 meses
Puntos: 8
Re: ¿como pasar $_POST a una clase?

Cita:
Iniciado por hugosalcedo Ver Mensaje
Hola emiliodeg, muchas gracias por la respuesta, cambie el codigo de acuerdo a tu sugerencia y funciono.

Este es el codigo de ControlArea.php

Código PHP:
<?php 
    
function __autoload($clase) {
                            include 
$clase.'.php';
                            }

class 
ControlArea{
    
    
public static function 
Main()  {
             switch(
$_POST['op']){
                  case 
'buscar'self::Buscar();
                 }
     }

public static function 
Buscar()
        {
    
$busqueda $_POST['busqueda'];
    
    
$are = new Area(); 
    
$Resultado $are->Buscar($busqueda);
    
$row $Resultado->num_rows;
 
    
    if (
$row 0)    
        {    
    
$contenido ="<div id='tablabuscar'><table>".
                
"<tr>".
                
"<th><b>Codigo</b></th>".
                
"<th ><b>Nombre</b></th></tr>";        
    while(
$Fila $Resultado->fetch_assoc())
            {
    
$contenido .=    "<tr><th>".
            
"<a href='?op=Editar&Id=".$Fila["cod_are"]."'>".
        
$Fila["cod_are"]."</a></th><td class='clsTxtCelda'>".
        
$Fila["nom_are"]."</td></tr>";
            }
        }    
    
$contenido .= "</table>"
    echo 
$contenido;
        
        }
 }

ControlArea::Main();

?>

la otra pregunta es: ¿COMO logro mostrar los resultados en la vista y no en el controlador ?

Muchas gracias Emilio, he aprendido bastante en este foro.
Excelente pregunta , pensando en MVC tendrás que tener una clase ViewEngine:



Código PHP:
<?php
class View
{
    private 
$vars = array();
    
    const 
SITE_PATH ='';
    
    const 
VIEW_EXTENSION 'phtml';
    
    const 
SITE_PATH ='';
    
    const 
DIRSEP ='/';
    
    public function 
__construct()
    {}

    public function 
__get($key)
    {
        if (
'_' != substr($key01) && isset($this->vars[$key])) {
            return 
$this->vars[$key];
        }

        return 
null;
    }
    
    public function 
__set($key$val)
    {
        if (
'_' != substr($key01)) {
            
$this->assign($key$val);
            return 
true;
        }
        throw new 
Exception('Setting private or protected class members is not allowed'$this);
    }

    public function 
__isset($key)
    {
        if (
'_' != substr($key01)) {
            return isset(
$this->vars[$key]);
        }

        return 
false;
    }
    
    
/**
     * Allows unset() on object properties to work
     *
     * @param string $key
     * @return void
     */
    
public function __unset($key)
    {
        if (
'_' != substr($key01) && isset($this->vars[$key])) {
            unset(
$this->vars[$key]);
        }
    }
    
    public function 
assign($varname$value$overwrite=false) {
        if (isset(
$this->vars[$varname]) == true AND $overwrite == false) {
            throw new 
Exception('Unable to set var `' $varname '`. Already set, and overwrite not allowed.'E_USER_NOTICE);
            return 
false;
        }

        
$this->vars[$varname] = $value;
 
        return 
true;
    }

    public function 
remove($varname) {
        unset(
$this->vars[$varname]);
        return 
true;
    }

    public function 
render($name) {
        
$path self::SITE_PATH 'views' self::DIRSEP $name '.' self::VIEW_EXTENSION;
        if (
file_exists($path) == false) {
            throw new 
Exception('View `' $name '` does not exist.'E_USER_NOTICE);
            return 
false;
        }

        
// Load variables
        
foreach ($this->vars as $key => $value) {
            
$this->{$key} = $value;
        }
        
        
ob_start();
        include (
$path);
        
$output ob_get_contents();
        
ob_get_clean();
        return 
$output;
    }
}
uso en el controlador:
Código PHP:
# Load View object
$view = new View();
$view->assign('first_name''Andrés');

//otra forma de setear variables a la vista
$view->first_name 'Andrés';
print 
$view->render('sample'); 
Vista sample.phtml
Código PHP:
Hello from the View, <?php echo $this->first_name?>!
Tu vista tiene que estar en una carpeta views en tu proyecto.

Te recomiendo usar un Framework ya que trae todas estas prácticas MVC y muchísimo más, usa Zend_Framework, en mi firma encontrarás tutoriales.


Saludos.

Última edición por zsamer; 08/05/2008 a las 20:47