How to send array from Block and get in Phtml file in magento 2. i am trying to return array from Block and wants to get array in template Phtml file. But they do not show my data.
you need to define any public function in your Block as
public function getMyCustomData(){
$arr = [];
$arr[] = // my data;
...
...
return $arr;
}
than in your phtml file you can call this function by
$myCustomArr = $block->getMyCustomData();
Related
So i am changing my project from core PHP to Codeigniter. I have a edit for in which their are 3 input type file. Now i want to run my query according to these inout file. like if input file 1 is empty than don't update value else update. I did it in core PHP like this:
The name of input type file are: image1, image2, image3
mysqli query in core PHP:
if(!empty($photo1))
{
$p1=str_replace("-","",time().$photo1);
move_uploaded_file($_FILES['photo1']['tmp_name'], $folder.$p1);
$query .=",`image1`='$p1'";
}
if(!empty($photo2))
{
$p2=str_replace("-","",time().$photo2);
move_uploaded_file($_FILES['photo2']['tmp_name'], $folder.$p2);
$query .=",`image2`='$p2'";
}
if(!empty($photo3))
{
$p3=str_replace("-","",time().$photo3);
move_uploaded_file($_FILES['photo3']['tmp_name'], $folder.$p3);
$query .=",`image3`='$p3'";
}
Now how to do this in codeigniter model:
controller
public function store_ad()
{
$id=$this->input->post('id');
$d_email=$this->session->userdata('dealer_email');
$p_type=$this->input->post('property_type');
$p_subtype=$this->input->post('property_subtype');
$p_for=$this->input->post('p_for');
$p_name=$this->input->post('p_name');
$p_price=$this->input->post('p_price');
$state=$this->input->post('state');
$city=$this->input->post('city');
$loc=$this->input->post('location');
$pincode=$this->input->post('pincode');
$about=$this->input->post('p_about');
$stat=$this->input->post('status');
$config['upload_path'] = './images/properties';
$config['allowed_types']='gif|png|jpeg|jpg';
$this->load->library('upload',$config);
if($this->upload->do_upload('image1'))
{
$data1=$this->upload->data();
$img1=$data1['raw_name'].$data1['file_ext'];
}
if($this->upload->do_upload('image2'))
{
$data2=$this->upload->data();
$img2=$data2['raw_name'].$data2['file_ext'];
}
if($this->upload->do_upload('image3'))
{
$data3=$this->upload->data();
$img3=$data3['raw_name'].$data3['file_ext'];
}
}
How to crearte model according to the non empty input file. I am storing the name of the file in database
Inside your method in the model class check if the files you want to upload is empty or not e.g
public function update($data_array, $img1, $img2, $img3){
if(!empty($img1)){
$data_array['img_column1'] = $img1;
}
if(!empty($img2)){
$data_array['img_column2'] = $img2;
}
if(!empty($img3)){
$data_array['img_column3'] = $img3;
}
return $this->db->insert('YOUR_TABLE_NAME', $data_array);
}
Hope this helps....
I have written code for accessing the array from the controller to view. but i am not able to access data on view.
codes are given below.
<?php
public function Student(){
$exam_no_students = array();
$exam_centers['no_of_centers']= $this->DB1->count_centers();
$exam_centers1= $this->DB1->count_centers();
foreach ($exam_centers1 as $center_code => $center_details){
$center_code = array('center_id' =>$center_details->center_id);
$center_table='exam_center';
$center_full_details =$this->DB1->getArrayWhereResult($center_table,$center_code);
foreach ($center_full_details as $full => $full_de) {
$fullstudent = array('center_f_id' =>$full_de->center_id,
'center_f_name' =>$full_de->exam_center,
'center_f_addres' =>$full_de->exam_center_address ,
'center_f_city' =>$full_de->village ,
'center_f_count' =>$center_details->count);
}
array_push($exam_no_students, $fullstudent);
}
}
}
$abcd['assignment']=$exam_no_students;
$this->load->view('dissa');
$this->load->view('dissa',$abcd);
I am not able to access variable assignment at the view.
Message: Trying to get property of non-object please give me suggestion.
Thanks.
I want to create a pdf of first 300 orders in magento. I want a functionality in which i will get first 300 orders and print their images(each order has different image) in a pdf. So how can i implement this functionality in magento. Is there any extension for that?
Take a look at /app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php
public function pdfinvoicesAction(){
$orderIds = $this->getRequest()->getPost('order_ids');
$flag = false;
if (!empty($orderIds)) {
foreach ($orderIds as $orderId) {
$invoices = Mage::getResourceModel('sales/order_invoice_collection')
->setOrderFilter($orderId)
->load();
if ($invoices->getSize() > 0) {
$flag = true;
if (!isset($pdf)){
$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
} else {
$pages = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoices);
$pdf->pages = array_merge ($pdf->pages, $pages->pages);
}
}
}
if ($flag) {
return $this->_prepareDownloadResponse(
'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
'application/pdf'
);
} else {
$this->_getSession()->addError($this->__('There are no printable documents related to selected orders.'));
$this->_redirect('*/*/');
}
}
$this->_redirect('*/*/');
}
From the above function you could assign the first 300 order ids to $orderIds (or modify Mage::getResourceModel('sales/order_invoice_collection to get the first 300 records)
See magento orders list query
Changes :
public function pdfinvoicesAction(){
$orderIds = $this->getRequest()->getPost('order_ids');
To (something like)
public function pdfinvoices($orderIds){
$orderIds = (array) $orderIds; // first 300 record ids
Change line to save pdf to file
return $this->_prepareDownloadResponse(
'invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').'.pdf', $pdf->render(),
'application/pdf'
);
To
$pdf->render();
// use the order_id for the pdf name like
$pdf->save("{$orderId}.pdf");
see Error in generated pdf file using zend_pdf under Magento
You could also delete the $this->_redirect('//')
I have a database table called input_types with a various input types:
[id] [input_type_Name]
1 text
2 radio
3 checkbox
4 select
... ...
I want to retrieve the names from the table and put them in an array so that I can then use form_dropdown to show them to the user in a dropdown.
The problem with how I'm doing it now is that I create optiongroups.
How I do it now:
Model
function get_inputTypes() {
$this->db->select('input_type_name');
$query = $this->db->get('input_types');
if($query->num_rows() > 0) {
return $query->result_array();
}
else {
return false;
}
}
Controller
$results = $this->survey_model->get_inputTypes();
$data['inputTypes'] = $results;
View
<label for="inputType">Input type:</label>
<?php echo form_dropdown('inputType', $inputTypes); ?>
This however doesn't create the desired effect. My dropdown gets populated, but because I have a multidimensional array the dropdown has optgroups.
I just want to have my selected data in a simple array. Why is this so freaking hard in CodeIgniter and php in general.
C# is much easier :/
Solution
The solution is very simple, use a foreach to loop through the multidimensional array in the model:
foreach($query->result() as $input_type) {
$data[] = $input_type->input_type_name;
}
return $data;
What do you mean by "This however doesn't create the desired effect" ?
Make sure you pass the $data to your view:
$this->load->view('viewname', $data);
The solution is very simple, use a foreach to loop through the multidimensional array in the model:
foreach($query->result() as $input_type) {
$data[] = $input_type->input_type_name;
}
return $data;
I have a situation where I want a set of users (employees) to be able to create a node, but to replace the uid (user ID) with that of the users profile currently displayed.
In other words, I have a block that that calls a form for a content type. If an employee (uid = 20) goes to a clients page (uid =105), and fills out the form, I want the uid associated with the form to be the client's(105), not the employee's.
I'm using arg(1) to grab the Client's uid - here is what I have..
<?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
if (arg(0) == 'user' && is_numeric(arg(1))) {
$form['#submit'][] = 'addSR_submit_function';
}
}
function addSR_submit_function($form, $form_state) {
$account = user_load(arg(1));
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}
?>
The form is loading in the block, but when submitted, is still showing the employee uid. I don't want to use hook_form_alter because I don't want to modify the actual form, because clients can fill out the form directly, in this case, I don't want to modify the form at all.
I'm also ashamed that I'm putting this in a block, but I couldn't think of a way to put this in a module, so any suggestions on that would also be appreciated...
To create your form in a block, you could use the formblock module. Especially if you are not used to use the Drupal API. Then all that's left if to add your own submit handler to the form. This is a piece of code that is run, when the form is submitted. You only want to do this on clients pages so you would do that using the hook_form_alter function.
/**
* Hooks are placed in your module and are named modulename_hookname().
* So if a made a module that I called pony (the folder would then be called
* pony and it would need a pony.info and pony.module file I would create this function
*/
function pony_form_service_request_node_form_alter(&$form, $form_state) {
// Only affect the form, if it is submitted on the client/id url
if (arg(0) == 'client' && is_numeric(arg(1))) {
$form['#submit'][] = 'pony_my_own_submit_function';
}
}
function pony_my_own_submit_function($form, &$form_state) {
$account = user_load(arg(1));
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}
The idea behind this code, is to only alter the form when the condition is met - that it is submitted on a client page. I guessed that the arg(0) would be client so if it's something else you would need to change that of cause. We only need to add a submit function, since what we want is to change the values if the form has passed validation.
Then if that is the case our 2nd function is run, which does that actual alteration of the values.
PHP blocks are bad. You can put them in a module.
function hook_block($op, $delta = 0) {
// Fill in $op = 'list';
if ($op == 'view' && $delta = 'whatever') {
$account = user_load(arg(1));
$node = array('uid' => $account->uid, 'name' => $account->name, 'type' => 'service_request', 'language' => '', '_service_request_client' => $account->uid);
$output = drupal_get_form('service_request_node_form', $node);
// Return properly formatted array.
}
}
Additionally, you want a form_alter just to enforce the values. It's ugly but it works.
function hook_form_service_request_node_form_alter(&$form, $form_state) {
if (isset($form_state['node']['_service_request_client'])) {
$form['buttons']['submit']['#submit'] = array('yourmodule_node_form_submit', 'node_form_submit');
}
}
function yourmodule_node_form_submit($form, &$form_state) {
$account = user_load($form_state['node']['_service_request_cilent'])l
$form_state['values']['uid'] = $account->uid;
$form_state['values']['name'] = $account->name;
}