I'm making a blog in Codeigniter. Now on the blog post view page, I want the previous and next post link. This is my controller of blog details.
public function blog_details($slug = NULL){
//data
$data['blog_info'] = $this->Blog_model->get_posts($slug);
if(empty($data['blog_info'])){
show_404();
}
$data['post_name'] = $data['blog_info']['post_name'];//post name
$data['categories'] = $this->Blog_model->get_categories();
//meta title, discription, keywords
$data['meta_title'] = $data['blog_info']['post_name'];
$data['meta_description'] = $data['blog_info']['post_meta'];
$data['meta_keywords'] = $data['blog_info']['post_tags'];
//view
$data['main_content'] = 'blog_details';
$this->load->view('include/template',$data);
}
and my model is
public function get_posts($slug = FALSE, $limit = TRUE, $offset = FALSE){
if($limit){
$this->db->limit($limit, $offset);
}
if($slug === FALSE){
$this->db->order_by('blogs.post_id', 'DESC');
$this->db->join('category', 'category.category_name = blogs.category');
$query = $this->db->get('blogs');
return $query->result_array();
}
$query = $this->db->get_where('blogs', array('post_slug' => $slug));
return $query->row_array();
}
How to create previous and next post links with post names? Thanks in advance.
you can use direct query method to run below query and just pass your blog id to get previus and next blog name
public function get_prewnext($id){
$query = $this->db->query("SELECT name,
(SELECT name FROM blog s1
WHERE s1.id < s.id
ORDER BY id DESC LIMIT 1) as previous_name,
(SELECT name FROM blog s2
WHERE s2.id > s.id
ORDER BY id ASC LIMIT 1) as next_name
FROM blog s
WHERE id = $id;");
if($query->num_rows() > 0){
$data = $query->result_array();
}
$query->free_result();
return $data;
}
Note: If value is not found then it will return null.
Related
Original function:
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$query = $this->db->get('orders');
return $query->result_array();
}
}
I need to JOIN data from table order_shipping and order_products where order_id = order_id
I do:
/*
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$query = $this->db->get_where('orders', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
This above code working correct. But currently JOIN only after else. I need do the same way before else:
* Fetch user data
*/
function getRows($id = ""){
if(!empty($id)){
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('', array('id' => $id));
return $query->row_array();
}else{
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_shipping', 'orders.id = order_shipping.id');
$this->db->join('order_products', 'order_shipping.id = order_products.id');
$query = $this->db->get('');
return $query->result_array();
}
}
But this code before else not working correct for me and currently display wrong data. When I try fetch all orders, then second function display correct all orders. But when I try fetch example only order ID 2, then is used first function and display wrong data, example when I fetch order ID2, then always display only order with ID 1.
This function is for REST API fetch orders.
This is my model:
public function GetStudentData()
{
$sID = $this->input->get('id');
$class_id = $this->db->query("SELECT student.class_id FROM student WHERE student.sID = '$sID'");
$query = $this->db->query("SELECT * FROM student WHERE(sID='$id'AND class_id = '$class_id')");
if($query->num_rows() > 0)
{
return $query->row();
}else{
return false;
}
}
How can I use $class_id in the next query?
you could do it in one query... (from your example [SIC])
public function GetCSData() {
$id = $this->input->get('id');
$query = $this->db->query("SELECT student.*
FROM student
WHERE student.sID = '$id'
AND student.class_id = (SELECT class_id
FROM student
WHERE student.sID = '$id')");
if($query->num_rows() > 0){
return $query->row();
}else{
return false;
}
}//..GetCSData end
looking at it though, that will give the same result as
SELECT *
FROM student
WHERE sID = '$id'
maybe I am missing a point?
Hi guys I tried to use the variable equation on Codeigniter as in the code I wrote below, Is my code writing correct? and here's my code so far I wanna using variable $amount in my controller
In Controller
$amount = $this->m_data->getTotalSales->$data['TOTAL_SALES'];
and this in Model
//GET TOTAL REVENUE
function getTotalSales(){
$this->db->select("(SELECT SUM(grand_total) FROM sales_order WHERE member = ".$this->session->userdata('ID').") - (SELECT SUM(amount) FROM payment WHERE member_id = ".$this->session->userdata('ID').") AS total_sales");
$query = $this->db->get();
if ($query->num_rows() >0){
foreach ($query->result() as $data) {
$hasilSemua[] = $data;
}
return $hasilSemua;
}
}
$amount = $this->m_data->getTotalSales();
function getTotalSales(){
$result1 = $this->db->select_sum("grand_total")
->from('sales_order')
->where("member = $this->session->userdata('ID')");
->get();
$result2 = $this->db->select_sum("amount")
->from('payment')
->where("member_id = $this->session->userdata('ID')")
->get();
return $result1->row()-$result2->row();
}
}
I believe this is what you are after. Note that I pass the UserID in as the second parameter to the WHERE function. This is in case there is any chance of the user having inputted it (always safter to do this anyway).
//GET TOTAL REVENUE
function getTotalSales(){
$db = $this->db;
$db->select('SUM(grand_total)');
$db->where('member',$this->session->userdata('ID'));
$q1 = $db->get_compiled_select('sales_order');
$db->select('SUM(amount)');
$db->where('member_id',$this->session->userdata('ID'));
$q2 = $db->get_compiled_select('payment');
$query = $db->query("SELECT ($q1) - ($q2) as total_sales");
$data = $query->row_array();
return $data['total_sales'];
}
You can then call $amount = $this->m_data->getTotalSales; to get your result.
I have a database in mysql which has 2 tables one named category(cat_id primary key) and one named book(b_id as primary key) where cat_id is a foreign key for book.
I'm working in CI and here I output the categories in a view:
foreach($categories->result()as $row){
foreach($categories->result()as $row){
echo ''.$row->category.'<br>';
}
}
I want that when clicking to the link category_details to output just the books of that category.
Here I have this method in my controller:
public function category_details($data)
{
$data['cat_id'] = $this->home_model->output_cat_detail();
$data['category_detail'] = $this->home_model->output_cat_detail();//printon librat
$data['categories'] = $this->home_model->output_categories();
$this->load->view('header', $data);
$this->load->view('category_details', $data);
}
So the method in model which does that selection is this:
public function output_cat_detail(){
$condition = "cat_id =" . "'" . $data['cat_id'] . "'";
$this->db->select('*');
$this->db->from('book');
$this->db->where($condition);
$query = $this->db->get();
return $query;
But after I do this when I click in a category all the books appear not just the books of that category I have clicked. Can someone help me with the condition to select just the books of that category?
public function category_details($id){
$data = array();
$data['category_detail'] = "";
$query = $this->db->get_where('book', array('id' => $id));
// Your current model logic will not work for this
if($query->num_rows() > 0){
$data['category_detail'] = $query->result();
}
$this->load->view('header', $data);
$this->load->view('category_details', $data);
}
Hello i am stuck on codeigniter pagination.I am getting total rows 6, per page showing 2 row.
I am getting pagination link {Page: 1,2,3} where 1 is not clickable. On Clicking Page no:2 my link look like localhost/demo/index.php/home/press/?per_page=0/2 but i am getting error on sql
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/2,2' at line 1
SELECT * FROM media where media_type = 'PRESS' AND id <> 0 ORDER BY id DESC LIMIT 0/2
Same Error,pagintaion link getting when i click on link : 3
I dont know how to fix this issue.I am sharing my code which i used on my controller,model and view.
My controller code:
function press()
{
$this->load->model('Home_model');
$page= $this->input->get('per_page') ? $this->input->get('per_page') : 0;
$this->load->library('Pagination');
$data['page'] = $this->input->get('page');
if($data['page'] == '') {
$data['page'] = $config['per_page'] = '2'; // Per Page
} else {
$data['page'] = $config['per_page'] = $this->input->get('page');
}
$config['first_url']='0';
$pageno = $this->input->get('per_page');
if($pageno == ''){
$pageno = '0';
}
$url_to_paging = $this->config->item('base_url');
$config['base_url'] = $url_to_paging.'home/press/?per_page='.$page;
$return = $this->Home_model->pressdata($config['per_page'],$pageno, $data);
$data['pressdata'] = $return['result']; // Get Two Row Data
$config['total_rows'] = $return['count']; // Total Count 6
$this->pagination->initialize($config);
$this->load->view('press',$data);
}
Model :
function pressdata($pg_num, $offset, $content)
{
if($offset == ''){
$offset = '0';
}
$sql = "SELECT * FROM media where media_type = 'PRESS' AND id <> 0";
if($pg_num!=0 || $pg_num!="")
{
$sql .= " ORDER BY id DESC LIMIT ".$offset.",".$pg_num;
}
$query = $this->db->query($sql);
$sql_couint = "SELECT * FROM media where media_type = 'PRESS' AND id <> 0";
$query1 = $this->db->query($sql_couint);
$ret['result'] = $query->result();
$ret['count'] = $query1->num_rows();
return $ret;
}
Try it like this
$this->load->library('pagination');
$this->load->model('Home_model');
$config['base_url'] = base_url().'home/press/;
$config['total_rows'] = 200; // Total no of rows returned from the database table
$config['uri_segment'] = 3; // It is the page no,used as offset in model
$offset = 0;
if ($this->uri->segment($config['uri_segment']) != "") {
$offset = $this->uri->segment($config['uri_segment']);
}
$config['per_page'] = 20; // you can change it as you want
$return = $this->Home_model->pressdata($config['per_page'],$offset, $data);
$this->pagination->initialize($config);
In your model you can do something like this
function pressdata($per_page, $offset) {
if($per_page !== '' && $offset !== '') {
$this->db->limit($per_page, $offset);
}
//rest of your query
}
Don't copy paste it though, I just wanted to give you an idea. Modify it, implement it according to your needs. Let me know if it worked. Go through CodeIgniter's pagination library for more detailed documentation.
Finally i found the way to solve this problem.
Here are the final code for Controller, Model, View.
Controller Code :
function press()
{
$this->load->library('pagination');
$url_to_paging = $this->config->item('base_url'); // URL here (www.example.com)
$config['base_url'] = $url_to_paging.'home/press/'; // www.example.com/home/press
$config['per_page'] = '4'; // Per Page Data Show 4
$data = array();
$return = $this->Home_model->pressdata($config['per_page'],$this->uri->segment(3));
$data['pressdata'] = $return['result'];
$config['total_rows'] = $return['count'];
$this->pagination->initialize($config);
$this->load->view('press', $data);
}
Model Code :
function pressdata($num, $offset)
{
if($offset == '')
{
$offset = '0';
}
$sql = "SELECT * FROM media where media_type = 'PRESS' AND id <> 0 ";
if($num!='' || $offset!='')
{
$sql .= " order by id desc limit ".$offset.",".$num."";
}
$query = $this->db->query($sql);
$sql_count = "SELECT * FROM media WHERE media_type = 'PRESS' AND id <> 0";
$query1 = $this->db->query($sql_count);
$ret['result'] = $query->result_array();
$ret['count'] = $query1->num_rows();
return $ret;
}
View Page for pagination link :
if($this->pagination->create_links()) { ?>
<div style="clear:both; border-top: solid 1px #e9ecf1;"></div>
<div style="float:right;">
<span class="pagination" style="margin:0px; border:none;">
<?php echo $this->pagination->create_links(); ?>
</span>
</div>
<?php }
function pressdata($pg_num, $offset, $content)
{
if($offset == ''){
$offset = '0';
}
$sql = "SELECT * FROM media where media_type = 'PRESS' AND id => 0";
if($pg_num!=0 || $pg_num!="")
{
$sql .= " ORDER BY id DESC LIMIT ".$offset.",".$pg_num;
}
$query = $this->db->query($sql);
$sql_couint = "SELECT * FROM media where media_type = 'PRESS' AND id => 0";
$query1 = $this->db->query($sql_couint);
$ret['result'] = $query->result();
$ret['count'] = $query1->num_rows();
return $ret;
}
<-------id should be greater and equal to zero----->