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>
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 am using codeigniter pagination. But I'm facing a problem while using codeingiter pagination. Here is the issue which I am facing.
Ex: If there are more than 10 records for a page and here I am displaying 5 records per page. If I click on the second page it is displaying the data correctly but if I need to go back for the first page it is not working. Here is my code:
Controller:
class Testimonial extends CI_Controller {
function __construct() {
parent::__construct();
//here we will autoload the pagination library
$this->load->model('testimonial_model');
$this->load->library('pagination');
}
public function index()
{
$config = array();
$config["base_url"] = base_url('testimonial/index');
$config['total_rows'] = $this->testimonial_model->record_count();//here we will count all the data from the table
$config['per_page'] = 2;//number of data to be shown on single page
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();//create the link for pagination
$data['mainpage'] = "testimonial";
$this->load->view('templates/template',$data);
}
}
Model:
class Testimonial_model extends CI_Model
{
function get_all_testimonials($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select('T.*');
$this->db->from('testimonials AS T');
$this->db->where(array('T.status'=>1));
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function record_count()
{
return $this->db->count_all("testimonials");
}
}
use this answer please read the code once to understand it
public function index()
{
$config = array();
//$config["base_url"] = base_url('testimonial/index');
$get_vars = $this->input->get();
if(is_array($get_vars))
$config['suffix'] = '?'.http_build_query($get_vars,'', "&");
//$config['prefix'] = '?'.http_build_query($get_vars,'', "&");
$config['base_url'] = base_url().$this->router->class.'/index';
$config['first_url'] = $config['base_url'] . (isset($config['suffix'])?$config['suffix']:'');
$config['total_rows'] = $this->testimonial_model->record_count();//here we will count all the data from the table
$config['per_page'] = 2;//number of data to be shown on single page
//this line should be according to url to fetch actual value
$config["uri_segment"] = ($this->uri->segment('2')?$this->uri->segment('2'):0); // 2 or 3 based on ur url
$this->pagination->initialize($config);
$page = $config["uri_segment"];
$data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();//create the link for pagination
$data['mainpage'] = "testimonial";
$this->load->view('templates/template',$data);
}
My Pagination not work , In this case simple print users article to match the rc code of users its work perfectly url is ../project/article/user_article/?rc=4715422 , its work but pagintion url not print any article, pagination url is ../project/article/user_article/?rc=4715422/1,
here is my code: controller code is -
public function user_article()
{
$rc=$_GET['rc'];
$data['title'] = "User Article";
$config = array();
$config["base_url"] = base_url() . 'article/user_article/?rc='. $rc ;
$config["total_rows"] = $this->article_m->record_count_uarticles($_GET);
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->article_m->fetch_result_uarticles($_GET, $config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if ($this->session->userdata ('is_logged_in')){
$data['profile']=$this->model_users->profilefetch();
$this->load->view('header',$data);
$this->load->view('user_article', $data);
$this->load->view('sfooter', $data);
} else {
redirect('signin');
}
}
and my modal is :
public function record_count_uarticles($rc) {
$query=$this->db->select('email,writer')->where('rc',$rc['rc'])->get('users');
$result=$query->result_array();
if ($query->num_rows() > 0) {
$row = $query->row_array();
$array = array ('email' => $row['email'], 'status' => '1');
return $this->db->where($array)->count_all_results("articles");
}
}
public function fetch_result_uarticles($rc, $limit, $start) {
$query=$this->db->select('email,writer')->where('rc',$rc['rc'])->get('users');
$result=$query->result_array();
if ($query->num_rows() > 0) {
$row = $query->row_array();
$this->db->limit($limit, $start);
$array = array ('u.email' => $row['email'], 'a.status' => '1', 'u.writer' => '1');
$query1=$this->db->select('a.id,title,a.status,description,image,a.email,tags,postdate,firstname,lastname,rc')->join('users u','u.email = a.email','left')->where($array)->order_by('a.id', 'DESC')->get('articles a');
if ($query1->num_rows() > 0) {
foreach ($query1->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
Check this code, pagination show properly, first page article print correctly but after click on pagination link then no article print in scnd page
This Link work but default link is not work , is there any query to change the url
../project/article/user_article/?rc=4715422/1 -- Default Link Not Work
../project/article/user_article/1/?rc=4715422 -- Custom Edited Link work
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/");
}
}