Codeigniter blog: display post title in comments table - php

I am working on a blog application in Codeigniter 3.1.8 and Bootstrap 4.
There is a posts table and a comments table in the database. I have displayed all the comments in a Bootstrap 4 table. I want to display the title of the post each comment belongs to, instead of the post's id:
My Comments controller:
class Comments extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Static_model');
$this->load->model('Posts_model');
$this->load->model('Categories_model');
$this->load->model('Comments_model');
}
public function index() {
$data = $this->Static_model->get_static_data();
$data['categories'] = $this->Categories_model->get_categories();
$data['number_of_categories'] = $this->Categories_model->get_num_rows();
$data['posts'] = $this->Posts_model->get_all_posts();
$data['number_of_posts'] = $this->Posts_model->get_num_rows();
$data['comments'] = $this->Comments_model->get_all_comments();
$this->load->view('partials/header', $data);
$this->load->view('dashboard/comments');
$this->load->view('partials/footer');
}
}
In the Comments_model model I have:
public function get_all_comments(){
$this->db->select('comments.*');
$this->db->order_by('comments.id', 'DESC');
//$this->db->limit($limit, $offset);
$this->db->join('posts', 'posts.id = comments.post_id');
$query = $this->db->get('comments');
return $query->result();
}
In the view:
<tbody>
<?php foreach ($comments as $index => $comment): ?>
<tr id="<?php echo $comment->id; ?>">
<td><?php echo $index + 1; ?></td>
<td class="w-25"><?php echo $comment->comment; ?></td>
<td><?php echo $comment->name; ?></td>
<td><?php echo $posts['title']; ?></td>
<td><?php echo nice_date($comment->created_at, 'D, M d, Y'); ?></td>
<td>Aproved</td>
<td></td>
</tr>
<?php endforeach ?>
</tbody>
While <?php echo $posts->id; ?> displays the posts id, which i do not need in the view, the line results in an
Message: Undefined index: title error.
What is missing?

Hope this will help you :
get_all_comments method should be like this : add posts.title in select also
public function get_all_comments()
{
$this->db->select('comments.*,posts.title as post_title');
$this->db->order_by('comments.id', 'DESC');
//$this->db->limit($limit, $offset);
$this->db->join('posts', 'posts.id = comments.post_id');
$query = $this->db->get('comments');
return $query->result();
}
Replace it
<td><?php echo $posts['title']; ?></td>
with
<td><?php echo $comment->post_title; ?></td>

Related

Codeigniter using SELECT_SUM, only returning the first column

I am trying to get the value of how many dishes are in each order of the logged-in user, by adding up the quantity of dishes group by order.
The normal query I run which is a success by assuming logged in user's userid is 35:
SELECT o.orderid, location, status, pay, total,
SUM(quantity) AS dishnum
FROM orders o
JOIN ordersdish od
ON od.orderid = o.orderid
WHERE userid = 35
GROUP BY o.orderid;
Result:
Codeigniter code I tried to run:
public function getorderself(){
$this->db->select_sum('quantity');
$this->db->select('orders.orderid','location','status','pay','total');
$this->db->from ('orders');
$this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
$this->db->where('userid', $_SESSION["userid"]);
$this->db->GROUP_BY('orders.orderid');
$query = $this->db->get ();
return $query;
}
Thank you.
Update: I changed a bit my code, it can return data but only the orderid.
Code i used in view:
<?php
if($orderdata->num_rows() > 0)
{
foreach($orderdata->result() as $row)
{
?>
<tr>
<td><?php echo $row->orderid; ?></td>
<td><?php echo $row->location; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->pay; ?></td>
<td><?php echo $row->total; ?></td>
<td><?php echo $row->quantity; ?><td>
<td><button type="button" href="#" class="delete_data delButton" orderid="<?php echo $row->orderid; ?>">Delete</button></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="7" align="center">No Data Founds</td>
</tr>
<?php
}
?>
Code I used in maim.php
public function vieworderself()
{
$this->load->view('header');
$this->model->caltotal();
$data["orderdata"]=$this->model->getorderself();
$this->load->view('/orders/vieworderself', $data);
$this->load->view('footer');
}
Try using table name before every column. Eg. orders.location
public function getorderself(){
$this->db->select_sum('quantity');
$this->db->select('orders.orderid','orders.location','orders.status','orders.pay','orders.total');
$this->db->from ('orders');
$this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
$this->db->where('orders.userid', $_SESSION["userid"]);
$this->db->GROUP_BY('orders.orderid');
$query = $this->db->get ();
return $query;
}

How to display data in view of the CodeIgniter?

Unable to display the data in a CodeIgniter view. Need to print the builders_name in a CodeIgniter view. So far I have the following code:
Model :
public function get_builders()
{
$query1 = $this->db->select('name')->from('builders')->where("name LIKE 'a%'")->limit(3)->get();
$query2 = $this->db->select('name')->from('builders')->where("name LIKE 'b%'")->limit(3)->get();
$result1 = $query1->result();
$result2 = $query2->result();
return array($result1, $result2);
}
Controller:
public function index(){
$this->load->database();
$data['h']= $this->Builders_Model->get_builders();
$this->load->view('home', $data);
}
View:
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Don't like this try it
public function get_builders()
{
$query1 = $this->db->select('name')
->from('builders')
->like('name','a')
->or_like('name','b')
->limit(3)->get();
$result = $query1->result();
return $result;
}
views code
<?php foreach ($h as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>
Hope fully works . Thank You
What you pass to the view in Codeigniter is accessible by its name. Just <php echo $h; ?> and you should be good to go
you can use :
<?php
foreach($h as $row){?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
}?>
You have to use $h variable in foreach for notation :
<?php
<tr>
<td><?php echo $rowDetail->name; ?></td>
</tr>
}
?>
First this is you haven't loaded the Builder_modal in your controller try to do some thing like
$this->load->model('Builder_model');
after $this->load->database();
Next thing in your view you have to echo like
echo $h; or var_dump($h) if you have an array
It's really working
model
public function get_builders()
{
$this->db->select('name');
$this->db->from('builders');
$this->db->like('name','a');
$this->db->or_like('name','b');
$this->db->limit(3);
$query = $this->db->get();
return $query->result();
}
controller
public function index(){
$this->load->model('Builder_model');
$data= $this->Builders_Model->get_builders();
$this->load->view('home', ['data'=>$data]);
}
view
<?php foreach ($h->result as $row) { ?>
<tr>
<td><?php echo $row->name;?></td>
</tr>
<?php } ?>

Unable to call fetch data from controller to view in codeigniter

I was unable to show data on view from the database using controller and model in CodeIgniter.
Controller Code:
class Leads extends CI_Controller {
public function show($id) {
$this->load->model('leads_model');
$leads = $this->leads_model->get_leads($id);
$data['name'] = $leads['name'];
$data['email'] = $leads['email'];
$data['contact'] = $leads['contact'];
$data['referral'] = $leads['referral'];
$data['project_detail'] = $leads['project_detail'];
$data['note'] = $leads['note'];
$this->load->view('layouts/header', $data);
$this->load->view('layouts/sidebar', $data);
$this->load->view('pages/leads', $data);
$this->load->view('layouts/footer', $data);
}
Model code:
class Leads_model extends CI_Model {
public function __construct() {
$this -> load -> database();
}
public function get_leads($id) {
if ($id != FALSE) {
$query = $this -> db -> get('leads', array('lead_id' => $id));
return $query -> row_array();
}
else {
return FALSE;
}
}
view code:
<td>
<?php echo $data['name']; ?>
</td>
<td>
<?php echo $data['email']; ?>
</td>
<td>
<?php echo $data['contact']; ?>
</td>
<td>
<?php echo $data['referral']; ?>
</td>
<td>
<?php echo $data['project_detail']; ?>
</td>
<td>
<?php echo $data['note']; ?>
</td>
$leads is an array so there should be foreach loop in the view
In controller :
public function show($id)
{
$this->load->model('leads_model');
$leads = $this->leads_model->get_leads($id);
$data['leads'] = $leads;
$this->load->view('layouts/header', $data);
$this->load->view('layouts/sidebar', $data);
$this->load->view('pages/leads', $data);
}
Your Model :
public function get_leads($id) {
if ($id != FALSE) {
$this->db->where(array('lead_id' => $id));
$query = $this->db->get('leads');
return $query->row_array();
}
else {
return FALSE;
}
}
Your view should be like this:
<?php if ($leads) {
foreach($leads as $lead) {?>
<?php echo $lead['name']; ?>
<?php echo $lead['email']; ?>
<?php echo $lead['contact']; ?>
<?php echo $lead['referral']; ?>
<?php echo $lead['project_details']; ?>
<?php echo $lead['note']; ?>
}
}?>
if single row data: Use variable without $data in your view
if your model method return $query->row();
Your view :
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $contact; ?></td>
<td><?php echo $referral; ?></td>
<td><?php echo $project_detail; ?></td>
<td><?php echo $note; ?></td>
for more : https://www.codeigniter.com/user_guide/database/query_builder.html

fetch id from view and pass to another controller

i have 2 table as follows
project
id | name | address
boq
id | project_id |item_no | description
project_id is the foreign key. then i have loaded the all data from project table.then i want to load all the data in to boq table which related to project id when click on the project name. here is my code.but it doesn't work.any one can plz help me.
controller
function show_boq() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['boq'] = $this->project_list_model->show_boq($project_id);
$this->load->view('boq_doc', $data);
}
model
function show_boq($id){
$this->db->select('*');
$this->db->from('boq');
$this->db->where('project_id',$data);
$this->db->join('project', 'project.id = boq.project_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
view
<?php foreach ($boq as $boq): ?>
<tr>
<td><?php echo $boq->id; ?></td>
<td><?php echo $boq->item_no; ?></td>
<td><?php echo $boq->description; ?> </td>
</tr>
<?php endforeach; ?>
Use Model like this. Use project table as first and boq table as second.
function show_boq($data){
$this->db->select('*');
$this->db->from('project');
$this->db->join('boq', 'project.id = boq.project_id');
$this->db->where('project.id',$id);
$query = $this->db->get();
$result = $query->result();
}
And view file is
<?php
if (is_array($boq) || is_object($boq))
{
foreach ($boq as $row): ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->item_no; ?></td>
<td><?php echo $row->description; ?> </td>
</tr>
<?php endforeach;
}
?>

delete and update specific row in codeigniter

I am new in codeigniter.In my view page I am showing the data from database in a table i have two buttons in table to edit and delete the row..I want to delete a specific row from database through id.
## view
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>last name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td><button name="edit">Edit</button></td>
<td><button name="delete">Delete</button></td>
</tr>
<?php } ?>
</tbody>
----------
## Controller ##
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message',$data);
}
public function register()
{
$this->load->view("register");
}
public function receive()
{
$fname= $this->input->post("fname");
$this->load->model('Model');
$this->Model->insertdata($fname);
// echo $this->input->post("fname");
// echo $this->input->post("lname");
// echo $this->input->post("password");
// echo $this->input->post("Email");
}
public function all_user(){
$this->load->model('Model');
$data['records'] = $this->Model->get_all_users();
$this->load->view("store_data",$data);
}
public function row_delete(){
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);
}
}
----------
## Model ##
class Model extends CI_Model{
public function insertdata($fname)
{
$data = array
('fname'=> $this->input->post("fname"),
'lname' => $this->input->post("lname"),
'email' => $this->input->post("email"),
'password' => $this->input->post("password"),
);
$this->db->insert('register',$data);
}
public function get_all_users()
{
$query= $this->db->get('register');
return $query->result();
}
public function delete_row()
{$this->db->where('id', $id);
$this->db->delete('');
}
}
Try this in ur code
//view
<tbody>
<?php for ($i = 0; $i < count($records); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $records[$i]->fname; ?></td>
<td><?php echo $records[$i]->lname; ?></td>
<td><?php echo $records[$i]->email; ?></td>
<td>Delete</td> <td>Edit</td>
</tr>
<?php } ?>
</tbody>
make sure u pass the record id to the view and base_url is configured on config file
//controller
public function row_delete(){
if(isset($_GET['id'])){
$id=$_GET['id'];
$this->load->model('Model');
$this->mod1->row_delete($id);
redirect($_SERVER['HTTP_REFERER']);}
}
//model
public function delete_row()
{
$this->db->where('id', $id);
$this->db->delete('register');
}
reference links here and here
Just pass the parameters to the where function and table name to delete function:
public function delete_row()
{
// Set where
$this->db->where('id', $id);
// Run query
return $this->db->delete('register');
}
Cheers

Categories