Controller[In Article Page Article Properly work with pagination, store user email id in 'articles' database , now i tried to get the user firstname, and lastname from users table but not work properly ]
public function articles()
{
$data['title'] = "Articles";
$config = array();
$config["base_url"] = base_url() . "sd/articles/";
$config["total_rows"] = $this->model_users->record_count_articles();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->model_users->fetch_result_articles($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('sd/header',$data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/footer', $data);
} else {
$this->load->view('sd/sdheader', $data);
$this->load->view('sd/articles', $data);
$this->load->view('sd/sdfooter', $data);
}
}
Model [ Get Users Name in Article Page ]
public function record_count_articles() {
return $this->db->where('status','1')->count_all("articles");
}
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->where('status','1')->order_by('id', 'DESC')->get("articles");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Add These Lines [ But Not Work]
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
$query = $this->db->select('firstname')->select('lastname')->where('email',$data[0]->email)->get("users");
$data['name_info']=$query->result_array();
}
return $data;
}
return false;
You have 2 problem here. please have a look on comments in code.
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
//1) $data[0]->email keep repeating same email.
// inner $query variable should be different.
$innerQuery = $this->db->select('firstname,lastname')->where('email',$row->email)->get("users");
//2) you need to store query result on array.
// $data['name_info'] stores only single record.
$data[]=$innerQuery ->result_array();
}
return $data;
}
return false;
You should avoid query in loop if you can achieve it by join
EDIT: Lets try this with join
public function fetch_result_articles($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db
->join('users u','u.email = a.email','left')
->where('a.status','1')->order_by('a.id', 'DESC')->get("articles a");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
I have not tested the code. but it is better way than loop.
Related
I'm unable to retrieve similar post data from db with codeigniter. In my blog, I have a tags field which is keeping data like 'php,mysql,mongo,java,jquery'
I just try to get similar post which is related with current posts tags. But im not getting expected result. and the problem is in my query. Its show only three post and that is 1st, last, and number 3rd one.
[CONTROLLER]
public function showpost()
{
$data = array();
$this->load->view('header',$data);
$data['post'] = $query->result();
$data['similar'] = $this->crudModel->getSimilarPost();
$this->load->view('showfull',$data);
$this->load->view('footer');
}
[MODEL]
public function getSimilarPost()
{
$query = $this->db->get_where('blogs',array('id' => $this->uri->segment(3)));
foreach($query->result() as $row){ $tags = $row->tags; }
$match = explode(',', $tags);
for($i = 0; $i < count($match); $i++)
{
$this->db->like('tags',$match[$i]);
$this->db->from('blogs');
$sqlQuery = $this->db->get();
}
return $sqlQuery->result();
}
[VIEW]
foreach($similar as $row)
{
echo($row->btitle.'<br/>');
}
Try this.
public function showpost()
{
$data = array();
$this->load->view('header',$data);
$data['post'] = $query->result(); // why this line??
$data['similar'] = $this->crudModel->getSimilarPost();
$this->load->view('showfull',$data);
$this->load->view('footer');
}
[MODEL]
public function getSimilarPost()
{
$query = $this->db->get_where('blogs',array('id' => $this->uri->segment(3)));
foreach($query->result() as $row){ $tags = $row->tags }
$match = explode(',', $tags);
$result = [];
for($i = 0; $i < count($match); $i++)
{
$this->db->like('tags',$match[$i]);
$this->db->from('blogs');
$sqlQuery = $this->db->get();
if($sqlQuery->num_rows()>0)
$result[] = $sqlQuery->result();
}
return $result;
}
[VIEW]
$check = [];
foreach($similar as $row)
{
foreach($row as $data)
{
if(!in_array($data->btitle,$check))
{
$check[] = $data->btitle;
echo $data->btitle.'<br/>';
}
}
}
I am trying to get the count() of my sql table. However, I keep getting this error.
Call to a member function master_banner() on null
How can I go about changing my code?
Controller
public function index()
{
//$this->landing_banners_upload();
$config["total_rows"] = count($this->Backend_banner_model->master_banner_list());
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$data['per_page']= $config["per_page"];
$data["no_pages"] = $config['total_rows']/$config['per_page'];
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$offset=$page+$config["per_page"];
$data['banners']=$this->Backend_banner_model->master_banner($page,$offset);
count($data['banners']);
$data["links"] = $this->pagination->create_links();
echo $data["links"];
$this->load->view('layout/header');
$this->load->view('backendBanner/landing_banners_upload_view', $data);
$this->load->view('layout/footer');
}
Model
public function master_banner_list(){
$sql="select * from banner";
$query = $this->db->query($sql);
$data = array();
if ($query->num_rows() > 0){
foreach ($query->result() as $row) {
$data[] = $row;
}
}
return $data;
}
public function master_banner($start,$offset){
$sql="SELECT * FROM banner
WHERE RowNum > '".$start."' AND RowNum <= '".$offset."'";
$query = $this->db->query($sql);
$data = array();
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
This question already has answers here:
Error in pagination of codeigniter 1
(3 answers)
Closed 7 years ago.
In model/rci_model.php
public function record_count() {
return $this->db->count_all("produk");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC");
return $query->result();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
In controller/sandal_clarudo.php
function sandals_for_men(){
$data['seo'] = $this->rci_model->tampil_meta(22);
$this->load->view('head', $data);
$config = array();
$config["base_url"] = "http://localhost/clarudo/index.php/sandal_clarudo/sandals_for_men/";
$config["total_rows"] = $this->rci_model->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->rci_model->
fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('men', $data);
$this->load->view('footer');
}
In view/men.php
<?php
foreach($results as $t) {
$t->nama_produk
}
<div><?php echo $links; ?></div>
I want to show product that has category = 'Men' only, but in my code showing all category product,it make me confused,help me please.
Just try with this code for your fetch_countries() function
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC");
return $query->result();
}
what happens here is you are return results if available or return false
In model/rci_model.php
public function record_count() {
return $this->db->count_all("produk");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC");
return $query->result();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
In controller/sandal_clarudo.php
function sandals_for_men(){
$data['seo'] = $this->rci_model->tampil_meta(22);
$this->load->view('head', $data);
$config = array();
$config["base_url"] = "http://localhost/clarudo/index.php/sandal_clarudo/sandals_for_men/";
$config["total_rows"] = $this->rci_model->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->rci_model->
fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('men', $data);
$this->load->view('footer');
}
In view/men.php
<?php
foreach($results as $t){
echo "$t->nama_produk";
}
I want to show product that has category = 'Men' only, but using my code it shows all category products. Ideas?
Try This
in cotroller
$count = $this->rci_model->get_count(); //get number of rows
$config['base_url'] = base_url().'index.php/sandal_clarudo/sandals_for_men/';
$config['total_rows'] = $count;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['links'] = $this->pagination->create_links();
$data["results"] = $this->rci_model->fetch_countries($limit, $page);
in model
public function get_count()
{
//count
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men'");
$result = $query->result_array();
$count = count($result);
return $count;
}
//data from table
public function fetch_countries($limit, $page)
{
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
in view
//to show data
<?php
foreach ($results as $new_result)
{
echo $new_result['nama_podak'];
}
?>
//to show pagination
<?php
echo $link//Page segment tabs(you can config boostarp for this)
?>
Try this one. You wrong when using limit.
$query=$this->db->query("SELECT * FROM produk WHERE id_kategori='Men' order by nama_produk ASC LIMIT $limit, $start");
$this->db->limit($limit, $start);
$this->db->where('id_kategori', 'men');
$this->db->order_by('nama_produk');
$query = $this->db->get("produk");
Hope this work!
for count use :
public function record_count($limit, $start) {
$this->db->from('count(id_produk) AS count');
$this->db->where('id_kategori', 'men');
$this->db->order_by('nama_produk');
$this->db->limit($limit, $start);
$return = $query->row_array();
return $return['count'];
}
Hello guys i am trying to create a blog. The first home page is ok.. i am getting the shrot description and the categories from the database but...i have problems with the links:
this are my controller functions:
public function index()
{
$this->load->model('Model_cats');
$data['posts'] = $this->Model_cats->getLivePosts(10);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = 'Welcome to Paul Harbuz Blog Spot!';
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function category($id)
{
$data['category'] = $this->Model_cats->getCategory($id);
$data['posts'] = $this->Model_cats->getAllPostsByCategory($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['category']['name'];
$data['main'] = 'public_home';
$this->load->vars($data);
$this->load->view('template', $data);
}
public function post($id)
{
$data['post'] = $this->Model_cats->getPost($id);
$data['comments'] = $this->Model_cats->getComments($id);
$data['cats'] = $this->Model_cats->getTopCategories();
$data['title'] = $data['post']['title'];
$data['main'] = 'public_post';
$this->load->vars($data);
$this->load->view('template');
}
this are my model function:
function getTopCategories()
{
$this->db->where('parentid',0);
$query = $this->db->get('categories');
$data = array();
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[$row['id']] = $row['name'];
}
}
$query->free_result();
return $data;
}
function getLivePosts($limit)
{
$data = array();
$this->db->limit($limit);
$this->db->where('status', 'published');
$this->db->order_by('pubdate', 'desc');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getCategory($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('categories');
if($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
function getAllPostsByCategory($catid)
{
$data = array();
$this->db->where('category_id', $catid);
$this->db->where('status', 'published');
$query = $this->db->get('posts');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row){
$data[] = $row;
}
}
$query->free_result();
return $data;
}
function getPost($id)
{
$data = array();
$this->db->where('id',$id);
$this->db->limit(1);
$query = $this->db->get('posts');
if ($query->num_rows() > 0)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
and in the view page i have something like this:
if ( count($posts) )
{
foreach ($posts as $key => $list)
{
echo '<h2>'.$list['title'].'</h2>';
echo auto_typography(word_limiter($list['body'], 200));
echo anchor('post/'.$list['id'],'read more >>');
}
echo '<br/><br/>';
}
I'm getting the post id in the url but.. i don't know why the page is not found.
You have to add the controller name to the anchor uri segments.
echo anchor('CONTROLLER/post/'.$list['id'],'read more >>');
More on this topic in the CodeIgniter URLs documentation.
If you want a URL like http://example.com/post/123 then you have to add the following to your application/config/routes.php file:
$route['post/(:num)'] = "CONTROLLER/post/$1";
More on routing is also available in the documentation.