Database Query showing in view and not $data - php

The below code seems to be showing in the "view" and not via $data as it should be (I have not echoed the $data['companyName'] into my view yet but $data['pageTitle'] works fine).
Issue:
Model:
function companyName()
{
$companyName = $this->db->query("SELECT company_name FROM core");
if ($companyName->num_rows() > 0)
{
foreach ($companyName->result() as $row)
{
echo $row->company_name;
}
}
Controller:
public function index()
{
$data['companyName'] = $this->core_model->companyName();
$data['pageTitle'] = "Admin Login";
$this->load->view('admin/assets/header', $data);
$this->load->view('admin/login.php');
$this->load->view('admin/assets/footer');
}

By echoing something in the model, that echo happens when the model is processed, which is way before the view is even a twinkle in its controller's eye.
Instead, you should do something like this in your model:
function companyName()
{
$companyName = $this->db->query("SELECT company_name FROM core");
if ($companyName->num_rows() > 0)
{
$company_names = '';
foreach ($companyName->result() as $row)
{
$company_names .= $row->company_name;
}
}
return $company_names;
}
Then you can pass $data into the view and the string will be ready to echo out like you want.

Related

Undefined variable and Invalid argument supplied for foreach()

i'm trying to learn codeigniter and i have problem here. There was an error Undefined variable and Invalid argument supplied for foreach() on view. here is the code :
Model (Model_Users.php) :
class Model_users extends CI_Model {
function __construct() {
parent::__construct();
}
function getFirstNames(){
$query = $this->db->query('SELECT firstname FROM users');
if($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->query('SELECT * FROM users');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return NULL;
}
}
}
Controller (welcome.php) :
class Welcome extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title';
$data['page_header']='Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
$data['users'] = $this->model_users->getUsers();
$this->load->view('welcome_message',$data);
}
}
View (Welcome_message.php) :
<div id="container">
<h1><?php echo $page_header ?></h1>
<div id="body">
<?php
foreach ($firstnames as $object) {
echo $object->firstname .'<br/>';
}
echo '<br/><hr/><br/>';
foreach ($users as $object){
echo $object->firstname . '\'s email address is'. $object->email . '<br/>';
}
?>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
Please help me what is wrong with my code ?
First check value return any value or not write this code in controller
use
var_dump($firstnames);
and
var_dump($users);
I think no data return from model
check any data in table
Thanks
You can eliminate the error by using return []; instead of return null; instead of checking for that case in every foreach call, because it appears that your user table might be empty based on the code provided.
Modify your model functions like below
function getFirstNames(){
$query = $this->db->get('users')->row()->firstname;
if($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
function getUsers(){
$query = $this->db->get('users');
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return NULL;
}
}
hope this will help

codeigniter retrieve data from database normal print_r working fine but can't use foreach why

I am going to fetch specific gender from db like gender='male' code is working fine but can't iterate it in foreach why it is
model you can check
class get_all_gender extends CI_Model {
//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->result() > 0)
{
return $query;
} else {
return FALSE;
}
}
}
and controller i m getting data from database
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res']=$this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
simple view
foreach ($res as $row)
{
echo $row->name;
}
Try this
In Model
class get_all_gender extends CI_Model {
//put your code here
public function select_male()
{
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result))
{
return $result;
}
else {
return FALSE;
}
}
}
In Controller
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res'] = $this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
In View
if ($res != FALSE) {
foreach ($res as $value) {
echo $value;
}
}
else {
echo "Seleted Category is Empty";
}
You should return the $query's result() instead of $query itself. Your model should look like this:
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->where("gender","male");
$query = $this->db->get("bride_groom_register");
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
}
}
You are saving the result in the controller:
$data['res'] = $this->get_all_gender->select_male();
So you can access it like this in the view:
if($res !== FALSE){
foreach ($res as $row)
{
echo $row->name;
}
}
You should check for the FALSE condition, otherwise the view will throw PHP errors when there is no rows returned from the model.
Try code below on model
Class Name first letter upper case
Filename: Get_all_gender.php
<?php
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where('gender', 'male');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
// Or
// return $query->row_array();
} else {
return FALSE;
}
}
}
On View
foreach ($res as $row)
{
echo $row['name'];
}
Class Name first letter upper case
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
}

Undefined variable: results

Model
public function show()
{
$query = $this->db->get('text');
return $query->result();
}
2.Controller
public function inf()
{
$this->load->model('addm');
$data['results'] = $this->addm->show($query);
$this->load->view('backend/first',$data);
}
3.view
I am trying to get the data from my db but i can't solve this error:
Undefined variable: result
<?php
echo"<td>";
if (is_array($result)) {
foreach ($result() as $row) {
$id = $row->id;
$words = $row->words;
}
}
echo "</td>";
?>
Few things,
In your model function show is not expecting any parameters
Remove $query from $this->addm->show(); as not needed
In the view variable should be results not result
// controller
public function inf()
{
$this->load->model('addm');
$data['results'] = $this->addm->show();
$this->load->view('backend/first', $data);
}
// view
if (!empty($results)) {
foreach($results as $row) {
$id = $row->id;
$words = $row->words;
}
}
// model
public function show()
{
$query = $this->db->get('text');
return $query->result();
}

Displaying default values in codeigniter

In the home page, all subjects should be displayed. When the subject is clicked, their respective chapters has to be displayed. For the first time, that is when the subject is not choosen, then chapters of subject id 1 has to be displayed. In the below code, should i have to call get_chapters() from index and make get_chapters() to return chapters or any other better way of doing it?
class Home extends CI_Controller {
private $data = array();
public function index()
{
$this->load->model('subjects_model');
if($query = $this->subjects_model->get_all()) {
$data['subjects'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(!is_null($id)) {
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
} else {
$query = $this->chapters_model->get('chapters');
}
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
}
Try like
public function get_chapters($id=null) {
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}
If the $id is null then defaltly subject id will become 1 orelse it will search with the existing subject id
public function index($id=null)
{
$this->load->model("chapters_model");
if(is_null($id)) {
$id = 1;
}
$query = $this->chapters_model->get_where('chapters', array('subject_id'=>$id));
if($query) {
$data['chapters'] = $query->result_array();
}
$this->load->view('home_view', $data);
}

Getting a single value from database - Codeigniter

I'm trying to get first name and last name of registered users.
This is my getfirstname , getlastname function in the model
public function getf()
{
$email = $this->input->post('email');
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
echo $row->fname;
}
}
public function getl()
{
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
echo $row->lname;
}
}
In My Contoller:
public function members()
{
if($this->session->userdata('is_logged_in'))
{
$data['fname'] = $this->getf();
$data['lname'] = $this->getl();
$this->load->view('members', $data);
}
else
{
redirect('main/restricted');
}
}
Im echoing the fname and lname variables in my view which prints 'Array' and not actual firstname and lastname
echo $fname;
echo $lname;
Your call to model is totally wrong and you are not
following the standard procedure. Try it like this.
Seperate the logics
Controller
public function members()
{
if($this->session->userdata('is_logged_in'))
{
$email = $this->input->post('email');
$result = $this->mymodel->getnames($email);
if($result){
$data['fname'] = $result->first_name;
$data['lname'] = $result->last_name;
$this->load->view('members', $data);
}
}
else
{
redirect('main/restricted');
}
}
And create single function for model not two functions
for two values that could be retrieved with single function
Also return the object instead of echoing
public function getnames($email)
{
$this->db->where('email',$email);
$query = $this->db->get('users');
if($query->num_rows == 1)
{
return $query->row();
}
return false;
}
From what I can see you haven't actually declared the 'fname' and 'lname' variables, unless I've missed it, but if it keeps returning 'Array' then it is because you need to serialise the array with
$variable = json_encode($arrayName);
This will turn the array into a string, hope this helps.
UserModel
class UserModel extends CI_Model
{
public function getItemByEmail($email)
{
// check email
$this->load->helper('email');
if( ! $email OR ! valid_email($email))
return array();
$this->db->where('email', $email);
$this->db->limit(1);
$item = $this->db->get('users')->row();
return $item;
}
}
Controller:
public function members()
{
if( ! $this->session->userdata('is_logged_in'))
redirect('main/restricted');
$this->load->model('UserModel');
$this->load->view('members', array(
'userRow' => $this->UserModel->getItemByEmail($this->input->post('email', TRUE))
));
}
View:
<?= (isset($userRow->fname) ? $userRow->fname : ''); ?>

Categories