How to Show counted data to Html using php codeigniter - php

so here is my model codes:
public function GetId()
{
$this->db->select('count(*) as total');
$this->db->from($this->table);
$this->db->where('dibaca', null);
$query = $this->db->get();
return $query->result_array();
}
and this is my code in html:
<?= $DataId['total']; ?>
i already call the function to my controller as DataId,
and i get error that Undefined array key "total"
can you guys tell me what is wrong in it?

Some untested advice:
Your model can be refined to:
public function countNullDibaca(): int
{
return $this->db
->where("dibaca", null)
->count_all_results($this->table);
}
Your controller should call for the model data and pass it to the view.
public function myController(): void
{
$this->load->model('my_model', 'MyModel');
$this->load->view(
'my_view',
['total' => $this->MyModel->countNullDibaca()]
);
}
Finally, your view can access the variable that associates with the first-level key in the passed in array.
<?= $total; ?>
Here is a related post which speaks on passing data from the controller to the view.

Replace the result_array() at the model with
num_rows()
and you can delete ['total'] from your code in html, or like this:
<?= $DataId; ?>

Related

Why doesn't the controller recognise the data passed on from the model?

I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}

How to call 2 model function in one function controller in codeigniter

I am new to codeigniter, so please help me to solve my problem.
I want to call my two model function in one function controller. Heres my two model function:
public function getposts($postid){
$this->db->select('*');
$this->db->from('tbl_endangered');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
//$this->db->join('tbl_images');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
And here is my function controller:
public function viewAnimals($postid){
if(!$this->session->userdata("id") )
return redirect("AuthCon/login");
$this->load->model('Show');
$posts = $this->Show->getSinglePost($postid);
//$posts = $this->Show->getposts($postid);
$this->load->view('Showimage',['posts'=>$posts]);
}
In that code above I always get the first one that I declared. I want to fetch the getposts and getSinglePost inside the function viewAnimals controller.
Hopeyou can help me. Thanks in advance
I think using same variable name is creating a mess.
you can try assigning to posts array with output in it.
$this->load->model('Show');
$posts['getSinglePostData'] = $this->Show->getSinglePost($postid);
$posts['getPostsData'] = $this->Show->getposts($postid);
$this->load->view('Showimage',$posts);
Do like this in the controller
public function viewAnimals($postid){
if(!$this->session->userdata("id"))
return redirect("AuthCon/login");
$this->load->model('Show');
$data['images'] = $this->Show->getSinglePost($postid);
$data['endangered'] = $this->Show->getposts($postid);
$this->load->view('Showimage', $data);
}
Another way is to use join() and only one model function
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
$this->db->join('tbl_endangered', 'tbl_endangered.endangeredid = tbl_images.endangeredid');
$this->db->where('tbl_images.endangeredid', $postid);
return $this->db->get()->result();
}

Codeigniter Fetch data

I'm trying to fetch de data from my database and show it in my view.
But I don't seem to get it right. I worked a bit with codeIgniter and I know you can display your data in the view in a way that goes something like this:
{data}{dataname}{/data}
And this would show you every record of the dataname.
This is my model:
public function __construct()
{
$this->load->database();
}
public function Get_Pedras()
{
$query=$this->db->query("SELECT * FROM pedras;");
$row = $query->row_array();
return $row;
}
And this is my controller
public function __construct()
{
parent::__construct();
$this->load->model('Pedras_Model');
$this->load->helper('url_helper');
}
public function index (){
$this->load->view('Main/Formulario_Email');
}
public function GetStone(){
$this->load->library('parser');
$name = $this->Pedras_Model->Get_Pedras();
$data = array(
'title' => 'IdPedra',
'heading' => 'NomePedra'
);
$this->load->view('Main/Formulario_Email', $data);
}
And at least my view:
<div class="col-sm-6 col-sm-offset-1">
{data}
{title}
{/data}
</div>
I think I might be missing something realy important, but I only worked a little bit with CI, and some time ago, could you be helping me out pls? Thanks in advance!
In your Model don't use row_array because you are querying all the available data from your table. use result_array instead.
Like this...
public function Get_Pedras(){
$query=$this->db->query("SELECT * FROM pedras;");
return $query->result_array();
}
Try this one in your Controller...
public function GetStone(){
$this->load->library('parser');
$data['pedras'] = $this->Pedras_Model->Get_Pedras();
$this->load->view('Main/Formulario_Email', $data); //pass data here!
}
then in your view, in order to display the data do this...
foreach($pedras as $p){
echo $p['data']; //rename data with you query_name.
}

Search through name instead of id in code igniter..?

I am a new to php, have this code in controller:
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectquery($id);
$this->load->view('aboutpageview', $data);
}
and this in model:
public function selectquery($id){
$query =$this->db->get_where('phone_mobileinfos',array('id'=>$id));
// return $query->row_array();
return $query;
}
this in view:
{
<p>Mobile_name:<?php echo $h['mobile_name'];?></p>
<p>Price:<?php echo $h['price'];?></p>
}
Just want to search by name instead of id any suggestions..?
In model:
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
use in controller:
$data['by_name'] = $this->aboutmodel->selectquery($id);
in view:
<p>Mobile_name:<?php echo $by_name['mobile_name'];?></p>
<p>Price:<?php echo $by_name['price'];?></p>
So, If I get you correctly and Adding to j.Litvak's code, you want to search the db for a name rather than an id? If so you would need to pass the controller method the name variable i.e. http://localhost/controller_name/method/search_parameter, in your case localhost/controller/selectquery/samsung
Your controller will be
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectByName($id);
$this->load->view('aboutpageview', $data);
}
And Model
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
And View
foreach($h as $key => $value){ The code here is obviously in php tags but I cant show them
Mobile: echo $value['mobile_name'];
Price: echo $value['price'];
}
So you create a loop through the result, iterating each result found.
CAVEAT: you will need to test for a NULL result or you will get an error and be careful passing strings through the URL, codeigniter has sql injection protection but I always like to be safe

Codeigniter : How can call model funcation in View?

i have model and defined 2 function
1.getMember()
2. getMemberCommunity($member_id)
Controller code
public function index($pass=null){
$this->load->model('member_model');
$data['query']=$this->member_model->getMember();
$this->load->view('backend/project',$data);
}
View
foreach($query as $row) {
// i want to call my another model function ?
//please give me any solutions ya tips
}
You could do something like this by joining the members table where the member id is equal to the member id in the member community table, I think this is what you want:
Model
public function getMembers(){
$this->db->from('members')
->join('member_community', 'members.id = member_community.member_id', 'left');
$members = $this->db->get();
return $members;
}
Controller
public function index($pass=null){
$this->load->model('member_model');
$data['query']=$this->member_model->getMembers();
$this->load->view('backend/project',$data);
}
View
foreach($query->result_array() as $row) {
// do whatever in here
}

Categories