codeigniter passing count_all_results to view - codeigniter

I'm using the count_all_results() function to return a user's number of languages spoken. But when I try to pass the number to the view, I keep getting a php undefined variable (for $lang_cnt). Below is my code:
Model
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Controller
function showLangCount() {
$data['lang_cnt'] = $this->language_model->countLanguages($id);
$this->load->view('lang_view', $data);
}
View
<p>This user speaks <?php echo $lang_cnt; ?> languages.</p>

One problem is that your model function takes two arguments:
function countLanguages($id, $cnt_languages)
But when you call it you are only passing one argument:
$this->language_model->countLanguages($cnt_languages);
And an even bigger problem, as Rocket points out, is that countLanguages doesn't return anything. Try this:
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}

Always check your model functions if they return value or not. Try this:
function showLangCount() {
if($this->language_model->countLanguages($id))
{
$data['lang_cnt'] = $this->language_model->countLanguages($id);
}
else
{
$data['lang_cnt'] = NULL;
}
$this->load->view('lang_view', $data);
}

Its better to use:
return $query->num_rows();
to return the number of rows effected...

Related

Which design pattern helps me in this case using - Laravel

so I have a controller which returns a view with a DB Collection which I have a repository,
now, in case I have a request I want to return an array - json and not a view
any ideas if this is the correct approach?
public function index(
Request $request,
CampaignPerformanceRepository $campaignPerformanceRepository
) {
$data = $campaignPerformanceRepository->getDataByPeriod($request);
if ($request->all()) {
return $campaignPerformanceRepository->getDataByPeriod($request);
}
return view('reports.campaign-performances', compact('data'));
}
I do something similar in cases where I may want to return a view or just json based on the request. There's an easy helper function request()->wantsJson() which you can use.
$data = $campaignPerformanceRepository->getDataByPeriod($request);
if ($request->wantsJson()) {
return $campaignPerformanceRepository->getDataByPeriod($request);
}
return view('reports.campaign-performances', compact('data'));

passing different type variables to the view from controller in laravel

i want to pass two variables called as repcounter and suppliers to the view to do two different task.
here is my function in controller,
public function admin()
{
$suppliers = SupplierData::all();
$repcounter= SalesRep::count();
return view('dashboard', compact('suppliers'));
}
this is how i send suppliers data. it worked fine. but i don't have an idea to send repcounter and suppliers at once.. each time i try i gave error undefined variable.
so how to send this two varibles to the dashboard.blade.php view?
You should try this:
public function admin()
{
$suppliers = SupplierData::all();
$repcounter= SalesRep::count();
return view('dashboard', compact('suppliers','repcounter'));
}
Replace:
return view('dashboard', compact('suppliers'));
With the following code:
return view('dashboard', compact('suppliers,'repcounter'));
You can pass multiple variables to the view by nesting the data you want in an array. See below code.
public function admin()
{
//create an empty array
$response = array();
//nest your data insde the array instead of creating variables
$response['suppliers'] = SupplierData::all();
$response['repcounter'] = SalesRep::count();
return view('dashboard', compact('response'));
}
Inside your View, you can access them as below
$response['suppliers']
$response['repcounter']

Laravel Mutators and Accessors

I have created mutator date function in model to convert the created_at date into human readable time using diffForHumans().
I have done the following
public function setDateAttribute($value){
return \Carbon\Carbon::parse($value)->diffForHumans();
}
It works fine but it affects in all. it is possible to apply this mutator only on the specified function of the controller rather than all function
A small logic in the mutator would do the job:-
public function setDateAttribute($value){
if ( request()->path() === "/yourURL/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
the idea is to check by the url.
getting request path helper
EDIT
Comparison using route name is better, as exact path can contain id/slugs.
public function setDateAttribute($value){
if ( \Route::currentRouteName() === "/yourRouteName/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
getting route name

How to passing value from link in codeigniter

My link in view page
Detail
My controller
public function cari(){
//what i'm doing here ? please help ?
}
My model
function search_by_id($id){
$query = $this->db->where('id',$id);
$query = $this->db->get();
return $query->result();
}
How pass value from link to model and show the result to view again?
it's my first time using codeigniter, need some help
You have to pass argument into method:
public function cari($hasil_id){
if ((int)$hasil_id > 0)
{
$this->load->model('Name_of_model');
$data['search_by_id'] = $this->Name_of_model->search_by_id($hasil_id);
$this->load->view('hasil_view', $data);//assuming page for item
}
else
{
redirect('not_good_id_method', 'refresh');//in case of not valid id
}
}
In your controller ,receive the value passed through link by uri class.And then pass it into your model.
Controller :
public function cari()
{
$id= $this->uri->segment(3);
$this->model_name->search_by_id($id);
}
Take a look on this.

Handling non objects

I might have a controller function like so
public function index()
{
$poll = DB::table('poll')->whereNull('deleted_at')->orderBy('id', 'desc')->first();
$question = DB::table('poll_question')->whereNull('deleted_at')->where('poll_id', $poll->id)->first();
$answers = DB::table('poll_answer')->whereNull('deleted_at')->where('question_id', $question->id)->orderBy('id')->get();
return view('index', compact('poll', 'question', 'answers'));
}
This is fine if the three collections I am obtaining contain data. If they dont and I try to visit the index page, I get
ErrorException in PollResponseController.php line 20: Trying to get property of non-object
So what is the best way to handle non-object's? To bypass this, I could do
public function index()
{
$poll = DB::table('poll')->whereNull('deleted_at')->orderBy('id', 'desc')->first();
if($poll) {
$question = DB::table('poll_question')->whereNull('deleted_at')->where('poll_id', $poll->id)->first();
if($question) {
$answers = DB::table('poll_answer')->whereNull('deleted_at')->where('question_id', $question->id)->orderBy('id')->get();
return view('index', compact('poll', 'question', 'answers'));
}
}
return view('error');
}
But is that not a bit exessive? I was just wondering if there was a better approach to handling this?
Thanks
You can use simple if($question) clause or if(is_null($question)).
$question = DB::table('poll_question')->whereNull('deleted_at')->where('poll_id', $poll->id)->first();
if($question){
// The object is not empty, so I'll use it
}else{
// The object is empty
}
In a blade template it will look like #if($question) and #if(is_null($question)) respectively.
#if($question)
{{ $question->property }}
#endif
In most cases you should just bypass all variables in a template and then check each of them with #if clauses.

Resources