How to display data in view of the CodeIgniter? - php

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 } ?>

Related

Codeigniter : How to change result array [duplicate]

This question already has an answer here:
looping in php codeigniter views
(1 answer)
Closed 3 years ago.
How to change result array like this
to like this
this is my Controller
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$arrData = json_decode($responseData,true);
echo "<pre>";
print_r($arrData);
echo "</pre>";
$this->load->view('welcome_message');
}
controller
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$arrData = json_decode($responseData,true);
$data = array();
$data['arrData'] = $arrData;
$this->load->view('welcome_message',$data);
}
view code
First you need to set header then this
<?php foreach($arrData as $data){ ?>
<tr>
<td><?php echo $data['user_firstname'] ?></td>
</tr>
<?php } ?>
controller: First of all pass data to your view from your controller:
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$arrData = json_decode($responseData,true);
// Create an Data Array to store all the data
// that you need to pass to your view
$data['user'] = $arrData;
$this->load->view('welcome_message', $data);
}
view: Then in your view file create a table and make it dynamic to use the data that need to be shown:
<?php foreach($user as $key => $value) : ?>
<tr>
<td><?= $value['user_firstname'] ?></td>
<td><?= $value['user_lastname'] ?></td>
...
..
.
</tr>
<?php endforeach; ?>
Do like this in the HTML table
$data['arrData'] = json_decode($responseData,true);
Pass the data in the view $this->load->view('welcome_message', $data);
<?php foreach($arrData as $data): ?>
<tr>
<td><?= $data['user_firstname'] ?></td>
</tr>
<?php endforeach; ?>
Change the controller to pass the $arrData data :
public function index(){
$response = Requests::get('https://xsintern-api.learnbalance.com/api/users/');
$responseData = $response->body;
$data['arrData'] = json_decode($responseData,true);
$this->load->view('welcome_message', $data);
}
and try like this within the welcome_message.php view file :
<table class="table table-hover">
<thead>
<tr>
<th>The Title</th>
</tr>
</thead>
<tbody>
<?php foreach($arrData as $data): ?>
<tr>
<td><?= $data['user_firstname'] ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Codeigniter blog: display post title in comments table

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>

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

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

Fetching the values from db before searching

In this below code when i execute it .It shows all the values from the database and when i search it is searching .But my expected result is when i execute it should not display data from the database and only when i search it should display the result.
Controller search1_site.php
<?php
class Search1_site extends ci_controller
{
function index()
{
$data = array();
$keyword = $this->input->post('keyword');
$data['results'] = $this->search1_model->search($keyword);
$this->load->view('result_view', $data);
}
}
?>
model search_model.php
<?php
class Search_model extends CI_Model
{
function search($keyword)
{
$this->db->like('course_code', $keyword);
$query = $this->db->get('coursemaster');
return $query->result();
}
}
?>
view result_view.php
<form action="<?php echo site_url('search1_site/index');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" id="opn" value = "Search" />
</form>
<table>
<tr>
<th>course_code</th>
<th>course name</th>
</tr>
<?php
foreach ($results as $row) {
?>
<tr>
<td><?php echo $row->course_code;?></td>
<td><?php echo $row->course_name;?></td>
</tr>
<?php
}
?>
</table>
In controller :
class Search1_site extends ci_controller
{
function index()
{
$data = array();
$keyword = $this->input->post('keyword');
if($keyword!=""){
$data['results'] = $this->search1_model->search($keyword);
}
$this->load->view('result_view', $data);
}
}
in view :
<?php
if($results){
foreach ($results as $row) {
?>
<tr>
<td><?php echo $row->course_code;?></td>
<td><?php echo $row->course_name;?></td>
</tr>
<?php
}
}else{
//Display somthing
}
?>
When your page loads the keyword in the following line in your controller is going blank and its searching all the records as your query might be like %%.
$keyword = $this->input->post('keyword');
First check if $keyword is not blank. If its not blank then search the data.
do something like:
if ( isset( $_POST['keyword'] ) ) { ... }

Categories