Right, i have:
Controller/PostsController.php
Model/Post.php
View/Posts/index.ctp
the controller finds all the posts in a database and then passes the values into my view/Posts/index.ctp, where i foreach though each post.
I now want to be able to access the same posts but on another controller and view. How do i do this?
PostsController is;
class PostsController extends AppController {
public function index() {
$this->set('posts', $this->Post->find('all'));
}
}
View/Posts/index.ctp is;
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php
echo $this->Html->link(
$post['Post']['title']
);
?>
</td>
<td>
<?php echo $post['Post']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Now in my AboutsController and Abouts/index.ctp i want to be able to access the same data so that i can show the posts on the view/abouts/index.ctp
when i make the view/abouts/index.ctp, have the same code as View/Posts/index.ctp i get the error message;
Undefined variable: posts [APP\View\Abouts\index.ctp, line 12]
This is because i do not know how to make the data accessible in my Abouts/Controller.
For the global variable , i suggest to set it un the appController from which you can access into it wherever you are, in the appController :
class AppController extends Controller {
var $uses = array('Post'); // to use the model
public function beforeFilter(){
$this->set('posts', $this->Post->find('all'));
}
}
then you can get posts in any view ...
Related
the image from the database becomes null and does not appear on the website. in other case, the same image from the same database are appear. there are no mistyped variables, but can't load on the web. I've been trying to put the namespace where the image is, but still not working.
I'm still very new to codeigniter, i've tried my best to explain with my poor english.
please response if you have the answer, thankyou
InvenModel getinfo() function
<?php
namespace App\Models;
use CodeIgniter\Model;
class InvenModel extends Model
{
public function getinfo($slug = false)
{
if ($slug == false) {
return $this->findAll();
}
return $this->where(['slug' => $slug])->first();
}
}
?>
<?php
namespace App\Controllers;
use App\Models\InvenModel;
class inventori extends BaseController
{ public function detail($slug)
{
$data = [
"title" => 'Detail Produk',
"invent" => $this->InvenModel->getinfo($slug)
];
return view('inventori/detail', $data);
}
}
?>
<tr>
<td> <img src="<?= $invent['img']; ?>" class="img"></td> <!-- here the error message appears -->
<td> <?= $invent['nama']; ?> </td>
<td> <?= $invent['stok']; ?> </td>
<td> <?= $invent['terjual']; ?> </td>
<td> Edit </td>
<td> hapus </td>
</tr>
I am a beginner in Codeigniter framework and I am also learning from youtube
In my admin.php code is
<?php
class Admin extends MY_Controller {
public function dashboard(){
$this->load->model('articlesmodel');
$articles = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard', ['articles'=> $articles]);
}
}
?>
And in my dashboard.php code is
<table class="table">
<thead>
<th>Sr. No</th>
<th>Title</th>
<th>Action</th>
</thead>
<tbody>
<?php if ( count($articles) ):?>
<?php foreach($articles as $article): ?>
<tr>
<td>1</td>
<td>
<?= $article->title ?>
</td>
<td>
Edit
Delete
</td>
</tr>
<?php endforeach; ?>
<?php else:?>
<tr>
<td colspan="3">No Records Found</td>
</tr>
<?php endif;?>
</tbody>
</table>
I am fetching the articles from my database in table format, but the following error is presented: Undefined variable: articles in dashboard.php page
change My_Controller to CI_Controller in Admin controller class definition.
When creating a controller class in Codeigniter, you should always extend CI_Controller class. Because $this->load() function is implemented in CI_Controller class. See the documentation
As a best practice create an array and pass it into view() function instead of initializing an array within view() function parameters. ex:
$data['articles'] = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard',$data);
controller
change these lines
$articles = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard', ['articles'=> $articles]);
to this
$data['articles'] = $this->articlesmodel->articles_list();
$this->load->view('admin/dashboard',$data);
Tip:
try to look at documentation syntax
https://www.codeigniter.com/user_guide/general/views.html
I have to get user_name using user_id in my view.
Following is my mode:
// Model
class main_model extends CI_Model {
public function getOrders() {
$this->db->where( 'real', 1 );
$this->db->ORDER_BY( 'id', 'DESC' );
$query = $this->db->get('orders',10);
return $query->result();
}
public function getUsername($id) {
$this->db->where( 'id', $id );
$query = $this->db->get('users');
return $query->result();
}
// And my Controller goes here below:
public function index() {
$this->load->model('main_model');
$data['orders'] = $this->main_model->getOrders();
$this->load->view('index', $data);
}
// Following is my View file.
<div class="container-fluid">
<div class="row">
<table class="table table-responsive" border="3">
<tr>
<th>OID</th>
<th>user_id</th>
<th>Username</th>
</tr>
<?php foreach($orders as $order): ?>
<tr>
<td> <?= $order->id;?> </td>
<td> <?= $order->uid;?> </td>
<td> ? </td> <!-- Want to get user name using uid. -->
</tr>
<?php endforeach; ?>
</table>
</div>
</div>
I have got user_id from my order table, and user name is on my users table, thus I have to get it from there. Can anyone help please.
You can get user name by single query.
Simple change on getOrders() function in main_model
class main_model extends CI_Model {
public function getOrders() {
$this->db->select('orders.*,(select u.user_name from users as u where u.id = orders.uid) as user_name');
$this->db->where('real', 1 );
$this->db->ORDER_BY( 'id', 'DESC' );
$query = $this->db->get('orders',10);
return $query->result();
}
<tr>
<td> <?= $order->id;?> </td>
<td> <?= $order->uid;?> </td>
<td> <?= $order->user_name;?></td>
</tr>
First, get your user id from getOrders() call, then loop through your orders to get your user. Then from Your getUsername() function in the model returns a user object, so in the controller call it and store the variable:
$orders = $this->main_model->getOrders();
foreach($orders as $order) {
$user = $this->main_model->getUsername($uid);
$order->username = $user->username;
}
$data['orders'] = $orders;
Now the username is in your orders array, just add <td> <?= $order->username;?> </td>
perhaps something like this:
MODEL
public function getUsername($id) {
$this->db->where( 'id', $id );
$query = $this->db->get('users');
return $query->row();
}
VIEW
<?php foreach($orders as $order):
$username = $this->main_model->getUsername($order->uid);
?>
<tr>
<td> <?= $order->id; ?> </td>
<td> <?= $order->uid; ?> </td>
<td> <?= $username->user_name; ?> </td>
</tr>
<?php endforeach; ?>
I have two Models:
1. Course.
2. Fee.
One course has only one fee. While giving input it's completely ok but when I tried to access the fee data, it's showing nothing in fee's column . How do I solve it?
Course Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $table='courses';
protected $fillable = ['name'];
public function fee(){
return $this->belongsTo('App\Fee');
}
}
Fee Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Fee extends Model
{
protected $table='fees';
protected $fillable = ['fee','course_id'];
public function course()
{
return $this->hasOne('App\Course');
}
}
Controller:
public function listofCourse(){
$course=\App\Course::all();
return view('listofCourse',compact('course'));
}
View Page:
<html>
<head>
<title> Course Details </title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<div class="container">
<h3> Student Details </h3>
<table class="table table-striped table-bordered" id="example">
<thead>
<tr>
<td>Serial No</td>
<td>Course Name</td>
<td>Course Fee</td>
</tr>
</thead>
<tbody>
<?php $i=1; ?>
#foreach($course as $row)
<tr>
<td>{{$i}}</td>
<td>{{$row->name }}</td>
<td> #if($row->course)
{{$row->course->fee}}
#endif</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
</div>
</body>
</html>
It seems that you have written the relationship wrongly... just do this.
Keep in Mind, Course Has The Fee means Course is must, so relation should start from Course side towards Fee.
Your Course Model
class Course extends Model
{
protected $table='courses';
protected $fillable = ['name'];
public function fee(){
return $this->hasOne('App\Fee','course_id', 'id');
}
}
Your Fee Model
class Fee extends Model
{
protected $table='fees';
protected $fillable = ['fee','course_id'];
public function course()
{
return $this->belongsTO('App\Course', 'course_id', 'id');
}
}
Now you can get the relationship by doing this.
public function listofCourse(){
$course=\App\Course::with('fee')->get();
// doing this for dumping purpose
echo "<pre>";
print_r($course->toArray()); // you will see the `fee` array
echo "</pre>";
die();
return view('listofCourse',compact('course'));
}
I have a post table and a comment table and I want to show a list of my comments. But for showing the title of post for each comment I get thrown this error:
Trying to get property of non-object (View:\myBlog\app\views\comments\list.blade.php)
I am really confused about it.
This is my controller:
public function listComment()
{
$comments = Comment::orderBy('id', 'desc')->paginate(20);
$this->layout->title = 'Comment Listings';
$this->layout->main = View::make('admin.index')->nest('content', 'comments.list', compact('comments'));
}
This is my comments/list.blade.php
<tbody>
#foreach($comments as $comment)
<tr>
<td>{{{$comment->commenter}}}</td>
<td>{{{$comment->email}}}</td>
<!--<td>{{$comment->post->title}}</td>-->
<td>
{{Form::open(['route'=>['comment.update',$comment->id]])}}
{{Form::select('status',['yes'=>'Yes','no'=>'No'],$comment->approved,['onchange'=>'submit()'])}}
{{Form::close()}}
</td>
<td>{{HTML::linkRoute('comment.delete','Delete',$comment->id)}}</td>
<td>{{HTML::linkRoute('comment.show','Quick View',$comment->id,['data-reveal-id'=>'comment-show','data-reveal-ajax'=>'true'])}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$comments->links()}}
What is my wrong with passing data to comments/list.blade.php?