Can you export excel spreadsheets based on a selection in Laravel? - php

I have a radio group that filters my table to show whether items are active, inactive or both. There is also a button that I am using to export an excel document of the table. My current button downloads all the records. What I want is to export the document based on the filter so if active is selected and showing and the download button is pressed, only active records will be exported
My code:
Controller:
public function filter()
{
$energy = Energy::where('isredundant', 0)->paginate(10);
return view('admin.staff', ['staff1' => $staff1]);
}
public function filtered()
{
$energy = Energy::where('isredundant', 1)->paginate(10);
return view('admin.staff', ['staff1' => $staff1]);
}
public function export()
{
return Excel::download(new UsersExport, 'allrecords.xlsx');
}
public function export()
{
return Excel::download(new UsersExport, 'AllReport.xlsx');
}
public function exportactive()
{
return Excel::download(new UsersExportactive, 'ActiveRecords.xlsx');
}
public function exportredundant()
{
return Excel::download(new UsersExportredundant, 'RedundantRecords.xlsx');
}
my button:
<div> <a class="btn btn-warning" href="/export_excel/excel">Generate Report</a></div>
I tried to use 3 different buttons but that just looks messy. I'm sure there's an easier method

you can try this way
in controller
public function exportactive(Request $request)
{
return Excel::download(new UsersExportactive($request->ids), 'ActiveRecords.xlsx');
}
in UsersExportactive() class
private $ids;
public function __construct($ids)
{
$this->ids = $ids;
}
and do a query for that for like that in UsersExportactive() class
Model::whereIn('id',$ids)->get();
and in view use your export button as a submit button like below
<form>
<input type="checkbox" name="ids[]" id="1"/>active
<input type="checkbox" name="ids[]" id="2"/>inactive
<input type="submit" id="download" value="download Excel"/>
</form>

<input type="radio" name="download" id="x86"/>active<br />
<input type="radio" name="download" id="x64"/>inactive<br />
<input type="button" id="download" value="download"/>
var radio_x86 = document.getElementById('x86');
var radio_x64 = document.getElementById('x64');
var button = document.getElementById('download');
button.onclick = downloadFile;
function downloadFile() {
if (radio_x86.checked) {
window.open("/app/test");
} else if (radio_x64.checked) {
window.open("/app/test1");
} else {
alert("Please check one of the options first.");
}
}

Related

Creating a button to save the typed data by user in database when the button is clicked

How to create a "Request" button for search box? The letters typed in search box should be saved in database when request button is clicked. I am using CodeIgniter.
Your view
<form method="POST" action="your-action" >
<input type="text" name="search" />
<input type="submit" value="Search" />
</form>
Controller
public function function_name()
{
$data = $this->input->post('search');
$result = $this->model_name->function_name($data);
//$result is the result/output of your search
}
MODEL
public function function_name()
{
$this->db->insert('table-name', $data);
$this->db->select('*');
$this->db->from('table-name');
$this->db->like('column-name', $data);
return $this->db->get()->result_array(); //you can also use row_array() for one record only

Redirect on different pages against different buttons (Laravel 5.3)

Here its my view i have 2 buttons one is save and exit and other is save and stay Here
View
<button type="submit" name="submit_1"> Submit and Exit</button>
<button type="submit" name="submit_2">save and stay Here</button>
Controller
public function store(Request $request)
{
$data['name'] = $request->input('name');
$data['adr'] = $request->input('adr');
$myuser = Data::create($data);
if(Input::get('submit_1')) {
return redirect()->route('myuser.index');
} elseif(Input::get('submit_2')) {
return redirect()->route('myuser.create');
}
}
I tried this but its not working for me kindly help me
you can try as:
<input type='submit' name='submit' value='submit_1'>
<input type='submit' name='submit' value='submit_2'>
Try this:
View
<button type="submit" name="submit_1" value="submit_1"> Submit and Exit</button>
<button type="submit" name="submit_2" value="submit_2">save and stay Here</button>
Controller
public function store(Request $request)
{
if($request->get('submit_1') == "submit_1") {
return redirect()->route('myuser.index');
} else if($request->get('submit_2') == "submit_2") {
return redirect()->route('myuser.create');
}
}

How to send id from view to Controller CodeIgniter via form_open_multipart

I am trying to an send id from a view to controller in CodeIgniter.My requirement is to switch the functions based on button id.Here is my HTML code.
View HTML
<?php echo form_open_multipart('upload_control/switch_load','id="bt_addImage"');?>
<input id= "bt_addImage" type="submit" value="Add Image" /> <br>
<?php echo form_open_multipart('upload_control/switch_load','id="bt_chooseImage"');?>
<input type="submit" id="bt_chooseImage" value="Submit"/><br>
Upload_control.php Code
public function switch_load($id)
{
if($id == "bt_addImage")
{
do_loadcategories();
}
else
{
do_upload();
}
}
public function do_loadcategories()
{
//code list categories
}
public function do_upload()
{
//code to upload
}
is it corrrect?
or
have any other way to do this?
help me to solve the issue.
In View
$hiddenFields = array('id' => 'bt_addImage'); # add Hidden parameters like this
echo form_open_multipart('upload_control/switch_load', '', $hiddenFields);
In Controller
public function switch_load()
{
$id = $this->input->post('id');
if($id == "bt_addImage")
{
do_loadcategories();
}
else
{
do_upload();
}
}
view look like this
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/upload_control/switch_load">
<input type="hidden" name="id" value="bt_addImage" /> # hidden filed.
Codeigniter Form Helper
Change from
if($id == "bt_addImage")
Into
if($this->input->post('id') == "bt_addImage")

Form Validation & Re-Populating Field Values After Redirect Error; Validation Works, But Values Doesn't (CodeIgniter 3)

I am trying to keep field values after validation errors on redirect. Validation error shows fine but I keep loosing field values
Form:
<form>
<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo set_value('v_item_title'); ?>" />
<input type="submit" value="Submit">
</form>
Controller:
$this->load->helper('security');
$this->load->library('form_validation');
$this->form_validation->set_rules('v_item_title', 'Property title', 'trim|required|xss_clean|max_length[100]');
if($this->form_validation->run() == FALSE)
{
$this->session->set_userdata('validation_errors', validation_errors());
$this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
$this->session->set_flashdata('v_item_title', $this->input->post());
$this->session->flashdata('v_item_title');
redirect('user/dashboard#new');
} else {
Redirects to
public function dashboard()
{
if($this->session->userdata('is_logged_in')){
$data['validation_errors'] = $this->session->userdata('validation_errors');
$data['v_item_title'] = $this->session->userdata('v_item_title');
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
}else{
You have form validation wrong on controller you have your success info on false area it should be in true area like
http://www.codeigniter.com/user_guide/libraries/form_validation.html
Not sure what your controller name is so I named example login
application > controllers > Login.php
<?php
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('security');
$this->load->library('session');
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Login';
$this->form_validation->set_rules('name_check_box', '', 'trim|required|callback_checkbox');
$this->form_validation->set_rules('v_item_title', 'Property title', 'trim|required|xss_clean|max_length[100]');
if($this->form_validation->run() == FALSE) {
// Load the view
$this->load->view('header', $data);
$this->load->view('login', $data);
$this->load->view('footer');
} else {
$data = array(
'is_logged_in' => true,
'validation_errors' => validation_errors(),
'v_item_title' => $this->input->post('v_item_title')
);
$this->session->set_userdata($data);
// data will automatically delete themselves after redirect
$this->session->mark_as_flash('validation_errors');
// You could set the title in session like above for example
$this->session->set_flashdata('v_item_title', $this->input->post('v_item_title'));
// Echo flash data on view file?
// $this->session->flashdata('v_item_title');
// Dashboard will be a separate controller
// application > controllers > user > Dashboard.php
redirect('user/dashboard');
}
public function checkbox() {
if (isset($_POST['name_check_box']) {
return true;
} else {
$this->form_validation->set_message('checkbox', 'Check box needs to be checked');
return false;
}
}
}
View
http://www.codeigniter.com/user_guide/helpers/form_helper.html
<?php echo form_open('login');?>
<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo set_value('v_item_title'); ?>" />
<input type="checkbox" name="name_check_box"> Something <br>
<input type="submit" value="Submit">
<?php echo form_close();?>
Note: you may need to set custom routes in application > config >
routes.php
http://www.codeigniter.com/user_guide/general/routing.html
Try this
VIEW
<form>
<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo $this->session->flashdata('v_item_title'); ?>" />
<input type="submit" value="Submit">
</form>
Make sure that you load the session library and form helper
i have same issue. i applied this logic and its works for me
Change your add and edit method like this...
public function add(){
$college = $this->session->flashdata('data');
$this->load->view("college_add", compact('college'));
}
public function edit(){
$college_id = $this->uri->segment(3);
if($college_id)
{
$college = $this->session->flashdata('data');
if(empty($college))
$college = $this->college->get_college_details_secure($college_id);
$this->load->view('college_add', compact('college'));
}
else
redirect('college/add');
}
And Your save method redirect like this..
if ($this->form_validation->run() != TRUE)
{
$this->set_flashdata("message","Ooopps... form validation failed.".validation_errors());
$this->session->set_flashdata('data', $this->input->post());
if($college_id == '')
redirect("college/add");
else
redirect("college/edit/$college_id");
}

set values to fields in adminhtml form template

I'd created a template file for admin form tab as:
class Excellence_Designer_Block_Adminhtml_Designer_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs {
protected function _beforeToHtml() {
$this->addTab('images', array(
'label' => Mage::helper('designer')->__('Images'),
'title' => Mage::helper('designer')->__('Images'),
'content' => $this->getLayout()->createBlock('designer/adminhtml_designer_edit_tab_images')->toHtml(),
));
return parent::_beforeToHtml();
}
}
class Excellence_Designer_Block_Adminhtml_Designer_Edit_Tab_Images extends
Mage_Adminhtml_Block_Template implements
Mage_Adminhtml_Block_Widget_Tab_Interface {
public function _construct() {
parent::_construct();
$this->setTemplate('designer/edit/tab/images.phtml');
}
public function getTabLabel() {
return $this->__('Images');
}
public function getTabTitle() {
return $this->__('Images');
}
public function canShowTab() {
return true;
}
public function isHidden() {
return false;
}
}
images.phtml
<div class="input-field">
<label for="image">Custom Field</label>
<input type="text" class="input-text" name="image" id="image" />
</div>
but there's no value in there if I do want to edit the form
even the value is saved in database. The other tab was created with Mage_Adminhtml_Block_Widget_Form and showing the values in fields but for this how could I get the value?
Your EditAction() has to be in the way. Check it out.
public function editAction()
{
$newsId = $this->getRequest()->getParam('id');
$newsModel = Mage::getModel('news/news')->load($newsId);
if ($newsModel->getId() || $newsId == 0) {
$data = Mage::getSingleton('adminhtml/session')->getFormData(true);
if (!empty($data)) {
$model->setData($data);
}
Mage::register('news_data', $newsModel);
$this->loadLayout();
$this->_setActiveMenu('news/items');
$this->_addBreadCrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
$this->_addBreadCrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->_addContent($this->getLayout()->createBlock('news/adminhtml_news_edit'))
->_addLeft($this->getLayout()->createBlock('news/adminhtml_news_edit_tabs'));
$this->renderLayout();
}
else
{
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('news')->__('Item does not exist'));
$this->_redirect('*/*/');
}
}
I'd come to a solution don't know is it the right approach but works in my case. If you have a better solution then let me know.
I'd made a change in images.phtml
<div class="input-field">
<label for="image">Custom Field</label>
<input type="text" value="<?php echo $this->getValue(); ?>" class="input-text" name="image" id="image" />
</div>
and added a method in the respective block file
public function getValue() {
return Mage::registry('designer_data')->getImage();
}

Categories