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();
}
}
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
Here is my issue:
I'm trying to creta a pagination for my blog page, but for some reason I try to put the number of entries that I want displayed, and it do not work. I do not know why?; any help will be helpful, thanks.
Here is my controller:
public function blog()
{
// Pagination for Blog //
$data['blog'] = $this->blog_model->get_blog();
$data['categorias'] = $this->categorias_model->get_categorias();
$data['title'] = 'Blog';
$this->load->library('pagination');
$config['base_url'] = site_url('/blog/');
$config['total_rows'] = 200;
$config['per_page'] = 1;
$config['uri_segment'] = 3;
$config['num_links'] = 2;
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$this->load->view('templates/head',$data);
$this->load->view('templates/navbar',$data);
$this->load->view('news\blog\index.php',$this->data);
$this->load->view('templates/footer',$data);
}
My model:
<?php
class Blog_model extends CI_Model{
// Connect to database //
public function __construct(){
$this->load->database();
}
// Get Posts from database //
public function get_blog($slug=FALSE){
if($slug===FALSE){
// Post order, ASC-DESC Categorias/Tags tabels //
$this->db->order_by('blog.id', 'DESC');
$query=$this->db->get('blog');
return $query->result_array();
}
$query=$this->db->get_where('blog', array('slug'=>$slug));
return $query->row_array();
}
public function crear_post(){
$slug=url_title($this->input->post('titulo'));
$data = array(
'titulo' => $this->input->post('titulo'),
'slug' => $slug,
'imagen' => $this->input->post('imagen'),
'fecha' => $this->input->post('fecha'),
'contenido' => $this->input->post('contenido'),
'categoria_id' => $this->input->post('categoria_id'),
);
return $this->db->insert('blog',$data);
}
public function delete_post($id){
$this->db->where('id', $id);
$this->db->delete('blog');
return true;
}
public function update_post(){
$slug = url_title($this->input->post('titulo'));
$data = array(
'titulo' => $this->input->post('titulo'),
'slug' => $slug,
'imagen' => $this->input->post('imagen'),
'fecha' => $this->input->post('fecha'),
'contenido' => $this->input->post('contenido'),
'categoria_id' => $this->input->post('categoria_id'),
);
$this->db->where('id', $this->input->post('id'));
return $this->db->update('blog', $data);
}
}
And here is my view:
<div class="col-md-8 animated fadeIn">
<?php foreach($blog as $blog) : ?>
<div class="card text-xs-left"><!-- FIRST ARTICLE DEMO -->
<div class="card-header" id="article-header">
<h4><?php echo ucfirst($blog['titulo']); ?></h4>
</div>
<img src="<?php echo $blog['imagen']; ?>" width="750" heihgt="350" class="img-fluid">
<!--<div class="card-body">
<p> <?php echo word_limiter($blog['contenido'],5); ?></p>
</div>-->
<div class="card-footer" id="article-footer">
<div class="row">
<div class="col-lg-12 col-md-9 col-sm-8">
<i class="fa fa-calendar" aria-hidden="true"></i> <?php echo ucfirst($blog['fecha']); ?>
<i class="fa fa-folder" aria-hidden="true"></i> <?php echo $blog['categoria_id']; ?>
Read more ยป
</div>
</div>
</div>
</div>
<?php endforeach ?>
<!-- /Article -->
<?php echo $pagination ?>
</div>
here is again:
my controller:
public function blog() {
$data['blog'] = $this->blog_model->get_blog();
$data['categorias'] = $this->categorias_model->get_categorias();
$data['title'] = 'Blog';
$config = array();
$config["base_url"] = base_url() . "blog";
$config["total_rows"] = $this->blog_model->record_count();
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$config['num_links'] = 1;
$config['query_string_segment'] = 'paginas';
$config['page_query_string'] = TRUE;
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["pagination"] = $this->pagination->create_links();
$data["results"] = $this->blog_model->get_paginas($config["per_page"], $page);
$this->load->view('templates/head',$data);
$this->load->view('templates/navbar',$data);
$this->load->view("news/blog/index", $data);
$this->load->view('templates/footer',$data);
}
and then again, my model:
class Blog_model extends CI_Model{
// Connect to database //
public function __construct(){
$this->load->database();
}
// Get Posts from database //
public function get_blog($slug=FALSE){
if($slug===FALSE){
// Post order, ASC-DESC Categorias/Tags tabels //
$this->db->order_by('blog.id', 'DESC');
// $this->db->join('categorias','categorias.id = blog.categoria_id');
// /Post order, ASC-DESC //
$query=$this->db->get('blog');
return $query->result_array();
}
$query=$this->db->get_where('blog', array('slug'=>$slug));
return $query->row_array();
}
///Pagination
public function record_count() {
return $this->db->count_all("blog");
}
public function get_paginas($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("blog");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
// Crear post //
public function crear_post(){
$slug=url_title($this->input->post('titulo'));
$data = array(
'titulo' => $this->input->post('titulo'),
'slug' => $slug,
'imagen' => $this->input->post('imagen'),
// 'autor' => $this->input->post('autor'),
'fecha' => $this->input->post('fecha'),
'contenido' => $this->input->post('contenido'),
'categoria_id' => $this->input->post('categoria_id'),
);
return $this->db->insert('blog',$data);
}
// Borrar post //
public function delete_post($id){
$this->db->where('id', $id);
$this->db->delete('blog');
return true;
}
// Actualizar Post //
public function update_post(){
$slug = url_title($this->input->post('titulo'));
$data = array(
'titulo' => $this->input->post('titulo'),
'slug' => $slug,
'imagen' => $this->input->post('imagen'),
// 'autor' => $this->input->post('autor'),
'fecha' => $this->input->post('fecha'),
'contenido' => $this->input->post('contenido'),
'categoria_id' => $this->input->post('categoria_id'),
);
$this->db->where('id', $this->input->post('id'));
return $this->db->update('blog', $data);
}
// Categorias //
// public function get_categorias(){
// $this->db->order_by('nombre');
// $query = $this->db->get('categorias');
// return $query->result_array();
// }
}
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>
Could someone please enlighten me as to what I'm doing incorrectly - I'm currently following a net tuts+ video, which goes through the process of listing an array of posts, and then having the ability to click on one post, to be shown the detail.
I'm adapting the above method for my own solution/web application, but I'm at a loss as to why my array is blank.
I'm using this line to create link/anchor to the next controller
<a href="<?php echo base_url(); ?>Search/site/<?php echo $site->siteID; ?>"><?php echo $site->site_title; ?>
I've highlighted the code where I believe I'm going a miss,
Thanks
Controller
class Search extends CI_Controller {
public function display($sort_by = 'site_title', $sort_order = 'asc', $offset = 0)
{
$limit = 20;
$data['columns'] = array(
'site_title' => 'Site Name',
'site_uprn' => 'Unique Property Reference'
);
$this->load->model('search_model');
$results = $this->search_model->search_sites($limit, $offset, $sort_by, $sort_order);
$data['sites'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
//pagination
$this->load->library('pagination');
$config = array ();
$config['base_url'] = site_url("Search/display/$sort_by/$sort_order");
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->load->view('search', $data);
}
**function site($siteID){
$this->load->model('search_model');
$data['site_detail']=$this->search_model->site_detail($siteID);
//$this->load->view('site', $data);
}
}**
Model
<?php
class Search_model extends CI_Model {
function search_sites($limit, $offset, $sort_by, $sort_order){
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('site_title', 'site_uprn');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'site_title';
//site table
$q = $this->db->select('siteID, site_title, site_uprn')
->from('sites')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
//count query
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('sites');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
**//sites details
function site_detail($siteID){
$this->db->select()->from('sites')->where('siteID', $siteID);
$query = $this->db->get();
return $query->first_row('array');
}
}**
View
<div id="right">
<h5>Site Details</h5>
<ul>
<li><?php $site_detail['siteID']?></li>
<li><?php $site_detail['site_title']?></li>
</div>
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