opencartcustom-pages

foreach not working in opencart custom page


Am creating a page called no.tpl. Here am showing customer firstname and lastname in select dropdown.

here my code no.tpl

<select name="customer_id" id="customer" style="width: 325px;margin-bottom:10px" class="form-control">
    <?php foreach($customerData as $customer){ ?>
        <option value=<?php echo $customer['customer_id']?>><?php echo $customer['customer_name']?></option>
    <?php }?>
</select>

This is my controller code

    <?php

class ControllerSaleno extends Controller {
    private $error = array();
    public function index() {

           $data=$this->request->post;
           $customerId=$data['customer_id'];

        //$this->sendMail($customerId);

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        //$this->response->setOutput($this->load->view('default/template/product/no.tpl', $data));

        $this->response->setOutput($this->load->view('sale/no.tpl', $data));


        //$data['productsData']=$this->getProductData();
         $customers=$this->getCustomerData();

             $data['customerData']=array();


 foreach($customers as $customer)
{
   $data['customerData'][]=array(
'customer_id'=>$customer['customer_id'],
'customer_name'=>$customer['firstname'].' '.$customer['lastname']
);
   //var_dump($data['customerData']);
   //var_dump($customer['firstname']);
}


    }
    public function getCustomerData()
    {

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer order by firstname asc");
        return $query->rows;
    }

    public function sendMail($customerId)
    {

        $tempArray=array();

      $selectedCustomerId=$customerId;
            $query=$this->db->query("SELECT state FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
            $state=$query->row;





            $querySalesEmail=$this->db->query("SELECT email FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
            $salesPersonEmail1=$querySalesEmail->row;

            $salesPersonEmail= $salesPersonEmail1['email'];


 $queryCustomerName=$this->db->query("SELECT concat(firstname,' ',lastname) as name FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
$customerName=$queryCustomerName->row['name'];






    $To = "radhamani123@gmail.com";
    $Subject = "New Order From " . "'" . $customerName . "'" . date('m/d/Y h:i:s');
    $Headers  = "MIME-Version: 1.0\r\n";
    $Headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $Headers .= "From: No ORder<orders@milkymist.in>\r\n";

   $Message = '';


            $Message .= 'No Order Today';


    if(mail($To, $Subject, $Message, $Headers)){
        echo "success";



    }else{
        echo "failure";
    }




 $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        $this->response->setOutput($this->load->view('default/template/product/no.tpl', $data));

    }

}

Solution

  • in controller page i did some changes

    <?php
    
    class ControllerSaleno extends Controller {
        private $error = array();
        public function index() {
    
               $data=$this->request->post;
               $customerId=$data['customer_id'];
    
            //$this->sendMail($customerId);
    
            $data['column_left'] = $this->load->controller('common/column_left');
            $data['column_right'] = $this->load->controller('common/column_right');
            $data['content_top'] = $this->load->controller('common/content_top');
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header');
    
            //$this->response->setOutput($this->load->view('default/template/product/no.tpl', $data));
    
            $this->response->setOutput($this->load->view('sale/no.tpl', $data));
    
    
            //$data['productsData']=$this->getProductData();
             $customers=$this->getCustomerData();
    
                 $data['customerData']=array();
    
    
     foreach($customers as $customer)
    {
       $data['customerData'][]=array(
    'customer_id'=>$customer['customer_id'],
    'customer_name'=>$customer['firstname'].' '.$customer['lastname']
    );
       //var_dump($data['customerData']);
       //var_dump($customer['firstname']);
    }
    
    
             $data['NoOrder']=$this->url->link('sale/order/NoOrder', 'token=' . $this->session->data['token'] , 'SSL'); //added code
             $this->response->setOutput($this->load->view('sale/no.tpl', $data)); //added code
    
        }
        public function getCustomerData()
        {
    
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer order by firstname asc");
            return $query->rows;
        }
    
        /*no order*/
        public function NoOrder()
        {
    
    
            $this->sendMail();
            //$this->response->redirect($_SERVER['HTTP_REFERER']);
        }
    
        public function sendMail()
        {
    
            $tempArray=array();
    
          $selectedCustomerId=$customerId;
                $query=$this->db->query("SELECT state FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
                $state=$query->row;
    
    
    
    
    
                $querySalesEmail=$this->db->query("SELECT email FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
                $salesPersonEmail1=$querySalesEmail->row;
    
                $salesPersonEmail= $salesPersonEmail1['email'];
    
    
     $queryCustomerName=$this->db->query("SELECT concat(firstname,' ',lastname) as name FROM " . DB_PREFIX . "customer where customer_id=".$selectedCustomerId);
    $customerName=$queryCustomerName->row['name'];
    
    
    
    
    
    
    $To = "radhamani123@gmail.com";
       $Subject = "New Order From " . "'" . $customerName . "'" . date('m/d/Y h:i:s');
        $Headers  = "MIME-Version: 1.0\r\n";
        $Headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $Headers .= "From: No ORder<orders@milkymist.in>\r\n";
    
       $Message = '';
    
    
                $Message .= 'No Order Today';
    
    
        if(mail($To, $Subject, $Message, $Headers)){
            echo "success";
    
    
    
        }else{
            echo "failure";
        }
    
    
    
            //$this->response->setOutput(json_encode($result));
     $data['column_left'] = $this->load->controller('common/column_left');
            $data['column_right'] = $this->load->controller('common/column_right');
            $data['content_top'] = $this->load->controller('common/content_top');
            $data['content_bottom'] = $this->load->controller('common/content_bottom');
            $data['footer'] = $this->load->controller('common/footer');
            $data['header'] = $this->load->controller('common/header');
    
           $this->response->setOutput($this->load->view('sale/noorder.tpl', $data));
            //$this->response->setOutput($this->load->view('sale/no.tpl', $data));
    
        }
        /*no order*/
    
    }