Here's the issue : CodeIgniter loads data from the controller but not from the view
When I go the the source of the page and click the form action link its load data in the source code but not in the html view.
--view
<table class="table hovered">
<thead>
<tr>
<th class="text-left">User ID</th>
<th class="text-left">Type</th>
<th class="text-left">Name</th>
<th class="text-left">Email</th>
<th class="text-left">Phone</th>
</tr>
</thead>
<tbody>
<?php echo form_open('admin/manage_customer') ?><!-- start of the form -->
<?php if(isset($records)) : foreach($records as $row) : ?>
<tr>
<td class="right"><?php echo $row->cus_id; ?></td>
<td class="right">Admin</td>
<td class="right"><?php echo $row->cus_name; ?></td>
<td class="right"><?php echo $row->cus_email; ?></td>
<td class="right"><?php echo $row->cus_phone; ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php echo form_close(); ?><!-- END of the form -->
</tbody>
<tfoot></tfoot>
</table>
---controller
function inbox()
{
$data = array();
if($query = $this->mod_contactus->get_records())
{
$data['records'] = $query;
}
$this->load->view('admin/admin_messages',$data);
}
---Model
<?php
class Mod_contactus extends CI_Model //Users (model name) share all the class and functions CodeIgniter models have
{
function get_records()
{
$query = $this->db->get('tbl_contactus');
return $query->result();
}
function add_record($data)
{
$this->db->insert('tbl_contactus', $data);
return;
}
function update_record()
{
}
}
i would go with hussain in this. first check if the necessary data is returned by the model or not. Only then you should check the html structure. But even if there is a problem with structure, something should be displayed. So, check the flow of data to the view first.
Related
I am new in cakephp 3. Now, I am fetching data from database which is successfully show all table data but I want to show 'No record found' message if table is empty. So, How can I do this ?Please help me.
Controller: PostsController
<?php
namespace App\Controller;
use App\Controller\AppController;
Class PostsController extends AppController {
Public function index(){
$this->set('data',$this->Posts->find('all'));
}
}
?>
Layout: Index.cpt
<div class="row">
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($data))
{
foreach($data as $row)
{
?>
<tr>
<td><?php echo $row->title; ?></td>
<td><?php echo $row->description; ?></td>
<td></td>
</tr>
<?php
}
}
else
{
echo '<p>No record found</p>';
}
?>
</tbody>
</table>
</div>
As find('all') method in cakephp return object, it wont be empty if check normally.
Instead of
if(!empty($data))
Try this line
if (!$data->isEmpty()) {
Should work.
Please find updated documentation link.
This is the kind of error that I'm facing:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: fetch_data
Filename: pages/instructors.php
Line Number: 126"
Here's my instructor.php line of codes:
<tr>
<th>ID</th>
<th>Last Name</th>
<th>First Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if($fetch_data) {
foreach ($fetch_data->result() as $row) {
?>
<tr>
<td><?php echo $row->ins_id; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3">No data found</td>
</tr>
<?php
}
?>
</tbody>
</table>
my Controller:
class Add_instructor extends CI_Controller{
public function instructor(){
$this->load->model('instructor_model');
$data["fetch_data"] = $this->instructor_model->fetch_data();
$this->load->vars($data);
$this->load->view('templates/header');
$this->load->view('pages/add_instructor', $data);
$this->load->view('templates/footer');
}
}
And my model:
class instructor_model extends CI_Model {
function fetch_data(){
$query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
return $query;
}
}
Change your controller code as below. It will work.
Controller code
class Add_instructor extends CI_Controller{
public function instructor(){
$this->load->model('instructor_model');
$data["fetch_data"] = $this->instructor_model->fetch_data();
$this->load->view('templates/header');
$this->load->view('pages/instructor', $data);
$this->load->view('templates/footer');
}
}
Model Code:-
class instructor_model extends CI_Model {
function fetch_data(){
$query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
return $query->result();
}
}
View Code:-
<tr>
<th>ID</th>
<th>Last Name</th>
<th>First Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if($fetch_data) {
foreach($fetch_data as $row) {
?>
<tr>
<td><?php echo $row->ins_id; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="3">No data found</td>
</tr>
<?php
}
?>
</tbody>
</table>
Hi I am creating a view where I will be displaying the list of records from database along with boxes where a user selects checkboxes and click on Activate button. In the database I have given a separate column for featured blogs once user click on activate, these id columns should be updated as 1 and rest will be as 0.
View:
<table>
<thead>
<tr>
<th scope="col">Featured Blogs</th>
<th scope="col">S.No</th>
<th scope="col">Blog Title</th>
<th scope="col">Author</th>
</tr>
</thead>
<tbody>
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php $i=0;foreach($records as $r):$i++;?>
<tr>
<td><input type="checkbox" name="checkbox" value="<?php echo $r->blog_id;?>"
<?php if ($r->featured_blogs == 1) { echo 'checked="checked"'; }else { echo 'disabled="disabled"'; }?> ></td>
<td class="align-center"><?php echo $i;?></td>
<td><?php echo $r->blog_title;?></td>
<td><?php echo $r->username;?></td>
</tr>
<?php endforeach ;endif;?>
</tbody>
</table>
<div class="pagination"><?php echo $links; ?></div>
Activate
</div>
</div>
Controller:
function active()
{
$this->blogs_model->active($this->uri->segment(3));
$this->flash->success('<h2>Successfully Activated the record.<h2>');
redirect('blogs');
}
Model:
function activate($blog_id)
{
$data=array('status'=>1);
$this->db->where(array('blog_id'=>$blog_id));
$this->db->update('blogs',$data);
}
function active()
{
$this->blogs_model->active($this->uri->segment(3));
$this->flash->success('<h2>Successfully Activated the record.<h2>');
redirect('blogs');
}
function active($blog_id)
{
$data=array('status'=>1);
$this->db->where(array('blog_id'=>$blog_id));
$this->db->update('blogs',$data);
}
on your controller you called the model and it's function as active($this->uri->segment(3))
but in your model you called it as function activate($blog_id)
you should need to change it
Updated with current Code*
I thought i would ask for your assistance regarding my issue.
Im wondering how i may go about replacing content that is contained in one column and take the value of another along with its own.
I have the following code and,
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Template</th>
<th>SaleID</th>
<th>Description</th>
<th>Title</th>
<th>Price</th>
<th>Status</th>
<th>User Action</th>
</tr>
</thead>
<tbody>
<?php
$res = $MySQLiconn->query("SELECT * FROM crud WHERE uid='{$_SESSION['userSession']}' ORDER BY id DESC");
while($row=$res->fetch_array())
{
?>
<tr>
<td><!-- THIS COL WILL BE HTML CONTENT A USER UPLOADED ALREADY WHICH SHOULD HAVE THE OTHER VALUES MIXED IN (price, des, title) --></td>
<td><?php echo $row['saleid']; ?></td>
<td><?php echo $row['des']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['price']; ?></td>
<?php
if($row['status']=='Listed')
echo "<td><span class='label label-success'>".$row['status']."</span></td>";
else if($row['status']=='Awaiting')
echo "<td><span class='label label-warning'>".$row['status']."</span></td>";
?>
<td></i> </i> </i></td>
</tr>
<?php
}
?>
</tbody>
</table>
Example:
Model
public function select_data($id) {
$this->db->select(*); //or any fields
$this->db->where('id', $id);
$query = $this->db->get('table_name');
return $query->row_array();
}
Controller:
$data = $this->your_model_name->select_data($id); /id of the specified person
//pass data to the view
$this->load->view('your_view_file', $data);
View:
//display name or description where 'desc' is your filed name from DB
<? echo $data['desc'] ?>
or mix this with your static data:
<p>
My name is <? echo $data['name'] ?>.
</p>
<p>
Description of the person: <? echo $data['desc'] ?>
</p>
I am trying to create a index method for the content model controller and I am getting the following error, I searched here but did not find the relevant answer. Please take a look at the following code and screenshot:
Content.php:
class Content extends AppModel{
public $name = 'Content';
}
ContentsController.php:
class ContentsController extends AppController {
public $name = 'Contents';
public function index() {
$this->set('Contents', $this->Content->find('all'));
}
}
index.ctp:
<h1>View All Content</h1>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Content</th>
</tr>
<?php foreach ($Contents as $content) : ?>
<tr>
<td><?php echo $this->$content['Content']['id']; ?></td>
<td><?php echo $this->$content['Content']['title']; ?></td>
<td><?php echo $this->$content['Content']['content']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Screenshot:
http://postimg.org/image/sslcgoutx/
Please help as I am new to cakephp and learning it.
Try this in index.ctp
<h1>View All Content</h1> <table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Content</th>
</tr>
<?php foreach ($Contents as $content) : ?>
<tr>
<td><?php echo $content['Content']['id']; ?></td>
<td><?php echo $content['Content']['title']; ?></td>
<td><?php echo $content['Content']['content']; ?></td>
</tr>
<?php endforeach; ?>
</table>