i did search like this but it is not taking passed value in text box,can any one help me to solve this issue..thanks in advances........my model query is correct or any error is there......
if i enter in search term it is redirecting to search results page but it is showing blank results.......and it is saying invalid argument supplied for foreach()
if i print last_query() in that it is showing Like below.....
SELECT * FROM (`clients`) WHERE `clientName` LIKE '%%'
This is My Controller:
class Client extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("client_model");
$this->load->model("interview_model");
$this->load->model("candidate_model");
$this->load->model("requirement_model");
}
function search()
{
$search_term = $this->input->post('search');
$data['query'] = $this->client_model->search_client($search_term);
echo "<pre>"; print_R($this->db->last_query()); exit;
$data['page_title'] = "Search Results";
$this->layout->view("client/search",$data);
}
}
Model:
function search_client($search_term)
{
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
// Return the results.
return $query->result_array();
}
This is form in index.php(View):
<form action="<?php base_url();?>client/search">
<input type="text" name="search" id="search" style="width:180px" placeholder="Search here..." />
<input type="submit" value="Search" name="submit" class="btn btn-primary btn-sm" /></form>
This is search.php(view):
<?php foreach($query as $client){ ?>
<tr>
<td><?php echo $client->clientName;?></td>
<td><?php echo $client->clientSName; ?></td></td>
<td>
<?php } ?>
change this line:
<form method="POST" action="<?php base_url();?>client/search">
to this:
<form action="<?php echo base_url();?>client/search">
and try the following approch:
function search_client($search_term){
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
else
return false;
}
Related
Controller
public function index()
{
//load session library
$this->load->library('session');
if($this->session->userdata('user')){
// redirect('home');
$this->load->view('heropage');
}
else{
$this->load->view('login_page');
}
}
public function login(){
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$grade=$data[0]->grade;
$points=$data[0]->points;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->session->set_userdata('grade',$grade);
$this->session->set_userdata('pts',$points);
$this->getImg();
redirect('home');
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found'); }
}
View
<?php if(isset($_SESSION['success'])) :?>
<div class="alert alert-success"><?=$_SESSION['success'];?></div>
<?php endif; if(isset($_SESSION['error'])) :?>
<div class="alert alert-warning"><?=$_SESSION['error'];?></div>
<?php endif;?>
<!-- End alerts -->
<form action="<?php echo base_url();?>index.php/User/login" method="post" accept-charset="utf-8">
<div class="form-group">
<label>Email:</label>
<input type="text" class="form-control" name="email" placeholder="Email">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control"name="password" placeholder="Password">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<button class="btn btn-sm btn-success" type="submit" align="center" name="login" class="submit">Log in</button>
</div>
</div>
</form>
model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
}
Upon trying to log in, I got the error page cant be found. I want it to go to the home page if the session is correct. here is the error message:
404 Page Not Found
The page you requested was not found.
How can I solve the error because I have also set as needed in the routes
I think your form action should be <?php echo base_url(); ?>user/login
Also in your model you're not checking for password anywhere.
You're also not returning anything if the email is not found or more than 1 results are found -
($query->num_rows() == 1)
Model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password))->result(); // you should use row() here to return only 1 row.
return $query; // you should check the uniqueness of email on registration, not here -- not allow duplicate email on registration
}
Controller
public function login(){
$email = $_POST['email']; // $this->input->post('email');
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if( !empty($data) ) // if no result found it'll be empty
{
// your code
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
See, if this helps you.
i have two tables in my database one is requirements another one is users..i have those user names in my requirements form with select box..how to store that user names id in database..
This is my view:
<div class="form-group">
<label>Choose Vendor</label>
<select class="form-control" multiple class="form-control" data-placeholder="user name" name="user_id[]" >
<option value="0"></option>
<?php foreach($user as $rows) { ?>
<option value="<?php echo $rows->user_id?>"><?php echo ucfirst($rows->first_name)?></option>
<?php } ?>
</select>
</div>
This is my controller:
public function requirement()
{
$data["msg"]="";
$this->load->model('RequirementModel');
$data['user']=$this->RequirementModel->getusers();
if($this->input->post())
{
$this->RequirementModel->add_requirement($this->input->post());
redirect(base_url('index.php/Login/dashboard'));
}
$this->load->view('Requirements/requirements',$data);
}
This is my model:
function getusers()
{
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get();
//echo $this->db->last_query();
return $query->result();
}
model add_requirement:
public function add_requirement($data)
{
$data=array('role_name'=>$post['role_name'],'vacancies'=>$post['vacancies'],'experience'=>$post['experience'],'jd'=>$post['jd'],'hiring_contact_name'=>$post['hiring_contact_name'],'hiring_contact_number'=>$post['hiring_contact_number'],'user_id'=>$user_id);
$this->db->insert('requirements', $data);
}
how to do this?
Thank you
Change your function add_requirement like below and try;
function add_requirement($data)
{
if(isset($data['user_id']) && !empty($data['user_id'])) {
$data['user_id'] = implode(',',$user_id);
}
$data=array('role_name'=>$post['role_name'],'vacancies'=>$post['vacancies'],'experience'=>$post['experience'],'jd'=>$post['jd'],'hiring_contact_name'=>$post['hiring_contact_name'],'hiring_contact_number'=>$post['hiring_contact_number']);
$this->db->insert('requirements', $data);
}
}
i want create a search box , where we input student name then show all records of that students.my controller code is
public function search_function_in_controller()
{
if ($this->session->userdata('admin_login') != 1)
redirect('login', 'refresh');
$keyword = $_POST['keyword']; // you can also use $this->input->post('keyword');
$data['search_result'] = $this->crud_model->search($keyword);
$this->load->view('search_result', $data);
}
my model is :
function search($keyword)
{
$this->db->like('name',$keyword);
$query = $this->db->get('student');
return $query->result();
}
my view is :
<body>
<form action="<?=site_url('admin/search_function_in_controller')?>" method="post">
search: <input type="text" name="keyword" />
<input type="submit" value="Submit" />
</form>
<div>
<?php
// List up all results.
foreach ($results as $val)
{
echo $val['username'];
}
?>
</div>
</div>
</body>
but when we give input on search box then it give "Unable to load the requested file: search_result.php" , can anyone help me??
Verify the path that you've called in your view.
I think you intended to call the view admin/search_result in your function search_function_in_controller;
My code is to fetch questions of users saved in the database by a foreach loop and let the admin answer each question and save the answer of each question after checking of validation rules in the database , Here we go :
Model is :
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result();
}
My view is:
foreach ($questions as $id => $row) :
?>
<?php
echo "<h5>".$row->question;
echo "<br>";
echo "from : ".$row->user_name."</h5>";
echo date('Y-m-d H:i');
echo "<br>";
$q_no='save'.$row->id;
$ans_no='answer'.$row->id;
echo "<h4> Answer:</h4>";
echo form_open('control_panel');
?>
<textarea name='<?php echo 'answer'.$row->id; ?>' value="set_value('<?php echo 'answer'.$row->id; ?>')" class='form-control' rows='3'> </textarea>
<input type='hidden' name='<?php echo $q_no ; ?>' value='<?php echo $q_no; ?>' />
<input type='hidden' name='<?php echo $ans_no ; ?>' value='<?php echo $ans_no ; ?>' />
<?php
echo form_error($ans_no);
echo "
<div class='form-group'>
<div >
<label class='checkbox-inline'>
<input type='checkbox' name='add_faq' value='yes' />
Adding to FAQ page .
</label>
</div>
</div>
<p>";
?>
<input type='submit' name='<?php echo 'save'.$row->id; ?>' value='<?php echo 'save'.$row->id; ?>' class='btn btn-success btn-md'/>
<?php
echo 'answer'.$row->id;
?>
<hr>
<?php endforeach; ?>
and my controller is :
$this->load->model('control_panel');
$data['questions']=$this->control_panel->get_questions();
$data['no_of_questions']=count($data['questions']);
if($this->input->post($q_no))
{
$this->form_validation->set_rules($ans_no,'Answer','required|xss_clean');
if($this->form_validation->run())
{
/* code to insert answer in database */
}
}
of course it did not work with me :
i get errors :
Severity: Notice
Message: Undefined variable: q_no
i do not know how to fix it
I am using codeigniter as i said in the headline.
In your controller on your post() you have a variable called q_no you need to set variable that's why not picking it up.
I do not think name="" in input can have php code I think it has to be text only.
Also would be best to add for each in controller and the call it into view.
Please make sure on controller you do some thing like
$q_no = $this->input->post('q_no');
$ans_no = $this->input->post('ans_no');
Below is how I most likely would do lay out
For each Example On Controller
$this->load->model('control_panel');
$data['no_of_questions'] = $this->db->count_all('my_table');
$data['questions'] = array();
$results = $this->control_panel->get_questions();
foreach ($results as $result) {
$data['questions'][] = array(
'question_id' => $result['question_id'],
'q_no' => $result['q_no'],
'ans_no' => $result['ans_no']
);
}
//Then validation
$this->load->library('form_validation');
$this->form_validation->set_rules('q_no', '', 'required');
$this->form_validation->set_rules('ans_no', '', 'required');
if ($this->input->post('q_no')) { // Would Not Do It This Way
if ($this->form_validation->run() == TRUE) {
// Run Database Insert / Update
// Redirect or load same view
} else {
// Run False
$this->load->view('your view', $data);
}
}
Example On View
<?php foreach ($questions as $question) {?>
<input type="text" name="id" value="<?php echo $question['question_id'];?>"/>
<input type="text" name="q_no" value"<?php echo $question['q_no'];?>"/>
<input type="text"name="a_no" value="<?php echo $question['a_no'];?>"/>
<?php }?>
Model
public function get_questions(){
$this->db->select('id,user_name, question, date');
$this->db->order_by("id", "desc");
$query=$this->db->get('t_questions');
return $query->result_array();
}
I am new to CI, I've been working on this for hours with no success, please help! I am trying to generate user profiles by retrieving information from a database and displaying it in the view (using the USERID). I am getting both an Undefined Variable Error and Trying to Get Property Out of Non-Object error. Here is my code:
Model:
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
return $query->result();
}
Controller:
public function profile()
{
$data['query'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $user);
$this->load->view('footer');
}
View:
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $data->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $data->occupation;?>
</div>
Thanks so much in advance, I feel like I have tried everything!
Hi as new to CI you will face some problems but later you will enjoy it here are some simple stuff you are doing wrong
Model
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
//this will return multiple rows or object of arrays
//return $query->result();
// you need to send only single row
return $query->row();
}
Controller
public function profile()
{
// in data array key name should be same which you will pass to view
$data['row'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
View
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $row->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $row->occupation;?>
</div>
for more information please read CI user guide you will understand so many stuff from there CI user guide
here change $user to $data
public function profile()
{
$data['result'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
then if your query returns only one row , you can use
return $query->row_array();
instead of
return $query->result();
in view you can now use
Name: <?php echo $result['name']; ?>
Location: <?php echo $result['location'];?>
Occupation: <?php echo $result['occupation'];?>