how to add simple searching yii2 - search

I have to make a simple searching in my index view.
The data to be searched in the table is not a grid view but a regular table.
How can I add a simple searching like this?
And this is the code in index view
Search
<table class="table table-striped table-bordered">
<tr>
<th class="text-center">NIP</th>
<th class="text-center">Nama Pegawai</th>
<th class="text-center">Jumlah Dokumen</th>
<th></th>
</tr>
<?php foreach($models as $data): ?>
<tr>
<td><?= $data["nip"] ?></td>
<td><?= $data["nama"] ?></td>
<td align = "center"><?= $data["jumlah"] ?></td>
<td width = "200 px" align = "center"><a class = "btn btn-success" href = "./?r=arsip-sdm/detail&id=<?= $data['nip'] ?>">Detail</td>
</tr>
<?php endforeach; ?>
</table>
And this is the code in controller
help me please

You could use a separate filter form
eg :
<div class="post-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'creation_date') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::submitButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
perform a render partial in your view and use the normal 'searchModel' => $searchModel, in your you gridView config
you can see some sample here
http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html
http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#filtering-data
http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#separate-filter-form

Related

How to use other classes object in .phtml file without object manager in magento 2

I am currently invoking the method “xyz” using the object manager, however the object manager should not be used in phtml file as mentioned in the Magento 2 documentation. What is the best practice to create an object of other classes in phtml file?
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
ViewModel files can be used to add codes in a template file without using an object manager.
Here Are Steps to create View Model
Step 1:- Create a layout file in Vendor/Module/view/frontend/layout/custom_index_custom.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block name="dummy" template="Sunarc_Custom::details.phtml">
<arguments>
<argument name="view_model" xsi:type="object">Sunarc\Custom\ViewModel\Customer</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
Step 2:- Create a view model file under Vendor/Module/ViewModel/Customer.php
<?php
$viewModel = $block->getViewModel();
$collection = $viewModel->getCollection();
if ($collection->count()) {
?>
<div class="table-wrapper customer">
<table class="data table" id="my-custom-table">
<thead>
<tr>
<th scope="col" class="col id"><?php echo __('ID') ?></th>
<th scope="col" class="col name"><?php echo __('Name') ?></th>
<th scope="col" class="col email"><?php echo __('Email') ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($collection as $item) : ?>
<tr>
<td data-th="<?= $block->escapeHtml(__('ID')) ?>" class="col id">
<?php echo $item->getId() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Name')) ?>" class="col name">
<?php echo $item->getName() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Email')) ?>" class="col email">
<?php echo $item->getEmail() ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
?>
Step 3:- Create your custom phtml file in Vendor/Module/view/frontend/templates/details.php
<?php
$viewModel = $block->getViewModel();
$collection = $viewModel->getCollection();
if ($collection->count()) {
?>
<div class="table-wrapper customer">
<table class="data table" id="my-custom-table">
<thead>
<tr>
<th scope="col" class="col id"><?php echo __('ID') ?></th>
<th scope="col" class="col name"><?php echo __('Name') ?></th>
<th scope="col" class="col email"><?php echo __('Email') ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($collection as $item) : ?>
<tr>
<td data-th="<?= $block->escapeHtml(__('ID')) ?>" class="col id">
<?php echo $item->getId() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Name')) ?>" class="col name">
<?php echo $item->getName() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Email')) ?>" class="col email">
<?php echo $item->getEmail() ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
?>
One can also use Block Files to do the same.

CI-4 how to show multiple tables in one page

good day all,
since i'm new in Code Igniter 4 (i was a native PHP self-learner) and i'm new in MVC, my question is how can i show multiple tables in one page ?
i have a dashboard view with 2 tables inside. one is for display students data and the other is for teachers data. there is no relation between tables, so i can't join the tables.
i have successfully show one of the table (student or teacher) but i can't show both tables in dashboard at once. i think i have to pass 2 models into controller function but i just can't figure it out how to do it.
any help will be appreciated
here is parts of my codes (successfully showing data from 1 table) :
Controller
use App\Models\mStudent;
class Admin extends BaseController
{
public function index()
{
$model = new mStudent();
$data['title'] = 'Students';
$data['getStudents'] = $model->getStudents();
echo view('admin/header', $data);
echo view('admin/top_menu');
echo view('admin/side_menu');
echo view('admin/dashboard', $data);
echo view('admin/footer');
}
Model
public function getStudents()
{
return $this->findAll();
}
public function getTeachers()
{
return $this->findAll();
}
View
<!-- students table-->
<table class="table table-bordered text-sm">
<thead>
<tr>
<th style="width: 10px; text-align: center;">No</th>
<th style="text-align: center;">NAME</th>
<th style="text-align: center;">Class</th>
</tr>
</thead>
<tbody>
<?php $no = 1;
foreach ($getStudents as $students) { ?>
<tr>
<td><?= $no; ?></td>
<td><?= $students['name'] ?></td>
<td><?= $students['class'] ?></td>
</tr>
<?php $no++;
} ?>
</tbody>
</table>
<!-- teacher table-->
<table class="table table-bordered text-sm" id="defaulttable">
<thead>
<tr>
<th style="width: 10px; text-align: center;">No</th>
<th style="text-align: center;">NAME</th>
<th style="text-align: center;">ID</th>
</tr>
</thead>
<tbody>
<?php $no = 1;
foreach ($getTeachers as $teachers) { ?>
<tr>
<td><?= $no; ?></td>
<td><?= $teachers['name'] ?></td>
<td><?= $teachers['id'] ?></td>
</tr>
<?php $no++;
} ?>
</tbody>
</table>
Adding Dynamic Data to the View
In your Controller, right below $data['getStudents'] = $model->getStudents(); add:
$data['getTeachers'] = $model->getTeachers();
Addendum
Then in your model, return the respective result sets. I.e:
Model
public function getStudents()
{
return $this->db->table("students")->get()->getResultArray();
}
public function getTeachers()
{
return $this->db->table("teachers")->get()->getResultArray();
}
this is what i looking for
Controller
use App\Models\mStudent;
use App\Models\mTeacher;
class Admin extends BaseController
{
public function index()
{
$model_student = new mStudent();
$model_teacher = new mTeacher();
$data = array(
'student' => $model_student->getStudents(),
'teacher' => $model_teacher->getTeachers()
);
echo view('admin/header');
echo view('admin/top_menu');
echo view('admin/side_menu');
echo view('admin/dashboard', $data);
echo view('admin/footer');
}
View
Students :
<select name="stdid">
<?php foreach ($student as $std) : ?>
<option value="<?= $std['stdid']; ?>"><?= $std['stdname']; ?></option>
<?php endforeach; ?>
</select>
Teachers :
<select name="tcrid">
<?php foreach ($teacher as $tcr) : ?>
<option value="<?= $tcr['tcrid']; ?>"><?= $tcr['tcrname']; ?></option>
<?php endforeach; ?>
</select>
hope this will help someone out there

Codeigniter - upload xls or xlsx and send the data to current view

i been stuck at this problem for a long time. I have a page with list of student and student's score. I want to add a button to upload xls/xlsx file at the current page, and after i uploaded the file, the column data fill the student's score input. How can i do that? Please i need help on this.
View
<form action="<?php echo base_url()?>index.php/nilai/ubahNilaiItem/<?php echo $idKelas ?>/<?php echo $id ?>" method="post" class="form-horizontal">
<button name="tombol" type="submit" value="import" class="btn pull-right btn-primary">Import</button>
<input type="file" name="inputfile" size="20" class="pull-right"/>
</div>
<div class="col-lg-12">
<table class="table">
<thead>
<tr>
<th class="col-md-1 active">No</th>
<th class="col-md-2 active">NIM</th>
<th class="col-md-5 active">Nama</th>
<th class="col-md-1 active">Nilai</th>
</tr>
</thead>
<tbody>
<?php
$no = 0;
foreach($input as $i){
$no++
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $i->nim ?></td>
<td><?php echo $i->nama ?></td>
<td>
<div class="input-group">
<input hidden value="<?php echo $i->id ?>" name="id[]">
<input class="text-center form-control" value="<?php echo $i->angka_nilai ?>" name="angka[]"> //this is for inputing student score
<span class="input-group-addon"><i class="glyphicon glyphicon-question-sign" data-toggle="tooltip" title="Gunakan koma (,) untuk nilai desimal"></i></span>
</div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<button name="tombol" value="simpan" type="submit" class="btn pull-right btn btn-primary">Simpan</button>
</form>
Controller for the view
public function itemView($idKelas,$id){
//load model
$this->load->model('nilais');
//take data from database
$data2['input'] = $this->nilais->input_item($idKelas,$id)->result();
$data3['detail'] = $this->nilais->tampil_detail($idKelas)->result();
//merge array
$data4 = array_merge($data2, $data3);
$data4['item'] = $this->nilais->item_detail($id)->result();
$data4['idKelas'] = $idKelas;
$data4['id'] = $id;
//take data for header and notification
$data6['notifikasi'] = $this->nilais->notifikasi($this->session->userdata('id_user'))->result();
$data6['count'] = count($data6['notifikasi']);
$data6['matakuliah'] = $idKelas;
$data6['active'] = 'nilai';
//load view nilai and header
$this->load->view('nilai/item_view',$data4);
$this->load->view('header',$data6);
}
Update
Now i used PHPExcel, but i cant get the null cell. How can i get all cell even if its null?
Controller for excel
public function importAction($idKelas,$id){
//load model
$this->load->model('nilais');
$this->load->library('excel');
$config['upload_path'] = './file/excel/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ($this->upload->do_upload('inputfile')) {
$upload_data = $this->upload->data();
$objPHPExcel = PHPExcel_IOFactory::load($upload_data['full_path']);
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
//header will/should be in row 1 only. of course this can be modified to suit your need.
if ($row == 1) {
$header[$row][$column] = $data_value;
} else {
$arr_data[$row][$column] = $data_value;
}
}
//send the data in an array format
$data['header'] = $header;
$data['values'] = $arr_data;
}
}

ZF2 module title in layout

I'm having trouble with my layout and viewModel
My design requires that the title set in my viewModel, eg 'blog categories' to be pushed to the layout.
I'm using $this->headTitle() to set the title of the page but i can't get it anywhere else.
because of a lot of changes in the layout per page, i have been forced to use partial in the layout.
single.phtml
<?php echo $this->partial('lendstudy/partial/basic/top'); ?>
<div class="container_12 clearfix">
<div id="content_wrapper">
<?= $this->partial('lendstudy/partial/basic/title'); ?>
<?php echo $this->content; ?>
<?= $this->partial('lendstudy/partial/sidebar/left'); ?>
</div>
</div>
<?php echo $this->partial('lendstudy/partial/basic/bottom');
I'm setting the header title in the view
index.phtml from category in a blog module
<?php
$title = $this->translate('Categories');
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>Add new category</p>
<table class="table table-bordered table-hover">
<tr>
<th>Category</th>
<th> </th>
<th> </th>
</tr>
<?php foreach($categories as $category) : ?>
<tr>
<td><?php echo $this->escapeHtml($category->getName());?></td>
<td>
[Edit]
</td>
<td>
[Delete]
</td>
</tr>
<?php endforeach; ?>
</table>
it would then need to be outputtet in the partial/basic/title.
title.phtml
<div class="grid_12 page_title">
<h1><?= $this->headTitle(); ?><span class="subtitle"><?= $this->translate('More information about the Lend Study association') ?></span></h1>
<div class="page_navigation">
<?php $partial = array('lendstudy/partial/basic/breadcrumbs', 'default') ?>
<?php $this->navigation('navigation')->breadcrumbs()->setPartial($partial) ?>
<?php echo $this->navigation('navigation')->breadcrumbs()->render() ?>
</div>
</div>
how do i do this?

Custom loop next and previous post links not working

I'm using WP Events Manager and Advanced Custom Fields to develop a site, I have a custom post type called 'event' which I want to display via the below:
<article class="col-main col clearfix">
<table>
<tr>
<th>Cours</th>
<th>Niveau</th>
<th>Dates</th>
<th>Heures</th>
</tr>
<?php
$args = array(
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => get_query_var('paged'),
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_event_start_date');
$loop = new WP_Query( $args );
if($loop->have_posts()):
while ( $loop->have_posts() ) : $loop->the_post();
$EM_Event = em_get_event($post->ID, 'post_id');
?>
<tr>
<td><?php echo $EM_Event->output('#_EVENTLINK'); ?></td>
<td><?php the_field('niveau'); ?></td>
<td><?php if($slug != 'stages-ete'): ?>Chaque: <?php the_field('jours'); echo '<br />'; endif; echo $EM_Event->output('#_EVENTDATES'); ?></td>
<td><?php echo $EM_Event->output('#_EVENTTIMES'); ?></td>
</tr>
<?php endwhile;?>
<?php else: ?>
<tr><td colspan="3"><em>Pas de cours à venir.</em></td></tr>
<?php endif; ?>
</table>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries'); ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »'); ?></div>
</div>
</article>
But it's not displaying the previous and next links. Any ideas?
Thanks
Try adding the arguments this way instead
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'event',
'posts_per_page' => 10,
'paged' => $paged,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_event_start_date');
);

Resources