Good Day!
I have this code and I don't know what could be the problem with my code 'casue I can't echo the value of variable in view.
This is my controller:
public function generate()
{
$name = $this->input->post('name');
$this->data['generated'] = $this->user_models->generate_name($name);
$this->load->view('templates/header');
$this->load->view('generated_user_views', $this->data);
$this->load->view('templates/footer');
}
The Model:
function generate_name($name)
{
$this->db->select('*');
$this->db->from('user');
$this->db->where('name', $name);
$query = $this->db->get();
return $query->result_array();
}
In View:
<?php
$view_name = $generated['name'];
$view_department = $generated['department'];
?>
<div class="form-group">
<label for="name" class="col-sm-2 control-label emp">Name</label>
<div class="col-sm-10">
<input type="text" class="typeahead form-control" id="name" placeholder="Name" name="name" autocomplete="off"><?php echo $view_name; ?></input>
</div>
</div>
<div class="form-group">
<label for="department" class="col-sm-2 control-label emp">Department</label>
<div class="col-sm-10">
<input type="text" class="typeahead form-control" id="department" placeholder="Department" name="department" autocomplete="off"><?php echo $view_department; ?></input>
</div>
</div>
I don't know why I always have an error like:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: name
Filename: views/generated_user_views.php
Line Number: 12
A PHP Error was encountered
Severity: Notice
Message: Undefined index: department
Filename: views/generated_user_views.php
Line Number: 13
In your model you are selecting all the data from your table so there is a multidimensional array returned,In view you need to loop over your results,see docs
foreach ($generated as $row)
{
echo $row['name'];
echo $row['department'];
}
Or i guess if you need a single record from your table then you can use $query->row_array();in your model and in view you can access them as
$view_name =$generated['name'];
$view_department = $generated['department'];
Try this:
$name = $this->input->post('name');
$department = $this->user_models->generate_name($name);
$this->data = array('generated' => array('name' => $name, 'department'=> $department);
Related
I am trying to learn select query with class.
Below is index.php, which have a form to display results and I in PHP I am calling class operations, which is in operations.php
<?php
session_start();
include('operations.php');
$userprofileobj = new operations();
$userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $userprofileobj->fetch_assoc();
?>
<form class="form-horizontal" method="post" action="#" name="number_validate" id="number_validate" >
<div class="control-group">
<label class="control-label">Admin Name</label>
<div class="controls">
<input type="text" name="min" id="min" value="<?php echo $row['user_name']; ?>" required />
</div>
</div>
<div class="control-group">
<label class="control-label">User Email</label>
<div class="controls">
<input type="text" name="max" id="max" value="<?php echo $row['user_email']; ?>" required />
</div>
<div class="form-actions">
<input type="submit" value="Update" class="btn btn-success">
</div>
</form>
operations.php is:
class operations{
public $user_email;
public $error;
public $con;
public function __construct()
{
$this->con = new mysqli("localhost","root","","admin_with_oops");
}
public function showuserprofile($user_email)
{
$result = $this->con->query("select * from user_table where user_email='$user_email'");
if($result->num_rows > 0)
{
return $result;
}
else
{
$this->error = "User not exist";
}
}
}
Am I on right way? If yes teh what is the problem on above code?
Error is: Fatal error: Call to undefined method operations::fetch_assoc() in F:\xampp\htdocs\admin_with_oops\HTML\index.php on line 16
$userprofileobj is an instance of the operations class. It doesn't have fetch_assoc() method. You should replace
$userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $userprofileobj->fetch_assoc();
with
$row = $userprofileobj->showuserprofile($_SESSION['user_email'])->fetch_assoc();
Or you could assign the result to a variable and then call the fetch_assoc() function on it:
$result = $userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $result->fetch_assoc();
im trying to have an edit/update module in my system. But, it seems i cant figure it out why im getting array to string conversion error. Below are the model,view,controller codes.
Line error in model
function updatebyid($userid, $data)
{
$this->db->where('id', $userid); **i get the error in this line in my model**
Line error in controller
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid, $data)) **AND IN THIS LINE TOO IN MY CONTROLLER**
My model
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('users');
return $query->result();
}
function updatebyid($userid, $data)
{
$this->db->where('id', $userid);
$query = $this->db->update('users', $data);
return $query->result();
//return $query->row()->query;
}
My view
</div>
<div class="row well" style="color: black; margin-top: 15px;">
<h2>Edit Profile</h2>
<?php $attributes = array("name" => "registerform");
echo form_open("account/edituserinfo/", $attributes);?>
<div class="form-group">
<div class="col-sm-4">
<label for="fname">First Name</label>
<input class="form-control" name="fname" required type="text" value="<?php echo $fname; ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
<div class="col-sm-4">
<label for="mname">Middle Name</label>
<input class="form-control" name="mname" required placeholder="Middle Name" type="text" value="<?php echo $mname; ?>" />
<span class="text-danger"><?php echo form_error('mname'); ?></span>
</div>
<div class="col-sm-4">
<label for="lname">Last Name</label>
<input class="form-control" name="lname" required placeholder="Last Name" type="text" value="<?php echo $lname; ?>" />
<span class="text-danger"><?php echo form_error('lname'); ?></span>
</div>
</div>
<div class="input-group" style="padding-top: 15px; margin-left: 15px">
<button name="submit" type="submit" class="btn btn-success">Update</button>
<!--input class="form-control" name="id" placeholder="id" type="hidden" value="<?php echo $id; ?>" /-->
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</div>
</div>
My controller
function index()
{
$this->load->view('include/headnav');
$details = $this->model_user->get_user_by_id($this->session->userdata('id'));
$data['name'] = $details[0]->fname . " " . $details[0]->lname;
$data['level'] = $this->session->userdata('level');
$data['fname'] = $details[0]->fname;
$data['mname'] = $details[0]->mname;
$data['lname'] = $details[0]->lname;
$data['gender'] = $details[0]->gender;
$data['email'] = $details[0]->email;
$data['mobileNum'] = $details[0]->mobileNum;
$data['landlineNum'] = $details[0]->landlineNum;
$data['homeaddress'] = $details[0]->homeaddress;
//$data['position'] = $details[0]->position;
//$data['institution'] = $details[0]->institution;
//$data['institutionAddr'] = $details[0]->institutionAddr;
//$data['institutionNum'] = $details[0]->institutionNum;
$this->load->view('view_account', $data);
$this->load->view('include/footernav');
}
function edituserinfo()
{
$this->load->view('include/headnav');
$data = array(
'lname' => $this->input->post('lname'),
'fname' => $this->input->post('fname'),
'mname' => $this->input->post('mname'),
'gender' => $this->input->post('gender'),
'homeaddress' => $this->input->post('homeaddress'),
'mobileNum' => $this->input->post('mobileNum'),
//'password' => $this->input->post('password'),
//'cPassword' => $this->input->post('cPassword'),
//'institution' => $this->input->post('institution'),
//'institutionAddr' => $this->input->post('institutionAddr'),
//'institutionNum' => $this->input->post('institutionNum'),
//'position' => $this->input->post('position'),
);
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid, $data))
{
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Profile edited.</div>');
redirect('account/edituserinfo');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Error. Check credentials.</div>');
redirect('account/edituserinfo');
}
}
function updateuinfoview()
{
$this->load->view('include/headnav');
$details = $this->model_user->get_user_by_id($this->session->userdata('id'));
//$data['fname'] = $details[0]->fname . " " . $details[0]->lname;
$data['fname'] = $details[0]->fname;
$data['lname'] = $details[0]->lname;
$data['mname'] = $details[0]->mname;
$data['gender'] = $details[0]->gender;
$data['email'] = $details[0]->email;
$data['mobileNum'] = $details[0]->mobileNum;
$data['landlineNum'] = $details[0]->landlineNum;
$data['homeaddress'] = $details[0]->homeaddress;
//$data['position'] = $details[0]->position;
//$data['institution'] = $details[0]->institution;
//$data['institutionAddr'] = $details[0]->institutionAddr;
//$data['institutionNum'] = $details[0]->institutionNum;
$this->load->view('view_editprofile', $data);
$this->load->view('include/footernav');
}
This is the error message
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_query_builder.php
Line Number: 669
Backtrace:
File: C:\xampp\htdocs\THTF.6\application\models\model_user.php
Line: 73
Function: where
File: C:\xampp\htdocs\THTF.6\application\controllers\Account.php
Line: 74
Function: updatebyid
File: C:\xampp\htdocs\THTF.6\index.php
Line: 315
Function: require_once
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
UPDATE users SET lname = 'Taz', fname = 'Vinny', mname = 'Paz', gender = 'Male', homeaddress = 'USA', mobileNum = '4123' WHERE id = Array
Filename: C:/xampp/htdocs/THTF.6/system/database/DB_driver.php
Line Number: 691
You are getting array to string conversion error because your model :
$this->model_user->get_user_by_id($this->session->userdata('id'))
returns an array of objects. To remedy your problem, you could loop the result
or if you only need the first row of your result you could do this:
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid[0]->id, $data))
or perhaps you can modify your model like this:
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('users');
return $query->row(); // use row if you only want the first row of the result of your query
}
controller:
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
if ($this->model_user->updatebyid($userid->id, $data))
***************************************************************************************
or a more comprehensive model so you wont need to change your controller:
function get_user_by_id($id)
{
$this->db->where('id', $id);
$query = $this->db->get('users');
$result = $query->row();
return ($result) ? $result->id : false;
}
You are passing array of user details instead of user id in update
change this line
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
to
$userid = $this->session->userdata('id');
in function edituserinfo() of controller
$userid = $this->model_user->get_user_by_id($this->session->userdata('id'));
In the above line you are querying the database which will return an array.
Instead set the user ID as below
$userid = $this->session->userdata('id');
Your Answer
Once the user login into site unable to fetch the data from database getting blank page if i write foreach condition here is my code.Fetching username and login verification is workig fine.
Controller:
public function index()
{
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
}
else{
$this->load->view('welcome');
}
}
Model:
function getprofiledata($id)
{
$this->db->select('profile_details.*');
$this->db->from('profile_details');
$this->db->where(array('profile_details.profile_id'=>$id));
$q=$this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
View:
<div id="legend">
<legend class="">Profile Information</legend>
</div>
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<form action="<?php echo base_url();?>profile/updateprofile" role="form" class="form-horizontal" id="location" method="post" accept-charset="utf-8">
<?php
echo form_hidden('profile_id',$r->profile_id);
?>
<div class="form-group">
<label class="control-label col-sm-2 " for="name">Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="name" placeholder="Enter name" value="<?php echo $r->first_name;?>" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="profilename">Profile Name:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="profile_name" placeholder="Enter Profile name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2 " for="designation">Designation:</label>
<div class="col-sm-4 col-sm-offset-1">
<input type="text" class="form-control" id="designation" placeholder="Enter Designation">
</div>
</div>
<button type="submit" class="btn">Submit</button>
</form>
<?php endforeach;endif;?>
You chose the wrong segment number on line $data['records']= $this->profile_model->getprofiledata($this->uri->segment(3));.
Take notice that segment counting starts with zero, so segment no 3 is actually the 4th one in the uri.
If you keep the user id inside your session, you should replace $data['records']= $this->profile_model->getprofiledata($this->uri->segment(3)); with $records = $this->profile_model->getprofiledata($this->session->userdata('profile_id'));. And you're done.
add a new session when you login process like bellow in your login model :
<?php
public function login_user($user_name = '', $password=''){
$userdetails = array(
'email' => $user_name,
'password' => md5($password),
'status'=>1,
);
$this->db->where($userdetails);
$query = $this->db->get('profile_details');
if($query->num_rows()):
$user = $query->result();
$sess_arry = array(
'profile_id' => $user[0]->profile_id, // add new session profile_id
'first_name' => $user[0]->first_name
);
$this->session->set_userdata('admin_logged_in', $sess_arry); //add admin details to session
return true;
else:
return false;
endif;
}
?>
And some change your index method like bellow :
<?php
public function index()
{
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['country'] = $this->signup_model->getcountry();
$data['states'] = $this->profile_model->getstates();
$profile_id = $this->session->userdata('profile_id');
$records = $this->profile_model->getprofiledata($profile_id);
$data['records']= $records;
$data['mainpage']='profile';
$this->load->view('templates/template',$data);
$this->load->view('templates/sidebar',$data);
}
else{
$this->load->view('welcome');
}
}
?>
I have added new 3 line because i thinks you are no getting profile id properly in $this->uri->segment(3)
So,
$profile_id = $this->session->userdata('profile_id');
$records = $this->profile_model->getprofiledata($profile_id);
$data['records']= $records;
I'm working on a custom CMS using PHP OOP and this is actually my first project ever which is made with object oriented programming so I don't have that much of experience with it. Basically I have a class called Site.class.php which retrieves data from one of the tables in MySQL database and goes like this:
<?php
class Site
{
public $id,$site_name,$site_title,$site_url,$site_tags,$site_desc;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function getSite($name)
{
if(!empty($name))
{
$site = $this->db->prepare("select * from admins where site_name = ?");
$site->bindParam(1,$name);
$site->execute();
while($row = $site->fetch())
{
$this->id = $row['id'];
$this->site_name = $row['site_name'];
$this->site_title = $row['site_title'];
$this->site_url = $row['site_url'];
$this->site_tags = $row['site_tags'];
$this->site_desc = $row['site_desc'];
}
}
else
{
header("Location: maint/php/includes/errors/005.php");
exit();
}
}
public function getID()
{
return $this->id;
}
public function getSiteName()
{
return $this->site_name;
}
public function getSiteTitle()
{
return $this->site_title;
}
public function getSiteUrl()
{
return $this->site_url;
}
public function getSiteTags()
{
return $this->site_tags;
}
public function getSiteDesc()
{
return $this->site_desc;
}
}
?>
I have included this file at another file which is called settings.php and called it in this way:
$siteSet = new Site();
$siteSet->getSite("Daygostar");
Then I tried echoing out the variables like this:
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="usr">Site Name:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName; ?>">
</div>
<div class="form-group">
<label for="usr">User URL:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl; ?>">
</div>
</div>
</div>
</div>
But the problem is that whenever I call this file ,I receive this error message:
Undefined property: Site::$getSiteName
Undefined property: Site::$getSiteUrl
I don't know what's really going wrong because I have coded everything correctly! So if you know how to solve this question please let me know, I really appreciate that.. Thanks in advance.
Those are both methods. You need to add the () to the end of them to invoke the method.
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="usr">Site Name:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName(); ?>">
</div>
<div class="form-group">
<label for="usr">User URL:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl(); ?>">
</div>
</div>
</div>
</div>
I have the following error message while updating my records in database
Severity: Notice
Message: Array to string conversion
Filename: database/DB_query_builder.php
Line Number: 662
Backtrace:
File: C:\xampp\htdocs\Site\application\models\class_model.php Line: 48
Function: where
File: C:\xampp\htdocs\Site\application\controllers\class_con.php Line:
107 Function: update
File: C:\xampp\htdocs\Site\index.php Line: 292 Function: require_once
here my controller
function edit($id)
{
$rules = [
[
'field' => 'classname',
'label' => 'Class Name',
'rules' => 'trim|required'
],
[
'field' => 'inchargename',
'label' => 'Incharge Name',
'rules' => 'trim|required'
],
[
'field' => 'classstrength',
'label' => 'Class Strength',
'rules' => 'trim|required'
]
];
$this->form_validation->set_rules($rules);
$class = $this->class_model->find($id)->row();
if($this->form_validation->run() == FALSE)
{
$this->load->view('admin/class/classEdit',array('class'=>$class));
}
else
{
$data['classname'] = set_value('classname');
$data['inchargename'] = set_value('inchargename');
$data['classstregth'] = set_value('classstregth');
$this->class_model->update($id,$data);
$this->session->set_flashdata('message','Class has been Updated Successfully');
redirect('class_con/index');
}
}
and here's my model
public function find($id) {
$this->db->where('id',$id);
$row = $this->db->get('class');
return $row;
}
function update($data, $id)
{
try{
$this->db->where('id',$id);
$this->db->update('class', $data);
return true;
}
catch(Execption $e){
echo $e->getMessage();
}
}
and here is my view
<?php echo form_open_multipart('class_con/edit/'.$class->id); ?>
<div class="form-group" id="register-login-group">
<label for="classname">Class Name</label>
<div class="input-group">
<input type="text" class="form-control" id="classname" name="classname" value="<?php echo $class->classname; ?>" placeholder="Class Name">
<div class="input-group-addon"><i class="fa fa-pencil"></i></div>
</div>
</div>
<div class="form-group" id="register-login-group">
<label for="classname">Incharge Name</label>
<div class="input-group">
<input type="text" class="form-control" id="inchargename" name="inchargename" value="<?php echo $class->inchargename; ?>" placeholder="Incharge Name">
<div class="input-group-addon"><i class="fa fa-pencil"></i></div>
</div>
</div>
<div class="form-group" id="register-login-group">
<label for="classname">Class Strength</label>
<div class="input-group">
<input type="text" class="form-control" id="classstrength" name="classstrength" value="<?php echo $class->classstrength; ?>" placeholder="Class Stregth">
<div class="input-group-addon"><i class="fa fa-pencil"></i></div>
</div>
</div>
<button type="submit" class="btn btn-primary">Save</button>
<?=anchor('class_con/index','Cancel',['class'=>'btn btn-warning'])?>
<?php echo form_close(); ?>
In your controller You call the update method in model by this line :
$this->class_model->update($id,$data);
but in your Model you have defined function as :
function update($data, $id)
{
try{
$this->db->where('id',$id);
$this->db->update('class', $data);
return true;
}
catch(Execption $e){
echo $e->getMessage();
}
}
the problem is with the order of parameter's that you have passed :
change it to :
$this->class_model->update($data,$id);
Just remove ->row() from
$class = $this->class_model->find($id)->row();
Instead use
$class = $this->class_model->find($id);
foreach ($class->result() as $row)
{
echo $row->colunn_name;
}
And set_value()
Permits you to set the value of an input form or textarea. You must
supply the field name via the first parameter of the function.
So instead
$data['classname'] = set_value('classname');
$data['inchargename'] = set_value('inchargename');
$data['classstregth'] = set_value('classstregth');
Use to store value in $data array
$data['classname'] = 'classname';
$data['inchargename'] = 'inchargename';
$data['classstregth'] = 'classstregth';
It seems there is a bug in your query.
Please trace error using following steps.
1) use $this->db->last_query(); which prints your query....
2) Run that query in sql, it gives the appropriate row or not.
3) if query works properly, now print_r the o/p variable and trace weather it gives the result or not.