Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Class variable and put it into value property textbox

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Class variable and put it into value property textbox

Postby rbaez » Mon Oct 22, 2012 12:18 pm

Hello what i want to do is to access a private variable in a php class and then using the value property of a textbox print it there. For example i have all this code:
Code: Select all
<?php
class ProductFinder extends \Configuration {
   private $idproducto;
   private $idmarca;
   private $idcategoria;
   private $producto;
   private $imagen;
   private $descipcion;
   private $url;
   private $destacado;
   private $idstatus;


        #Get the product id for update operation
   public function get_id_product()
   {   
      try
      {
         $my_con = Configuration::getInstance();
         $sql = 'SELECT * FROM productos WHERE idproducto = ? limit 0,1';
         $query = $my_con->_cn->prepare($sql);
         $query->execute(array($_GET['idproducto']));
         
         $row = $query->fetch(PDO::FETCH_OBJ);
         
         $this->idmarca = $row->idmarca;
         $this->idcategoria = $row->idcategoria;
         $this->producto = $row->producto;
         $this->imagen = $row->imagen;
         $this->descripcion = $row->descripcion;
         $this->url = $row->url;
         $this->destacado = $row->destacado;
         $this->idstatus = $row->idstatus;
      }
      catch(PDOException $e)
      {
         echo 'ERROR ' . $e->getMessage();
      }
   }

}
?>

Then in the html form i have this.
#load the get_id_product function
$producto = new ProductFinder();
$producto->get_id_product();

<?php
include '../../classes/configuration.class.php';
include '../../classes/productfinder.class.php';

$action = isset($_POST['action']) ? $_POST['action'] : "";
if($action == 'actualizaProducto')
{
   $actualiza = new ProductFinder();
   $actualiza->update_products();
}
?>
<div class="container">
   <form id="actualiza-productos" method="post">
       <label>Marca</label>
       <select id="marca" name="marca"></select>
        <label>Categoria</label>
        <select id="categoria" name="categoria"></select>
        <label>Producto</label>
        <input type="text" id="producto" name="producto" value="" />
        <label>Imagen</label>
        <input type="file" id="imagen" name="imagen" />
        <label>Descripcion</label>
        <textarea id="descripcion" name="descripcion" rows="5"></textarea>
        <label>Url</label>
        <input type="text" id="url" name="url" />
        <label>Destacado</label>
        <select id="destacado" name="destacado"></select>
        <label>Status</label>
        <select id="status" name="status"></select><br />
        <input type="hidden" name="action" value="actualizaProducto" />
        <input type="submit" value="Guardar Producto" class="btn btn-inverse" />
    </form>
</div>

And what i want is that in every textbox or select the value property its filled with the variable like this:
 For example:

 <?php echo $producto->idproducto; ?>



But im getting and error, and i dont know what else to do.

Thanks in advance.
rbaez
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Oct 22, 2012 12:10 pm

Re: Class variable and put it into value property textbox

Postby Derezo » Mon Oct 22, 2012 12:55 pm

You need a get/set method for each private variable you need to access from outside of the class.

For example.
Code: Select all
public function getProducto() { return $this->producto; }


Then your html
Code: Select all
 <input type="text" id="producto" name="producto" value="<?php echo isset($actualiza) ? $actualiza->getProducto() : ""; ?>" />


This will check if $actualiza has been set, and if it has it prints the results of getProducto, if it has not it prints nothing.
Derezo
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Oct 22, 2012 12:48 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.