how to paginate a table in zend framework? - php

I'm using zend framework and want to paginate the table of contents based on the data received from the MySQL table. I want to show only 10 rows in the html table.
here is the .phtml code i have..
<div class="panel-body">
<?php
if(isset($this->ErrorMessage))
echo "<div class='alert alert-danger'>" . $this->ErrorMessage . "</div>";
if(isset($this->success))
echo "<div class='alert alert-success'>" . $this->success . "</div>";
echo $this->form;
?>
<!-- / .table -->
<div class="table-responsive">
<table class="table table-striped table-hover dataTable table-bordered no-footer" role="grid">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php if(isset($this->results)) { ?>
<?php foreach($this->results as $result) { ?>
<tr>
<td><?php echo $result->id; ?></td>
<td><?php echo $result->name; ?></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
so how can i add the pagination...

You don't state whether you want to paginate on the client or server side. If you want to paginate on the client side, the easiest way is to just use JQuery DataTables:
https://www.datatables.net/
Example:
$(document).ready(function() {
$('.dataTable').DataTable();
} );

First you will need to use Paginator class. Let' say that you already have a factory class which passess an instance of tablegateway adapter to your modeltable class. Let's create a function to retreive all the data
public function fetchAll()
{
$dbSelect = new DbSelect($select, $this->tableGateway->getAdapter(), $this->tableGateway->getResultSetPrototype());
return new Paginator($dbselect);
}
In your controller you will do something simillar to this.
public function indexAction()
{
$this->view->setTemplate("application/index/index");
$paginator = $this->getServiceLocator()->get("myModelTable")->fetchAll();
$paginator->setCurrentPageNumber((int) $this->params("page", 1));
$paginator->setItemCountPerPage(10);
$this->view->paginator = $paginator;
return $this->view;
}
In your view part put this somewhere (usually at the bottom of the file)
echo $this->paginationControl($this->paginator, 'sliding', 'application/pagination.phtml', ['route' => 'application']);

Related

Codeigniter anchor tag hyperlinked to a form in controller but the data shown outside the table

I am using Codeigniter 3 and trying CRUD operation. I have created the basic crud operation and am showing the data in a table however I have linked a paragraph tag in the controller below the table to the form controller, If I want to enter another data
The issue is when I click on the link to enter another data it redirect me the original form in controller but when I enter the data and submit it, The data is shown below the table in the paragraph tag.
I am not able understand why this is happening as the controller is the same
I had faced a similar issue before when redirecting in controller.I had redirected the page after submission to show_form() controller which was basically redirecting the page to $this->load->view('learn/view_form');
in which I have kept a condition that if No data is present click to enter. Now when it redirects to show_form() controller it goes into else condition even if the data is present
CONTROLLER
<?php
defined('BASEPATH') OR exit("No direct script access allowed");
class Learning extends CI_Controller{
public function __construct(){
parent::__construct();
$this ->load->helper("url");
$this->load->model("tatti_test");
$this->load->database();
$this->load->helper();
}
//functions should be passed here
//creating a function
function start_learn() {
//this varible
$this->load->view('learn/start_learn');
}
function start_crud(){
$this->load->view('learn/form');
}
function show_form(){
$this->load->view("learn/view_form");
}
function insert_form(){
$name = $this->input->post("u_name");
$email = $this->input->post("u_email");
$mobile = $this->input->post("u_mobile");
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("u_file")){
$file='noimage.png';
}
else {
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->insert_tatti($data);
redirect("learning/view_form");
}
function view_form(){
$data['returned_data']=$this->tatti_test->show_form();
$this->load->view("learn/view_form",$data);
}
function delete_entry(){
$id=$this->uri->segment(3);
$data=$this->tatti_test->for_unlink($id);
$filepath="./assets/images/".$data['file_name'];
unlink($filepath);
$this->tatti_test->delete_entry($id);
redirect('learning/view_form');
}
function time_to_update(){
$id=$this->uri->segment(3);
$data['fetched_update_entry']=$this->tatti_test->update_entry($id);
$this->load->view("learn/update.php",$data); //bus associative array hi leta hai
}
function up_db(){
$name =$this->input->post('up_name');
$email = $this->input->post('up_email');
$mobile = $this->input->post('up_mobile');
$file = $this->input->post('up_file');
$id = $this->input->post('up_id');
//File Uploading
$config['upload_path']="./assets/images/";
$config["allowed_types"]="gif|jpg|png";
$config['encrypt_name']=true;
$this->load->library("upload",$config);
if(!$this->upload->do_upload("up_file")){
$data= $this->tatti_test->remove_prev($id);
$file=$data['file_name'];
}
else {
$data= $this->tatti_test->remove_prev($id);
$path="./assets/images/".$data['file_name'];
unlink($path);
$filework = $this->upload->data();
$file =$filework['file_name'];
}
$data = array(
"name"=>$name,"email"=>$email,"mobile"=>$mobile,"file_name"=>$file
);
$this->tatti_test->up_nw($data,$id);
redirect('learning/view_form');
}
} /*this accesses command from main ci controller */
?>
VIEW
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } ?>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
MODEL
<?php
class Tatti_test extends CI_Model{
function insert_tatti($insert_data){
$this->db->insert("f_form",$insert_data);
}
function show_form(){
$query = $this->db->get("f_form");
$response=[];
if ($query->num_rows() > 0){
$response = $query->result_array();
}
else {
$response = 0;
}
return $response;
}
function for_unlink($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response=[];
foreach ($query->result_array() as $rows){
return $response = $rows;
}
}
function delete_entry($id){
$this->db->where("id",$id);
$this->db->delete("f_form");
}
function update_entry($id){
$this->db->where("id",$id);
$query = $this->db->get("f_form");
$response = [];
if($query->num_rows() > 0 ){
foreach($query->result_array() as $rows);
$response = $rows;
}
return $response;
}
function up_nw($introduced_data,$id){
$this->db->set($introduced_data);
$this->db->where('id',$id);
$this->db->update('f_form');
}
function remove_prev($id){
$this->db->where('id',$id);
$query = $this->db->get('f_form');
$response = [];
foreach($query->result_array() as $rows){
$response=$rows;
}
return $response;
}
}
?>
This is how the data is showing when clicked on the link below table
enter image description here
You're html formatting is messed up. You should have the closing </table> outside your foreach loop or premature <table> closure.
Also moved the Add another entry link outside the foreach loop. So it only appears once, and your document format not messed up.
You can use this fixed view instead:
<?php $this->load->view("common/header.php");
if ($returned_data != 0){ ?>
<table border='1'>
<tr>
<th>Sr No</th>
<th>Name</th>
<th>Password</th>
<th>Mobile</th>
<th>Email</th>
<th>Final Name</th>
<th>Delete</th>
<th>View</th>
</tr>
<?php $i=0; foreach ($returned_data as $key=>$d){
?>
<tr>
<td>
<?php echo ++$i; ?>
</td>
<td>
<?php echo $d['name'];?>
</td>
<td>
<?php echo $d['mobile'];?>
</td>
<td>
<?php echo $d['email'];?>
</td>
<td>
<?php echo $d['file_name'];?>
</td>
<td>
<img src="<?php echo base_url().'/assets/images/'.$d['file_name'];?>" width="100px" ; height="100px" />
</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
</table>
<p>Add another entry
<?php echo anchor("learning/start_crud"," here "); ?>
</p>
<?php } else { ?>
<p>No data to show please click
<?php echo anchor("learning/start_crud"," here "); ?>to enter</p>
<?php } ?>
<?php $this->load->view("common/footer.php");
I realize that you are trying to do CURD,
first of all, try this to improve your code and fill the missing library of codeigniter: https://github.com/avenirer/CodeIgniter-MY_Model
For your code:
No data passed to the view in show_form(),
You should check the form submission and the conditions to load the view,
simple thing to do is follow the best practice using ready scripts,
Hope this will be useful,

To get a link from database

Hi Im currently using codeigniter, Below i have attached the complete code. As i run this Im getting data from database but i want URL field to be link.How to do that.
This is my controller:
public function ajaxsearch()
{
if(is_null($this->input->get('id')))
{
$this->load->view('input_view');
}
else
{
$this->load->model('Infomodel');
$data['Infotable']=$this->Infomodel->Infotable($this->input->get('id'));
$this->load->view('output_view',$data);
}
}
Below is the model:
class Infomodel extends CI_Model
{
function Infotable($search)
{
$query = $this
->db
->select('*')
->from('Info_table')
->like('NAME',$search)
->or_like('URL',$search)
->get();
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return null;
}
}
}
and two views: one is input_view
And another is output_view:
if(!empty($Infotable ))
{
$output = '';
$outputdata = '';
$outputtail ='';
$output .= '<div class="container">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Url</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
';
foreach ($Infotable as $objects)
{
$outputdata .= '
<tr>
<td >'.$objects->id.'</td>
<td >'.$objects->name.'</td>
<td>'.$objects->url.'</td>
<td>'.$objects->tags.'</td>
</tr>
';
// echo $outputdata;
}
$outputtail .= '
</tbody>
</table>
</div>
</div> ';
echo $output;
echo $outputdata;
echo $outputtail;
}
else
{
echo 'Data Not Found';
}
Thanks in advance for the help.
if you mean that you want the url to be clickable by the user you can simply change this line:
<td>'.$objects->url.'</td>
to something like this:
<td>'.$objects->url.'</td>
This will create a link in the table cell that will send you to the link
Try using tag inside the table data.
<td> <a heref="'.$objects->url.'">'.$objects->url.'</a></td>

Codeigniter DATABASE Trouble

The thing is when i use this code
in my model :
public function get_all_subject_tasks(){
$this->db->select('*');
$this->db->from('task');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("task_id", "desc");
$query_result=$this->db->get();
$result=$query_result->result();
return $result;
}
in my controller:
public function subjects($t,$action=""){
$data=array();
$data['subject_tasks']=$this->Schoolmodel->get_all_subject_tasks($t);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
in my php page:
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } ?>
</table>
</div>
I get all of the tasks that are in the database, without any subject filter.
So my question is
How do i make the page echo the tasks based on which subject they are in?
Also here is how i have setup my database structure
subject_id
task_id
task_name
task_desc
user_id
Where subject_id is the id of the subject where the task is inserted in.
<?php
// your model
public function getTask($subject_id)
{
$result = $this->db
->where("subject_id", $subject_id) // !!!
->where("user_id", $this->session->userdata('user_id')) // !!!
->order_by("task_id", "desc")
->get('tasks');
return $result->num_rows() > 0 ?
$result->result() : false;
}
// your class
class School extends CI_Controller {
public function __construct() {
parent::__construct();
if($this->session->userdata('logged_in')==FALSE){
redirect('User');
}
$this->load->database();
$this->load->model('Schoolmodel');
$this->load->library('form_validation');
}
public function subjects($subject_id,$action=""){
$data=array();
$data['subject_tasks'] = $this->Schoolmodel->getTask($subject_id);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
}
?>
// your view
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php if(false !== $subject_tasks) { foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } } ?>
</table>
</div>
This is not going to work:
$this->db->where("subject_id",$subject_id AND "user_id",$this->session->userdata('user_id'));
From Active Record documentations: Multiple calls to where will result in AND:
$this->db->where("subject_id", $subject_id);
$this->db->where("user_id", $this->session->userdata('user_id'));

Pass value while submitting a form button from model to controller

I am making an application in codeigniter. I am trying to pass value stored in a variable when a submit button is being clicked from model to controller.
How can I achieve this? I have tried the below code so far:
model.php
public function loadadd($mekhala_Id) {
echo form_open('Payment/amount($count)');
$query = $this->db->get_where('tb_unit', array('mandalam_Id' => $mekhala_Id));
echo $count= $query->num_rows();
?>
<h1>Members List</h1>
<table border="1">
<tr>
<th>Unit</th>
<th>Unit Secretary</th>
<th>Amount paid</th>
</tr>
<?php
foreach ($query->result() as $row)
{
//$i=1;
?>
<tr>
<td> <?php echo $row->unitName ;?></td>
<td> <?php echo $row->unit_sec ;?></td>
<td> <?php echo form_input(array('name'=>'na','placeholder'=>'Rupees Paid')) ;?></td>
<td><?php echo form_checkbox(array('name'=>'check','value'=>'paid')) ; ?></td>
</tr>
<?php
// $i++;
}
// echo $query->result();
echo form_submit(array('name'=>'sub','value'=>'submit'));
echo form_close();
}
public function loadpayment($paid,$count){
for($i=1;$i<=$count;$i++)
{
$a='na'.$i;
$paid=array($this->input->post($a));
$this->db->insert('tb_unit',array('Amount'=> $paid));
}
}
}
?>
controller.php public function amount($count) {
$paid=$this->input->post('na');
$this->cms_model->loadpayment($paid,$count);
}
While running this code below is being shown:
An error was encountered
The URI you submitted has disallowed characters.
I think you should read again the main idea of the model, view and controller.
The idea of the models is only to work with the database. You do not use form_open() or other html in models, this should be done in the views.
This tutorial shows more details.
To pass value with submit you can use hidden field in the form as show here.
To pass data in a variable when submit button is being clicked: you can set the variable to the value attribute: echo form_submit(array('name'=>'sub','value'=>"$variable"));

Error in search and display records using yii framework

I am new in yii framework.I am doing seach recoeds in database and display in another page using yii framework.My controller is Sitecontroller.php,view pages are search.php and search_result.php. and my model is job.php.My error is
"urlencode() expects parameter 1 to be string, object given
C:\wamp\www\yii\framework\web\CUrlManager.php(440)".
My controller is Sitecontroler.php
<?php
class SiteController extends Controller
{
public function actionsearch()
{
$user_id = trim($_GET['id']);
$model = new Job ;
if(isset($_POST['Job']))
{
$model->attributes=$_POST['Job'];
$title=$_POST['Job']['title'];
$title=trim($title);
$model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",));
$number=count($model);
if($number>0)
{
$this->redirect($this->createUrl('site/search_result',array('title'=>$title)));
}
}
$this->render('search',array('model' =>$model));
}
public function actionsearch_result()
{
$title=$_GET['title'];
$model = new Job ;
$model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",));
$this->render('search_result',array('model' =>$model));
}
}?>
My view files-search.php
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>false,
'htmlOptions' => array(),
'clientOptions'=>array(
'validateOnSubmit'=>true
),
)); ?>
<?php
foreach(Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
}
?>
<div class="row">
<?php echo $form->labelEx($model,'Keyword'); ?>
<?php echo $form->textField($model,'title'); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
view file-search_result.php
<div style="float:right;margin-right:285px;">
<h1>Search Jobs</h1>
<table width="200" border="1">
<tr>
<td>SI No</td>
<td>Title</td>
<td>Key Skill</td>
<td>Experince</td>
</tr>
<?php
foreach($model as $models)
{
?>
<tr>
<td></td>
<td><?php echo $models->title; ?></td>
<td><?php echo $models->key_skills; ?></td
><td><?php echo $models->experience; ?></td
></tr>
<?php
}
?>
</table>
</div>
Any body help me?
There are problems in this line
$this->redirect(array('site/Search_result',array('model' =>$model)));
You should try this instead
$this->redirect($this->createUrl('site/search_result',array('parameter'=>'value')));
Note:- You cant send $model through URL as it is an object and that is why you are getting this error.
UPDATE
In your search_result.php
change this code
foreach($model as $models)
{
?>
<tr>
<td></td>
<td><?php echo $models->title; ?></td>
<td><?php echo $models->key_skills; ?></td
><td><?php echo $models->experience; ?></td
></tr>
<?php
}
to this
<tr>
<td></td>
<td><?php echo $model->title; ?></td>
<td><?php echo $model->key_skills; ?></td
><td><?php echo $model->experience; ?></td
></tr>
Explaination
In this line
$model=Job::model()->find(array('select'=>'*',"condition"=>"title like '%$title%'",));
As you can see you have used find() for fetching the data. SO find() is going to return you a single row (hence a single object) and not an array of objects. If you want to fetch multiple records then you should use findAll()
So when using findAll() your code becomes
$model=Job::model()->findAll(array('select'=>'*',"condition"=>"title like '%$title%'",));
and then you can use the same code as you were using in the search_result.php file like
foreach($model as $models)
{
?>
<tr>
<td></td>
<td><?php echo $models->title; ?></td>
<td><?php echo $models->key_skills; ?></td
><td><?php echo $models->experience; ?></td
></tr>
<?php
}
I think the problem may be, that you have a bad naming of your actions.
You should use camelCase:
public function actionSearch()
...
public function actionSearch_result()
this is the naming needed by Yii, so it can find your actions.
The redirect method expects an action and without this I suppose it cannot find it.
Also the redirect expect a controler/action route, that means in your case it should read:
$this->redirect(array('site/search_result','model' =>$model));
Please keep in mind that the function is actionSearch_result with a Capital S but the route itself, is site/search_result with a lowercase s

Categories