codeigniter - Message: Undefined variable: ques - php

I'm pretty new to Codeigniter and tried to follow the Query builder however getting an error.
The view window:
<div class="col-lg-4">
<div class="bs-component">
<div class="list-group">
<li class="list-group-item"><b><font color="white">EXAMBANK DATABASE</b>
</li>
<li class="list-group-item"> <span class="badge">9</span>
Total Subjects:
</li>
<li class="list-group-item"> <span class="badge"><?php echo $ques; ?></span>
Questions In Database:
</li>
</div>
</font>
</div>
</div>
Controller:
function get_total_questions()
{
$this->load->model('control/Student_model');
$question = $this->uri->segment(5);
$this->Student_model->get_all_question($question);
}
Model:
function get_all_questions()
{
$this->db->select('*');
$this->db->from('questionbank');
$this->db->where('question', $question);
$ques = $this->db->count_all_results();
return $ques->result();
}
Any suggestions??

You need to return $ques; from your model and you need to pass $question in your argument
Model:
function get_all_questions($question)
{
$this->db->select('*');
$this->db->from('questionbank');
$this->db->where('question', $question);
$ques = $this->db->count_all_results();
return $ques;
}
You need to load your view and pass your variable to views
Controller
function get_total_questions()
{
$this->load->model('control/Student_model');
$question = $this->uri->segment(5);
$data['ques']=$this->Student_model->get_all_question($question);
$this->load->view('student/list',$data)
}

Please try this way:
Controller:
function get_total_questions()
{
$this->load->model('control/Student_model');
$question = $this->uri->segment(5);
$data['ques'] = $this->Student_model->get_all_question($question);
$this->load->view('folder_name/file_name',$data );
}
Model:
function get_all_questions($question)
{
$this->db->select('*');
$this->db->from('questionbank');
$this->db->where('question', $question);
$ques = $this->db->count_all_results();
return $ques; //not $ques->result() because $this->db->count_all_results() Produces an integer
}

<li class="list-group-item"> <span class="badge"><?php echo #$ques; ?></span>
Questions In Database:
</li>

Related

Merge two arrays into single arrays in php laravel and displaying in blade

I want to filter the datas according to colleges and universities and my query is
$exams = Exam::where('status', 1);
if ($request->filled('universities')) {
$exams = $exams->whereIn('university_id', $request->universities); //data 1st
}
if ($request->filled('colleges')) {
$exams = $exams->whereIn('college_id', $request->colleges); //data 2nd
}
$exams = $exams->get();
And my form is
<form action="">
<ul class="list-group list-group-flush">
#foreach($universities as $university)
<li class="list-group-item">
<a href="#">
<label>
<input name='universities[]' onchange="searchExam();"
class="mr-2" type="checkbox"
value='{{$university->id}}'/>
{{$university->name}}
</label>
</a>
</li>
#endforeach
</ul>
<ul class="list-group list-group-flush">
#foreach($colleges as $college)
<li class="list-group-item">
<a href="#">
<label>
<input onchange="searchExam();" name='colleges[]'
class="mr-2" type="checkbox"
value='{{$college->id}}'/>
{{$college->name}}
</label>
</a>
</li>
#endforeach
</ul>
</form>
My problem is I am getting data for only either universities or colleges but what I want is if colleges and universities options are checked, I want to get data for both colleges and universities. How can I merge these two datas into single array?
You would want to group the wheres for the college or university. You can try something like this:
$exams = Exam::where('status', 1);
if ($request->has('universities', 'colleges')) {
$exams->where(function ($q) use ($request) {
if ($request->input('universities')) {
$q->orWhereIn('university_id', (array) $request->input('universities'));
}
if ($request->input('colleges')) {
$q->orWhereIn('college_id', (array) $request->input('colleges'));
}
});
}
You should be able use orWhereIn eloquent function for this: https://github.com/laravel/framework/blob/f802e8134bc898871cd02398621745cee45739ef/src/Illuminate/Database/Query/Builder.php#L974
Exam::where('status', 1)
->orWhereIn('university_id', $request->universities ?? false)
->orWhereIn('college_id', $request->colleges ?? false)
->get();
This should produce (assuming that table name is exams) something like:
"select * from `exams` where `status` = ? and (`university_id` IN ? or `college_id` IN ?)"
If that doesn't work with your PHP version, you could try adding conditional clauses ( https://laravel.com/docs/master/queries#conditional-clauses)
Exam::where('status', 1)
->when($request->universities, function ($query) use ($request) {
return $query->whereIn('university_id', $request->universities);
})
->when($request->colleges, function ($query) use ($request) {
return $query->whereIn('college_id', $request->colleges);
})
->get();

Trying to get property 'slug' of non-object 5.7

Looking to solve this error that I'm getting when trying to display some data to the view. I'm working with v5.7 and I have a feeling it might be something with the index method in my controller, I could be very wrong. If there is any more info that is needed please let me know.
Trying to get property 'slug' of non-object (0)
Route:
Route::get('/category/{category}','BlogController#category')->name('category');
BlogCategory Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class BlogCategory extends Model
{
protected $fillable = ['title', 'slug'];
public function posts()
{
return $this->hasMany(Post::class);
}
public function getRouteKeyName()
{
return 'slug';
}
}
Post Model
public function category()
{
return $this->belongsTo(BlogCategory::class);
}
Controller:
protected $limit = 3;
public function index()
{
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = Post::with('author')
->latestFirst()
->published()
// ->filter(request()->only(['term','year','month']))
->simplePaginate($this->limit);
return view('pages.frontend.blog.index', compact('posts', 'categories'));
}
public function category(BlogCategory $category)
{
$categoryName = $category->title;
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = $category->posts()
->with('author')
->latestFirst()
->published()
->simplePaginate($this->limit);
return view("pages.frontend.blog.index", compact('posts', 'categories', 'categoryName'));
}
View:
#foreach ($posts as $post)
<article class="post-item">
#if ($post->image_url)
<div class="post-item-image">
<a href="{{ route('blog.show', $post->slug) }}">
<img src="{{ $post->image_url }}" alt="">
</a>
</div>
#endif
<div class="post-item-body">
<div class="padding-10">
<h2>
{{ $post->title }}
</h2>
{!! $post->excerpt_html !!}
</div>
<div class="post-meta padding-10 clearfix">
<div class="pull-left">
<ul class="post-meta-group">
<li>
<i class="fa fa-user"></i>
{{ $post->author->name }}
</li>
<li>
<i class="fa fa-clock-o"></i>
<time> {{ $post->date }}</time>
</li>
<li>
<i class="fa fa-folder"></i>
{{ $post->category->title }}
</li>
<li>
<i class="fa fa-comments"></i>
4 Comments
</li>
</ul>
</div>
<div class="pull-right">
Continue Reading ยป
</div>
</div>
</div>
</article>
#endforeach
Post table
Blog Cats table
The belongsTo function accepts a second argument for the name of the foreign key in your posts table, if you do not provide it the framework will try to guess what is the foreign key column name giving the name of the function as pattern, in your case category(), so the framework is searching for category_id, however, your foreign key column name is blog_category_id.
public function category()
{
return $this->belongsTo(BlogCategory::class, 'blog_category_id');
}
You should call the category relationship in the index of your controller, if this view is related to the index method!
public function index()
{
$categories = BlogCategory::with(['posts' => function ($query) {
$query->published();
}])->orderBy('title', 'asc')->get();
$posts = Post::with('author')
->category()
->latestFirst()
->published()
// ->filter(request()->only(['term','year','month']))
->simplePaginate($this->limit);
return view('pages.frontend.blog.index', compact('posts', 'categories'));
}

LINK PREVIOUS AND NEXT POST in a php blog

I'm a new developer and I'm trying to make work my very simple blog.
I want to set a previous and a next link to my previous and next articles in the blog. This is my current code.
POSTS CONTROLLER
public function move($id)
{
$post = DB::table('posts')->find($id);
$previous = DB::table('posts')->where('id', '<', $post->id)->max('id');
$next = DB::table('posts')->where('id', '>', $post->id)->min('id');
return view('posts.show')->with('previous', $previous)->with('next', $next);
}
WEB.PHP
<?php
Route::get('/', 'PostsController#index')->name('home');
Route::get('/posts/create', 'PostsController#create');
Route::post('/posts', 'PostsController#store');
//Route::get('/posts/{post}', 'PostsController#show');
Route::get('/posts/tags/{tag}', 'TagsController#index');
Route::post('/posts/{post}/comments','CommentsController#store');
Route::get('/posts/{id}/edit', 'PostsController#edit');
Route::get('/edit/{post}', 'PostsController#update');
Route::patch('/post/{post}', 'PostsController#update');
Route::get('/register', 'RegistrationController#create');
Route::post('/register', 'RegistrationController#store');
Route::get('/login', 'SessionsController#create');
Route::post('/login', 'SessionsController#store');
Route::get('/logout', 'SessionsController#destroy');
Route::get('/posts/{id}', 'PostsController#move');
SHOW.BLADE
#extends ('layouts.master')
#section ('content')
<div class="col-sm-8 blog-main">
<h1> {{$post->title}}</h1>
#if (count($post->tags))
<ul>
#foreach($post->tags as $tag)
<li>
<a href="/posts/tags/{{ $tag->name}}">
{{ $tag->name }}
</a>
</li>
#endforeach
</ul>
#endif
{{$post->body}}
<hr>
Modifica
<hr>
<div class='comments'>
<ul class="list-group">
#foreach ($post->comments as $comment)
<li class="lista-commenti">
<strong>
{{$comment->created_at->diffForHumans()}}:
</strong>
{{ $comment -> body}}
</li>
#endforeach
</ul>
</div>
<hr>
<div>
<div>
<form method="POST" action="/posts/{{$post->id}}/comments">
{{csrf_field()}}
<div>
<textarea name="body" placeholder="Il tuo commento" class="form-control" required></textarea>
</div>
<div>
<button type="submit" class="bottone">Invia Commento</button>
</div>
</form>
#include('layouts.errors')
</div>
<div class="row">
<ul>
<li> Previous</li>
<li> Next
</li>
</ul>
</div>
</div>
</div>
#endsection
POST.PHP
<?php
namespace App;
use Carbon\Carbon;
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function addComment($body)
{
$user_id= auth()->id();
$this->comments()->create(compact('user_id','body'));
}
public function scopeFilter($query, $filters)
{
if(!$filters)
{
return $query;
}
if ($month = $filters['month'])
{
$query->whereMonth('created_at', Carbon::parse($month)->month);
}
if ($year = $filters['year']) {
$query->whereYear('created_at', $year);
}
}
public static function archives()
{
return static::selectRaw('year(created_at) year, monthname(created_at) month, count(*) published')
->groupBy('year','month')
->orderByRaw('min(created_at) desc')
->get()
->toArray();
}
public function tags(){
return $this->belongsToMany(Tag::class);
}
}
This gives me an error about the undefined variables previous and next and also about the www.
Sorry but this is my first post and I can't upload any images. Hope someone can help me.
Thanks
Alessandro
use url() helper method:
<li> Previous</li>
<li> Next</li>
Edit: remove this Route::get('/posts/{post}', 'PostsController#show'); line or change your code like move method in your show method.
For your new error, add this line at top-
use Illuminate\Support\Facades\DB;
When you're going to post/1, you're executing the show method and not move.
Also, change the code to:
<li> Previous</li>
<li> Next</li>
You've said, "and also about the www". I'm pretty sure you're not getting the error about the $previous or $next but you get the error about the www... because you're trying to use text as variable in your code.

view can't find variable (laravel)

I have the following action in my controller:
public function viewPost($id)
{
$current_post = forum_post::with('replies')->where('id', $id)->get();
return view('forumViewPost',['currentPost', $current_post]);
}
Then I have the following view:
#extends('layout.app')
#section('content')
<div class="container">
<div class="nk-gap-2"></div>
<ul class="nk-forum nk-forum-topic">
<li>
<div class="nk-forum-topic-author">
<img src="assets/images/avatar-2-sm.jpg" alt="Kurt Tucker">
<div class="nk-forum-topic-author-name" title="Kurt Tucker">
{{$currentPost->nickname}}
</div>
<div class="nk-forum-topic-author-role">
Member
</div>
</div>
<div class="nk-forum-topic-content">
{{$currentPost->content}}
</div>
<div class="nk-forum-topic-footer">
<span class="nk-forum-topic-date">June 19,
2017</span> <span class="nk-forum-action-btn"> Reply</span>
<span class="nk-forum-action-btn"> Spam</span>
<span class="nk-forum-action-btn"><span class="nk-action-heart liked"><span class="num">1</span>
Like</span></span>
</div>
</li>
#foreach($currentPost->replies as $replies)
#endforeach
</ul>
</div>
#endsection
now when I run this. I get the following error:
Undefined variable: currentPost
Can anyone tell me what I've done wrong?
If you use [] you may write this ['currentPost' => $current_post]
All
public function viewPost($id)
{
$current_post = forum_post::with('replies')->where('id', $id)->get();
return view('forumViewPost',['currentPost' => $current_post]);
}
You can also you compact(''); and as Wreigh mentioned change $current_post to $currectPost
public function viewPost($id)
{
$currentPost = forum_post::with('replies')->where('id', $id)->get();
return view('forumViewPost', compact('currentPost'));
}

codeigniter template header white Data

how can I embed a foreach with my header?
I have already tried as in the code.
But with the output I get only "Undefined variable: header_warenkorbs" raus.
Although I am involved in the controller with the value data.
What am I doing wrong?
Controller:
<?php
class Warenkorb extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function header()
{
$data['header_warenkorbs'] = $this->Admin_model->header_warenkorbs();
$this->load->view('templates/header', $data);
// $this->load->view('test/index');
// $this->load->view('templates/footer');
}
}
Model:
<?php
class Warenkorb_model extends CI_Model
{
public function header_warenkorbs()
{
$this->db->select('*');
$this->db->from('db_artikel');
$query = $this->db->get();
return $query->result_array();
}
}
View:
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<span class="glyphicon glyphicon-shopping-cart"></span><span class="cart-items-count"><span class=" notification-counter">243</span></span>
<ul class="dropdown-menu dropdown-cart" role="menu">
<?php foreach($header_warenkorbs as $warenkorb): ?>
<li>
<span class="item">
<span class="item-left">
<span class="item-info">
<span>Item name</span>
<span>23$</span>
</span>
</span>
<span class="item-right">
<button class="btn btn-xs btn-danger pull-right">x</button>
</span>
</span>
</li>
<?php endforeach; ?>
<li class="divider"></li>
<li><a class="text-center" href="<?php echo base_url(); ?>warenkorb/index">Warenkorb</a></li>
</ul>
</li>
</ul>
Unless it is a typo or you're not showing us something the problem could be with this line.
$data['header_warenkorbs'] = $this->Admin_model->header_warenkorbs();
The model code you show is Warenkorb_model not Admin_model. Seems like the above code should maybe be
$data['header_warenkorbs'] = $this->Warenkorb_model->header_warenkorbs();
You don't show where you load the model. It is loaded somewhere... right? It is common practice to load models in the constructor of the Controller that uses the model.
class Warenkorb extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Warenkorb_model');
}
// other code
}
Do you realize you are not actually doing anything with the $header_warenkorbs in your foreach loop? You never use $warenkorb.
you need to echo it (example:)
<?php
echo '<table>
<tr>
<td>Name</td>
<td> .$name.</td>
</tr>
</table>';
?>
just edit it to your needs, im too lazy

Categories