SOLVED!
if I click page 2 that`s error:
Not Found
The requested URL /rank/GetAll/30 was not found on this server.
My link is:
http://localhost/rank/GetAll/30
Model: Rank_Model
<?php
Class Rank_Model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("ranking");
}
public function fifa_rank($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("ranking");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
Controller: Rank
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class rank extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper("url");
$this->load->helper(array('form', 'url'));
$this->load->model('Rank_Model','',TRUE);
$this->load->library("pagination");
}
function GetAll() {
$config = array();
$config["base_url"] = base_url() . "rank/GetAll";
$config["total_rows"] = $this->Rank_Model->record_count();
$config["per_page"] = 30;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Rank_Model->fifa_rank($config["per_page"], $page);
$data['errors_login'] = array();
$data["links"] = $this->pagination->create_links();
$this->load->view('left_column/open_fifa_rank',$data);
}
}
View Open: open_fifa_rank
<?php
$this->load->view('mains/header');
$this->load->view('login/loggin');
$this->load->view('mains/menu');
$this->load->view('left_column/left_column_before');
$this->load->view('left_column/menu_left');
$this->load->view('left_column/left_column');
$this->load->view('center/center_column_before');
$this->load->view('left_column/fifa_rank');
$this->load->view('center/center_column');
$this->load->view('right_column/right_column_before');
$this->load->view('login/zaloguj');
$this->load->view('right_column/right_column');
$this->load->view('mains/footer');
?>
and View: fifa_rank
<table>
<thead>
<tr>
<td>Pozycja</td>
<td>Kraj</td>
<td>Punkty</td>
<td>Zmiana</td>
</tr>
</thead>
<?php
foreach($results as $data) {?>
<tbody>
<tr>
<td><?php print $data->pozycja;?></td>
<td><?php print $data->kraj;?></td>
<td><?php print $data->punkty;?></td>
<td><?php print $data->zmiana;?></td>
</tr>
<?php } ?>
</tbody>
</table>
<p><?php echo $links; ?></p>
Maybe you know where is my problem?
Now I know where is my problem.
In first page I have link:
http://localhost/index.php/rank/GetAll
But on the next:
http://localhost/rank/GetAll/30
In secend link, I don`t have index.php. How can I fix it?
In $config["base_url"] = base_url() . "rank/GetAll";
I add :
$config["base_url"] = base_url() . "index.php/rank/GetAll";
And it`s ok :)
looking here $data["links"] = $this->pagination->create_links();
you have an array of links as your code says.
foreach( $data["links"] as $links)
{?>
<p><?php echo $links; ?></p>
<?php }
Try defining default value as $page=0 to GetAll() and remove assignment to the $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; and provide / after GetAll in url
Related
hi all and sorry for my bad english but anyway hope someone can help me
so i have web portal for wathcing movies and tv shows online.i want implement search on site across all database tables(i have 3 tables: MOVIES, TV SHOWS, ANIMATIONS)
so i create model:
<?php
class Search_model extends CI_Model {
public function search($q, $row_count, $offset) {
$array_search = array(
'name' => $q,
'descriptions' => $q
);
$query1 = $this->db
->or_like($array_search)
->limit(100)
->get('movie', $row_count, $offset);
$query2 = $this->db
->or_like($array_search)
->limit(100)
->get('serial', $row_count, $offset);
$query3 = $this->db
->or_like($array_search)
->limit(100)
->get('animation', $row_count, $offset);
return [
'movie' => $query1->result_array(),
'serial' => $query2->result_array(),
'animation' => $query3->result_array(),
];
}
}
also i create controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Search extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['title'] = "Search";
$this->load->model('search_model');
$this->data['search_result'] = array();
$offset = (int) $this->uri->segment(3);
$row_count = 5;
if($this->input->get('q_search')) {
$q = $this->input->get('q_search');
$this->data['search_result'] = $this->search_model->search($q, $row_count, $offset);
//pagination
$this->load->library('pagination');
$p_config['suffix'] = '?' . http_build_query($_GET, '', "&");
$count = count($this->search_model->search($q, 0 ,0));
$p_config['base_url'] = '/search/index/';
$p_config['first_url'] = $p_config['base_url'].'?'.http_build_query($_GET);
//pagination config
$p_config['total_rows'] = $count;
$p_config['per_page'] = $row_count;
//bootstrap pagination
$p_config['full_tag_open'] = "<ul class='pagination'>";
$p_config['full_tag_close'] ="</ul>";
$p_config['num_tag_open'] = '<li>';
$p_config['num_tag_close'] = '</li>';
$p_config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$p_config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$p_config['next_tag_open'] = "<li>";
$p_config['next_tagl_close'] = "</li>";
$p_config['prev_tag_open'] = "<li>";
$p_config['prev_tagl_close'] = "</li>";
$p_config['first_tag_open'] = "<li>";
$p_config['first_tagl_close'] = "</li>";
$p_config['last_tag_open'] = "<li>";
$p_config['last_tagl_close'] = "</li>";
//init pagination
$this->pagination->initialize($p_config);
$this->data['pagination'] = $this->pagination->create_links();
$this->data['tCount'] = $count;
}
$this->load->view('templates/header', $this->data);
$this->load->view('search', $this->data);
$this->load->view('templates/footer');
}
}
also create route:
$route['search'] = 'search';
$route['search/(:any)'] = 'search/$1';
also i create search file in views/main folder:
<h2>Search (Result <?php echo $tCount; ?>)</h2>
<?php foreach ($search_result as $key => $value): ?>
<div class="well">
<?php echo $value['name']; ?><br><br> <?php echo $value['descriptions'].'<br>'; ?>
</div>
<?php endforeach ?>
<?php echo $pagination; ?>
but when i try search for example movie or tv show on portal i have error:
An Error Was Encountered:Unable to load the requested file: search.php
I am listing the registered employee from the db and showing 10 employee per page. I'm using codeigniter pagination for the purpose.
If I deleted an employee in page 2, after deleting I need to go to page 2 showing rest of the employees in page 2.
View:
<a href="<?php echo $path;?>welcome/delete_employee/employeeid/<?php echo $row->empID;?>/pageNum/<?php echo $currentPage; ?>" onClick="return delAlert()";><img src="<?php echo $path;?>/img/delete.png" height="30px" width="30px" /></a>
Controller:
public function delete_employee()
{
$session_id = $this->session->userdata('logged_in');
if($session_id) {
$array = $this->uri->uri_to_assoc(3);
$count=$array['pageNum']-1;
$i=$count*10;
$this->load->model('welcomemodel','m',true);
$this->m->deleteemployee($array['employeeid']);
$config = array();
$config["base_url"] = base_url() . "welcome/employee";
$config["total_rows"] =$this->m->employee_count();
$config["per_page"] = 10;
$config["uri_segment"] = $array['pageNum'];
$data['showData'] = $this->m->getEmployee($config["per_page"], $i);
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data["currentPage"] =$array['pageNum'];
$this->load->view('header');
$this->load->view('employee',$data);
} else {
$this->load->view('session_expired');
}
}
Model:
public function deleteemployee($employeeid)
{
$this->db->where('empID',$employeeid);
$this->db->delete('employee');
return $this->db->affected_rows();
}
public function getEmployee($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select()
->from('employee')
->order_by('emp_fname');
$this->db->join('service', 'employee.serviceID = service.serviceID','left');
$query=$this->db->get();
return $query;
}
public function employee_count()
{
return $this->db->count_all("employee");
}
But now the pagination link shows first page's link,even though contents of second page is displayed....
model method
public function employee_count()
{
$result = $this->db->get('employee');
if ($result->num_rows() > 0) {
//employee record available
return true;
}
else
{
// employee record not available
return false;
}
}
View
<?php
$getData = $this->[your model name]->getEmployee($limit, $start);
?>
<?php if ($getData): ?>
//Your Pagination Part Here.....
<?php else: ?>
// else Nothing
<?php endif ?>
I'm trying to build a easy booking system in codeIgniter and using a database with a table called conference_rooms. I'm calling for this in my Booking_Model.phplike this:
public function __construct()
{
$this->load->database();
}
public function get_room() {
$query = $this->db->get('conference_rooms');
return $query->result_array();
}
}
To display it I'm using my Booking.php class looking like this:
public function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms'])) {
show_404();
}
$data['title'] = $data['conference_rooms']['title'];
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
And my view.php:
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>
It won't find $room. What am I doing wrong?
UPDATE:
Basically changed the whole code, my view class now looks like this (changed to index in my Booking controller)
public function index() {
$this->load->helper('form');
$this->load->view('bootstrap/header');
$this->load->model('Booking_Model');
$rooms = $this->Booking_Model->get();
$rooms_form_options = array();
foreach ($rooms as $id => $room) {
$rooms_form_options[$id] = $room->title;
}
$this->load->model('Package_Model');
$packages = $this->Package_Model->get();
$packages_form_options = array();
foreach ($packages as $id => $package) {
$packages_form_options[$id] = $package->package_name;
}
$this->load->view('booking', array(
'rooms_form_options' => $rooms_form_options,
'packages_form_options' => $packages_form_options,
));
$this->load->view('bootstrap/footer');
}
And my booking.php;
<div>
<?php echo form_label('Conference Room', 'id') ; ?>
<?php echo form_dropdown('id', $rooms_form_options, set_value('id')); ?>
</div>
<div>
<?php echo form_label('Package type', 'package_id') ; ?>
<?php echo form_dropdown('package_id', $packages_form_options, set_value('package_id')); ?>
</div>
<div>
<?php echo form_label('Antal deltagare', 'number_people') ; ?>
<?php echo form_input('number_people', set_value('number_people')) ; ?>
</div>
<div>
<?php echo form_submit('preview', 'Book'); ?>
</div>
Use this
In Model
<?
function __construct()
{
$this->load->database();
}
function get_room() {
$query = $this->db->get('conference_rooms');
$result = $query->result_array();
return $result;
}
?>
In controller
function view()
{
$data['conference_rooms'] = $this->booking_model->get_room();
if (empty($data['conference_rooms']))
{
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
?>
in view
<h3><?php echo $conference_rooms[0]['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms[0]['seats']; ?>
</div>
cz of $conference_rooms[0] pointing 0 is we passing data with Objective array. so we need to point data. check with print_r
MODEL
You need to row_array(); to return an array
public function get_room() {
$query = $this->db->get('conference_rooms');
$rowcount = $query->num_rows();
if( $rowcount > 0 ){
return $row = $query->row_array();
} else {
return FALSE;
}
}
CONTROLLER
public function view()
{
$conference_rooms = $this->booking_model->get_room();// asing your array to variable
if (empty($conference_rooms)) {
show_404();
} else {
$data['conference_rooms']=$conference_rooms;// pass your variable to data array to pass into view
$this->load->view('templates/header', $data);
$this->load->view('view', $data);
$this->load->view('templates/footer');
}
}
VIEW
<h3><?php echo $conference_rooms['title']; ?></h3>
<div class="main">
<?php echo $conference_rooms['seats']; ?>
</div>
Well I've searched and searched all around but I still can't find a solution to my problem. I'm still new to php and codeigniter so maybe I missed the answer already but anyways, here's what I'm trying to do.
This is my Controller (c_index.php) - calls a search function and performs pagination on the resulting array.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_index extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('m_search');
$this->load->library("pagination");
$this->load->library("table");
}
/** Index Page for this controller */
public function index()
{
if($this->session->userdata('logged_in')){
$this->load->view('v_user');
}
else{
$this->load->view('index');
}
}
public function search()
{
// start of search
$search_term = $this->input->post('word');
$books = $this->m_search->search_books($search_term);
// start of pagination
$config['base_url'] = "http://localhost/test/index.php/c_index/search";
$config['per_page'] = 5;
$config['num_links'] = 7;
$config['total_rows'] = count($books);
echo $config['total_rows'];
$this->pagination->initialize($config);
$data['query'] = array_slice($books,$this->uri->segment(3),$config['per_page']);
$this->load->view("index",$data);
}
}
Here's my view (index.php) - basically just displays the pagination result
<h3> Search Results </h3>
<!-- table -->
<table class="table table-condensed table-hover table-striped" id="result_table">
<tbody>
<?php
if(isset ($query)){
if(count($query)!=0){
foreach($query as $item){
echo "<tr><td>". $item['title'] ." by " .$item['author'] ."<br/></td></tr>";
}
echo $this->pagination->create_links();
}
else
{
echo "No results found for keyword ' ". $this->input->post('word')." ' .";
}
}
else
{
echo "Start a search by typing on the Search Bar";
}
?>
</tbody>
</table>
My model (m_search.php) - basically searches the database and returns an array of results.
<?php
class M_search extends CI_Model{
function search_books($search_term='default')
{
$filter = $this->input->post('filter');
//echo $filter;
if($filter == 'title')
{
//echo 'title';
$this->db->select('*');
$this->db->from('book');
$this->db->like('title',$search_term);
// Execute the query.
$query = $this->db->get();
return $query->result_array();
}else if($filter == 'author')
{
//echo 'author';
$this->db->select('*');
$this->db->from('book');
$this->db->like('author',$search_term);
// Execute the query.
$query = $this->db->get();
return $query->result_array();
}else if($filter == 'type')
{
//echo 'type';
$this->db->select('*');
$this->db->from('book');
$this->db->like('book_type',$search_term);
// Execute the query.
$query = $this->db->get();
return $query->result_array();
}else if($filter == 'status')
{
//echo 'status';
$this->db->select('*');
$this->db->from('book');
$this->db->like('book_status',$search_term);
// Execute the query.
$query = $this->db->get();
return $query->result_array();
}else
{
//echo 'all';
$this->db->select('*');
$this->db->from('book');
$this->db->like('book_status',$search_term);
$this->db->or_like('book_type',$search_term);
$this->db->or_like('author',$search_term);
$this->db->or_like('title',$search_term);
// Execute the query.
$query = $this->db->get();
return $query->result_array();
}
}
}
Now my problem is keeping the results for the pagination.
The first page is fine but whenever I click on a page link, the results on the table show the whole database and is not limited to my search results.
I've read somewhere that I need to use sessions to keep my search_term so that it works when I switch pages but I don't know where to put it.
Any advice or suggestions would be greatly appreciated. Thanks.
There are several of different ways to handle search and pagination depending on your needs. Based on your existing code, this is what I would do.
Change
$search_term = $this->input->post('word');
to
$search_term = ''; // default when no term in session or POST
if ($this->input->post('word'))
{
// use the term from POST and set it to session
$search_term = $this->input->post('word');
$this->session->set_userdata('search_term', $search_term);
}
elseif ($this->session->userdata('search_term'))
{
// if term is not in POST use existing term from session
$search_term = $this->session->userdata('search_term');
}
function search_books($search_term='default')
{
$filter = $this->input->post('filter');
//echo $filter;
if($filter == 'title')
{
$this->db->like('title',$search_term);
}
else if($filter == 'author')
{
$this->db->like('author',$search_term);
}
else if($filter == 'type')
{
$this->db->like('book_type',$search_term);
}
else if($filter == 'status')
{
$this->db->like('book_status',$search_term);
}
else
{
$this->db->like('book_status',$search_term);
$this->db->or_like('book_type',$search_term);
$this->db->or_like('author',$search_term);
$this->db->or_like('title',$search_term);
// Execute the query.
}
$query = $this->db->get('book');
return $query->result_array();
}
}
Just trying to lessen your Model code, if you don't want to use "session" and want to copy, paste link/url and get same result in your search then you should use uri class in your controller.
you can use following code in your controller all query string will show in pagination links.
$config['reuse_query_string']=TRUE;
for additional parameters in paging link you can un comment following 2 lines.
//$getData = array('s'=>$search);
//$config['suffix'] = '?'.http_build_query($getData,'',"&");
$this->pagination->initialize($config);
Try in this way in your Controller,
public function search()
{
$result=array();
if($this->session->userdata('login_id')!='')
{
$q = trim($this->input->get('q'));
if(strlen($q)>0)
{
$config['enable_query_strings']=TRUE;
$getData = array('q'=>$q);
$config['base_url'] = base_url().'admin/Product/search/';
$config['suffix'] = '?'.http_build_query($getData,'',"&");
$config['first_url'] = $config['base_url'].'?q='.$q;
$config["per_page"] = 10;
$config['use_page_numbers'] = TRUE;
$total_row = $this->Prod_model->search_record_count($q);
$config['total_rows']=$total_row;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next >';
$config['prev_link'] = '< Previous';
if($this->uri->segment(3))
{
$page = $this->uri->segment(3);
}
else
{
$page = 1;
}
$result['search'] = $this->Prod_model->search($q,$config["per_page"],$page);
if(empty($result['search']))
{
echo "no data found";die;
}
else
{
$this->pagination->initialize($config);
$str_links = $this->pagination->create_links();
$result["links"] = explode(' ',$str_links);
$id=$this->session->userdata('login_id');
$result['user']=$this->Home_model->user_details($id);
$this->template->load('prod_table',$result);
}
}
else
{
echo "empty string";die;
}
}
else
{
redirect(base_url()."admin/");
}
}
Hi everyone I am new to CodeIgniter and PHP.
I'm trying to create pagination on one of my pages, but my code doesn't display the pagination on the page for some reason.
view.php
</head>
<body>
<h1>Answer</h1>
<?php if (isset($records)) : foreach($records as $row) : ?>
<div = 'container'>
<?php
if (isset($pagination))
{
echo $pagination;
}
?>
<ul>
<h1><?php echo $row->question; ?></h1>
<li><?php echo $row->answer1; ?></li>
<li><?php echo $row->answer2; ?></li>
<li><?php echo $row->answer3; ?></li>
<li><?php echo $row->answer4; ?></li>
<li><?php echo $row->answer5; ?></li>
<li><?php echo $row->answer6; ?></li>
<ul>
</div>
<?php endforeach; ?>
<?php else : ?>
<h2>no records were returned</h2>
<?php endif; ?>
</body>
</html>
control.php
<?php
class Survey extends CI_Controller{
function index()
{
$data = array(
'question' => $this->input->post('question'),
'answer1' => $this->input->post('answer1'),
'answer2' => $this->input->post('answer2'),
'answer3' => $this->input->post('answer3'),
'answer4' => $this->input->post('answer4'),
'answer5' => $this->input->post('answer5'),
'answer6' => $this->input->post('answer6'),
);
if($query = $this->membership_model->get_records())
{
$data['records'] = $query;
}
$this->load->view('survey_view', $data);
}
//pagination
function page()
{
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/admin/index.php/survey/index';
$config['total_rows'] = $this->db->get('save_survey')->num_rows();
$config['per_page'] = 1;
$config['num_links'] =10;
$this->pagination->initialize($config);
$data['records'] = $this->db-get('save_survey', $config['per_page'], $this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$this->load->view('survey_view', $data);
}
}
?>
The function page() is never called.
When you navigate to the controller then the default index() function will be triggered. And in that function there is no call to page(); method.
I never used the pagination stuff of CI before, but to include your pagination code (page()) in the index() method, you can do it like this (I removed the view from the page method):
class Survay extends CI_Controller{
function index() {
$data = array(
'question' => $this->input->post('question'),
'answer1' => $this->input->post('answer1'),
'answer2' => $this->input->post('answer2'),
'answer3' => $this->input->post('answer3'),
'answer4' => $this->input->post('answer4'),
'answer5' => $this->input->post('answer5'),
'answer6' => $this->input->post('answer6'),
);
if($query = $this->membership_model->get_records()) {
$data['records'] = $query;
}
//call your page method and set the pagination variables to this object
$this->page();
//show the view
$this->load->view('survay_view', $data);
}
//pagination ONLY sets the pagination variables
function page() {
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/admin/index.php/survay/index';
$config['total_rows'] = $this->db->get('save_survay')->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$data['records'] = $this->db-get('save_survay', $config['per_page'], $this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
}
}