0

I'am using ZEND1.12, and I want to store an image from a form to the database, and then display it on a CRUD table. Here is the code of the forms : Product.php

<?php

class Application_Form_Product extends Zend_Form {

protected $db;

public function __construct($db)
{
    $this->db = $db;

    // Don't forget to call the parent __construct. Ultimately
    // it is the parent __construct() that calls your init()
    // method that adds your elements 
    parent::__construct();
}

public function init()
{

    $this->addElement('select', 'category', array('label'=>'Category', 'multiOptions' => $this->buildMultiOptions()));

     $this->addElement('text', 'name', array('label' => 'Enter The Name:','required' => true));

     $this->addElement('select', 'warehouse', array('label'=>'Warehouse', 'multiOptions' => $this->buildMultiOptionsWarehouse()));
     $this->addElement('file', 'picture', array('label' => 'Enter The picture:','required' => true));
     $this->addElement('text', 'price', array('label' => 'Enter The Price:','required' => true));
   //  $this->addElement('file','file',array('label' => 'Add file'));

     $this->addElement('submit','submit',array('label' => 'Add Product'));
}

} And here is the controller with all Actions :

<?php

class ProductController extends Zend_Controller_Action {

public function init()
{
    /* Initialize action controller here */
}

public function indexAction()
{
      $categoryModel = new Application_Model_DbTable_Category();
      $warehouseModel = new Application_Model_DbTable_Warehouse();
      $productModel = new Application_Model_DbTable_Product(); 
      $products = $productModel->showProducts();


      $this->view->products = $products;
}

public function createAction()
{
    $db = $this->getInvokeArg('bootstrap')->getResource('db');
    $productForm = new Application_Form_Product($db);
       $request = $this->getRequest();
        if ($request->isPost()) {
             if ($productForm->isValid($request->getPost())) {
                 $productModel = new Application_Model_DbTable_Product();
                 $productModel->create();
                 $this->_redirect('product');
            }
                                }
             $this->view->productForm= $productForm;

}

public function deleteAction()
{
      $productModel = new Application_Model_DbTable_Product();
      $productModel->deleteProduct();
      $this->_redirect('product');
}

public function editAction()
{
      $db = $this->getInvokeArg('bootstrap')->getResource('db');
      $productForm = new Application_Form_Product($db);
      $request = $this->getRequest();
      $id = $request->getParam('id');
      $productModel = new Application_Model_DbTable_Product();
      $product = $productModel->fetchRow('id = '.$id);

      if ($request->isPost()) {
           $productModel->edit();
           $this->_redirect('product');
      }
      $this->view->product = $product;
      $this->view->productForm = $productForm;
}

} And here is the View :

</br>
 </br>
      <table class="table">
      <thead class="thead-dark">
        <tr>
          <th scope="col" class="mx-auto" style="width: 300px;">Name</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Category</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Warehouse</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Picture</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Price</th>
          <th scope="col" class="mx-auto" style="width: 300px;">Action</th>
        </tr>
      </thead>
     <?php 
          foreach ($this->products as $product) 
          {
                echo "<tr>";
                echo "<td>" . $product->name_product . "</td>";
                echo "<td>" . $product->name . "</td>";
                echo "<td>" . $product->warehouse_id . "</td>";
                echo "<td>" . $product->picture . "</td>";
                echo "<td>" . $product->price . "</td>";
                echo "<td colspan='2'><a href='" . $this->url(array('controller' => 'product', 'action' => 'edit', 'id' => 
                $product->id)) . "' type='button' class='btn btn-primary'>Edit</a>";
                echo " <a href='" . $this->url(array('controller' => 'product', 'action' => 'delete', 'id' => $product->id)) . "' onclick='return confirm(\"Do you really want to delete this contact?\");' type='button' class='btn btn-danger'>Delete</a></td>";
                echo "</tr>";
         }
   ?>
   </table>

And here is the model : DbTable : Product.php with all functions.

<?php

class Application_Model_DbTable_Product extends Zend_Db_Table_Abstract {

protected $_name = 'products';

public function create() {
      $front = Zend_Controller_Front::getInstance();
      $request = $front->getRequest();
      $data = array(
           'id' => $request->getPost('id'),
           'name_product' => $request->getPost('name'),
           'category_id' => $request->getPost('category'),
           'warehouse_id' => $request->getPost('warehouse'),
           'picture' => $request->getPost('picture'),
           'price' => $request->getPost('price')

      );
      $this->insert($data);
 }

  public function edit() {
      $front = Zend_Controller_Front::getInstance();
      $request = $front->getRequest();
      $data = array(
           'name_product' => $request->getPost('name'),
           'category_id' => $request->getPost('category'),
           'warehouse_id' => $request->getPost('warehouse'),
           'picture' => $request->getPost('picture'),
           'price' => $request->getPost('price'),
      );
      $where = array('id = ?' => $request->getParam("id"));
      $this->update($data, $where);
 }

 public function deleteProduct() {
      $front = Zend_Controller_Front::getInstance();
      $request = $front->getRequest();
      $where = array('id = ?' => $request->getParam("id"));
      $this->delete($where);
 }

 public function showProducts() {
      $select = $this->select()
               ->from('products')
               ->setIntegrityCheck(false)
               ->join('categorys', 'products.category_id =categorys.id', array('*'))
               ->columns(array('products.id'));
       $result = $this->fetchAll($select);  

if ($result !== null){

    return $result;
} else {
    echo "no records found";
}   
 }

 public function showProductsWarehouse() {
      $select = $this->select()
               ->from('products')
               ->setIntegrityCheck(false)
               ->join('warehouses', 'products.warehouse_id =warehouses.id', array('*'))
               ->columns(array('products.id'));
       $result = $this->fetchAll($select);  

if ($result !== null){

    return $result;
} else {
    echo "no records found";
}   
 }

}

  • It looks the product model is not appears on your question. – OO7 Apr 15 '20 at 23:42
  • Yes, sure I edited it, you can take a look, thank you. – Mugen solo Apr 15 '20 at 23:48
  • You are welcome. That seems you prefer to save post image to database directly, so that could be an overhead for both web server and database server. 1. Put the file into array. 2. Store into the database over database connection protocol. And when retrieve for view on client. 1. Retrieve from database over protocol as a query result. 2. Store into array as a column model and used by controller for prepare and render to client. But depending on the file size, if only1-2kb, than it still make sense, however it needs to pass proper image validation before been stored into database. – OO7 Apr 16 '20 at 00:43
  • Excuse me, I don't get it, can you be more specific about what should i do in the code exactly to store and display the image ? – Mugen solo Apr 16 '20 at 01:20
  • Google "mysql php blob example" and you find tons of help. And this one telling to display image from database: https://stackoverflow.com/questions/23842268/how-to-display-image-from-database-using-php – OO7 Apr 16 '20 at 18:04

1 Answers1

0

Get the image file for persistent blob column using getFiles and file_get_contents:

$rowData = [
       'name_product' => $request->getPost('name'),
       'category_id' => $request->getPost('category'),
       'warehouse_id' => $request->getPost('warehouse'),
       'picture' => file_get_contents($request->getFiles('picture')->toArray()['tmp_name']),
       'price' => $request->getPost('price')
];

getFiles reference: https://docs.zendframework.com/zend-http/request/

Display the image file from persistent blob directly.

jpg image:

echo '<td><img src="data:image/jpeg;base64,'.base64_encode( $product->picture ).'"/></td>';

png image:

echo '<td><img src="data:image/png;base64,'.base64_encode( $product->picture ).'"/></td>';

---==^^^==---

OO7
  • 660
  • 4
  • 10