I have four tables in my database named as 'user_login', 'store', 'admin', 'store_product'. when I try fetch data from user_login table, it does not work.
My codes are as below:
View form:
<form method="get">
Enter city: <input type ="text" name="city">
Enter search keyword: <input type="text" name="searchB">
<button type="submit"> Submit </button>
</form>
Controller:
public function search()
{
$this->load->model('searchmodel');
$fc = $this->input->get('city');
$sb = $this->input->get('searchB');
$rr=$this->searchmodel->searchDetail($sb,$fc);
$data['results'] = $rr;
$this->load->view('search/detailResults',$data);
}
Model
public function searchDetail($data,$datacity){
$this->db->select();
$this->db->from('user_login');
$this->db->join('store', 'store.vendor_bri = user_login.bri', 'full outer');
$this->db->join('admin', 'admin.bri = user_login.bri', 'full outer');
$this->db->join('store_product', 'store_product.vendor_bri = user_login.bri', 'full outer');
$this->db->having('store.town', $datacity);
$this->db->or_having('store.country', $datacity);
$this->db->like('user_login.business_name',$data, 'both');
$this->db->or_like('user_login.business_type', $data);
$this->db->or_like('user_login.speciality', $data);
$this->db->or_like('store.locality', $data);
$this->db->or_like('store.vendor_bri', $data,'none');
$this->db->or_like('store_product.pd_universal_code', $data,'none');
$this->db->or_like('store_product.pd_name', $data);
$this->db->or_like('store_product.pd_description', $data);
$query = $this->db->get();
return $query->result_array();
}
Related
I'm new to CI and just trying to build a basic blog for practice. Everything works fine (pull all posts, individual posts, add new post, delete post) except for the update posts. The update form loads fin, but When I submit the form I get a blank page with no errors and nothing gets updated on the db. I've searched stack for solutions, but none of these seem to make a difference. Any help would be greatly appreciated.
EDIT: Now spits the following error:
Fatal error: Call to undefined method Post::where() in /Applications/MAMP/htdocs/CodeIgniter_2.2.0/application/models/post.php on line 26
Which is the first line inside the update_post model function:
function update_post($postID, $data){
$this->where('postID', $postID);
$this->db->update('posts', $data);
}
HTML form markup from the view edit_post.php:
Add new post
<?php if($success==1){ ?>
<p>Post successfully updated</p>
<? } ?>
<form action="<?=base_url()?>posts/editpost/<? echo $post['postID']?>" method="post">
<p>Title
<input name="title" type="text" value="<?=$post['title']?>" />
</p>
<p>Body
<textarea name="post"><?=$post['post']?></textarea>
</p>
<p><input type="submit" value="Edit Post" /></p>
</form>
Models:
class Post extends CI_Model{
function get_posts($num=20, $start=0){
//$sql="SELECT * FROM users WHERE active=1 ORDER BY date_added DESC LIMIT 0,20;";
$this->db->select()->from('posts')->where('active',1)->order_by('date_added','desc')->limit($num,$start);
$query = $this->db->get();
return $query->result_array();
}
function get_post($postID){
$this->db->select()->from('posts')->where(array('active'=>1,'postID'=>$postID))->order_by('date_added','desc');
$query=$this->db->get();
return $query->first_row('array');
}
function insert_post($data){
$this->db->insert('posts', $data);
return $this->db->insert_id();
}
function update_post($postID, $data){
$this->where('postID', $postID);
$this->db->update('posts', $data);
}
function delete_post($postID){
$this->db->where('postID', $postID);
$this->db->delete('posts');
}
}
Controller:
class Posts extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('post');
}
function index(){
$this->load->model('post');
$data ['posts']=$this->post->get_posts();
$this->load->view('post_index',$data);
}
function post($postID){
$data['post'] = $this->post->get_post($postID);
$this->load->view('post',$data);
}
function new_post(){
if($_POST){
$data = array(
'title'=>$_POST['title'],
'post'=>$_POST['post'],
'active'=>1
);
$this->post->insert_post($data);
redirect(base_url().'posts/');
}else{
$this->load->view('new_post');
}
}
function editpost($postID){
$data['success']=0;
if($_POST){
$data_post=array(
'title'=>$_POST['title'],
'post'=>$_POST['post'],
'active'=>1
);
$this->post->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->post->get_post($postID);
$this->load->view('edit_post', $data);
}
function deletepost($postID){
$this->post->delete_post($postID);
redirect(base_url().'posts/');
}
}
You missed the db call before where. Here:
function update_post($postID, $data){
$this->where('postID', $postID);
$this->db->update('posts', $data);
}
Replace:
function update_post($postID, $data){
$this->db->where('postID', $postID);
$this->db->update('posts', $data);
}
you can try to debug it by using the below line in the model function
echo $this->db->last_query();
It will print the last query executed, and you can try to run it manually to check whether the query is fine or not, or try to debug it by putting logs.
OR add
error_reporting(E_ALL);
In the controller to display all the errors.
I am trying to create a search form in Codeigniter. I want to give the user 4 different options to search from. For the result I want a table that displays all 11 columns in the table I am searching from. This is the code I have so far.
Controller:
public function index(){
$this->searchTest();
}
public function searchTest(){
$this->load->model('reg_model');
$search_term = array(
'firstName' => $this->input->post('firstName'),
'lastName' => $this->input->post('lastName'),
'street' => $this->input->post('street'),
'dob' => $this->input->post('dob'));
$data['query'] = $this->reg_model->test($search_term);
$this->load->view("reg_header");
$this->load->view("reg_nav");
$this->load->view("reg_search_form", $data);
$this->load->view("reg_search", $data);
Model:
public function test($search_term='default'){
$this->db->select('*');
$this->db->from('voterinfo');
$this->db->like('firstName', $search_term);
$this->db->or_like('lastName', $search_term);
$this->db->or_like('street', $search_term);
$this->db->or_like('dob', $search_term);
$query = $this->db->get();
return $query->result_array();
}
View:
<?php $this->load->library('table'); foreach ($query as $row){ echo $this->table->generate($row); }?>
If you want to put in table using codeigniter table class , it should be something like this:
$this->load->library('table');
$this->table->set_heading(array('Name', 'Color', 'Size')); //your column name
foreach($query as $row ){
$this->table->add_row($row);
}
echo $this->table->generate();
you need also to modify your model. You're query is wrong you forgot the key , it should be like this:
public function test($search_term='default'){
$this->db->select('*');
$this->db->from('voterinfo');
$this->db->like('firstName', $search_term['firstName']);
$this->db->or_like('lastName', $search_term['lastName']);
$this->db->or_like('street', $search_term['street']);
$this->db->or_like('dob', $search_term['dob']);
$query = $this->db->get();
return $query->result_array();
}
I am trying to update a table where id of the row is entered and title is selected through a dropdown list, but I have two problems. Firstly Im not sure how to pass the data to the model, and the second problem is actually updating the table using active record.
controller class
function edit_profile($id)
{
$data = array(
'table_name' => 'user',
'id' => $id ,
'fullname' => $this->input->post('fullname')
);
if($this->user_model->upddata($data))
{
$data['msg']= 'Update Done';
$this->load->view('header_view',$data);
$this->load->view("edit_profile_view.php", $data);
$this->load->view('sidbar_user_view',$data);
$this->load->view('footer_view',$data);
}
else
{
$data['msg']= 'Update Not Done';
$this->load->view('header_view',$data);
$this->load->view("edit_profile_view.php", $data);
$this->load->view('sidbar_user_view',$data);
$this->load->view('footer_view',$data);
}
}
model class
function upddata($data) {
extract($data);
$this->db->where('id', $id);
$this->db->update($table_name, array('fullname' => $fullname));
return true;
}
view
<?php echo form_open("profile/edit_profile/$d->id"); ?>
<div class="tuple">
<h1>full name : </h1>
<input type="text" name="fullname" class="t-name" readonly value="<?=fullname; ?>" >
<div class="clear"></div>
</div>
<input type="submit" name="mysubmit" value="Save" id="mysubmit">
<?php echo form_close(); ?>
and when open url
/site/profile/edit_profile/1
i get this message
Fatal error: Call to a member function upddata() on a non-object
Try to load your model in controller like
function edit_profile($id) {
$this->load->model('user_model');
//Do other things
}
And it is optional like
function uppdata($data) {
$this->db->where('id', $data['id']);
$this->db->update($data['table_name'], array('fullname' => $data['fullname']));
return true;
}
From $data also you can directly get the relavent values.ANd you need to pass $data to the view file not the $data1 because it is un-defined and
Replace
$this->load->view("edit_profile_view.php", $data);
to
$this->load->view("edit_profile_view", $data);
please help me how to display data to the view page after insert query into database.
view page(member_view.php):
<form action="<?php member_controller/insert_info; ?>" method="post">
<input type="text" name="fname">
<input type="text" name="lname">
<input type="submit" name="submit">
</form>
<?php
if($res){
foreach($res as $data){
echo $data['fname'];
echo $data['lname'];
}
}
?>
controller page(member_controller.php):
<?php
class Member_Controller extends CI_Controller {
public function insert_info(){
$data = array('fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
);
$this->load->model('member_model');
$this->member_model->member_posting($data);
$data['username'] = $session_data['username'];
$retrieved_info['res'] = $this->member_model->member_posting($data);
$this->load->view('member_view',$data, $retrieved_info)
}
}
model page(member_model):
<?php
class Member_Model extends CI_Model{
public function member_posting($data=array()){
extract($data);
$query = "INSERT INTO member_table (fname, lname)values ('$fname', '$lname')";
$result = $this->db->conn_id->prepare($query);
$result->execute();
if($result){
$query = "SELECT * FROM member_table WHERE fname = '$fname' AND lname='$lname'";
$result = $this->db->conn_id->prepare($query);
$result->execute();
return $res = $result->fetchAll(PDO::FETCH_ASSOC);
}
}
}
and here would be the pattern:
view(submit form)---->controller(get vars and send to model) --->
model(insert to db and perform select query)--->controller(get the select query results from model)--->view(display the results)
please help me, im just new to codeigniter, hope i can learn from you guys.
This is the way you need to follow.........
View file:member_view.php
<form action="<?php echo ROOT_FOLDER ?>/member_controller/insert_info" method="post">
<input type="text" name="fname">
<input type="text" name="lname">
<input type="submit" name="submit">
</form>
Controller file: member_controller.php
<?php
class Member_Controller extends CI_Controller {
public function insert_info()
{
$data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
);
$this->load->model('member_model');
$this->member_model1->member_posting($data);
$retrieved_info = array();
$retrieved_info['res'] = $this->member_model2->member_posting($data);
$this->load->view('member_view',$data, $retrieved_info)
}
}
Model file: member_model1.php
class Member_Model extends CI_Model{
public function member_posting_inserting($data){
$this->insert_helper('member_table',$data);
}
public function member_posting_retrieving($data){
$query = "SELECT * FROM member_table WHERE fname = '$data['fname']' AND lname='$data['lname']'";
$res=$this->db->query($query);
if($res->num_rows()>0){
return $res->result("array");
}
return array();
}
}
Insert helper function...............
public function insert_helper($table_name, $data_array){
$this->db->insert($table_name,$data_array);
return $this->db->insert_id();
}
This
$this->load->view('member_view',$data, $retrieved_info)
in your case won't do the trick. Read about the views here: http://ellislab.com/codeigniter/user-guide/general/views.html
2nd parameter is data array. 3rd parameter can be TRUE or FALSE, deciding if the view has to be rendered or returned as string.
This should do the trick:
$this->load->view('member_view', $retrieved_info);
Edit:
Just noticed your member_posting() doesn't return anything. It should return $res;
I'm using codeigniter and trying to do an UPDATE query, but it keeps creating a new row like an INSERT query. I'm baffled and searching the web didn't yield much for me. (I'm a bit of a n00b). Any help would be greatly appreciated.
Here is the form:
<form action="<?=base_url();?>blog/new_post" method="post" name="write_new">
<input type="hidden" name="user" value="<?=$user?>">
<label>Title:</label><br>
<input type="text" name="title" value="<?php if(isset($query->title)) { echo $query->title; }?>" size="50"><br>
<label>Date:</label><br>
<input type="text" name="date" size="46" value="<?php if(isset($query->date)) { echo $query->date; }?>" />
<script language="JavaScript">
new tcal ({
// javascript calender thingy
// form name
'formname': 'write_new',
// input name
'controlname': 'date'
});
</script> <br><br>
<label>Article:</label><br>
<textarea name="content" style="width:100%;height:300px;"><?php if(isset($query->article)) { echo $query->article; }?></textarea><br>
<select name="category">
<option value="news">Dreamsy News</option>
<option value="web">From the Interwebs</option>
</select>
<input type="submit"/>
</form>
<div class="clearboth"></div>
Here's the model:
function edit_post($title, $date, $author, $article, $category, $id)
{
$this->load->helper('form'); //loads the CI form helper
$this->load->library('form_validation'); //loads the form validation class
$this->form_validation->set_rules('title', 'title', 'required|min_length[3]|max_length[40]|xss_clean');
$this->form_validation->set_rules('date', 'date', 'required|min_length[5]|max_length[15]|xss_clean');
$this->form_validation->set_rules('category', 'category', 'required|xss_clean');
$this->form_validation->set_rules('content', 'article', 'required|xss_clean');
$this->form_validation->set_rules('user', 'you are not logged in', 'required');
if ($this->form_validation->run() == FALSE) //If the form does not validate
{
$this->load->vars(array('title' => 'Error'));
$this->template->write_view('content', 'error/new_post_fail');
$this->template->render();
}
else
{
$article = htmlentities($article, ENT_QUOTES);
/*$data = array(
'title' => $title,
'date' => $date,
'author' => $author,
'article' => $article,
'category' => $category
);*/
//$this->db->where('id', $id);
//$this->db->update('blog', $data);
$query = 'UPDATE blog SET title = '.$title.' date = '.$date.', author = '.$author.', article = '.$article.', category = '.$category;
$this->db->query($query);
redirect(base_url().'blog');
}
And here's the controller:
public function write()
{
$id = $this->uri->segment(3);
$query = 'SELECT * FROM blog WHERE id = '.$id;
$query = $this->db->query($query);
$query = $query->row();
$title = $query->title;
$user = $this->session->userdata('user_id');
if(isset($id)){ $title = 'Edit: '.$title; } else { $title = 'Write new post'; }
$vars['title'] = $title;
$vars['page'] = 'blog';
$vars['user'] = $user;
$vars['id'] = $id;
$vars['query'] = $query;
$this->load->helper('form'); //loads the CI form helper
$this->load->library('form_validation'); //loads the form validation class
$this->load->vars($vars);
$this->template->write_view('content', 'blog/write');
$this->template->render();
}
<form action="<?=base_url();?>blog/new_post" method="post" name="write_new">
You have to modify the action attribute to call the edit method of your blog controller. Otherwise the new_post method is called, which as far as I can see just inserts the POST data.
Did you set up some route, because i didnt see related function in your above controller which referencing by action tag/attr from your form. It hard to guest where the error came from, if you doesn't include that function.
Some highlight from me, i notice you put form validation, template stuff and even redirection function in models instead put that in your controller.
I think there is no WHERE condition in your UPDATE Query this can be one of the problem of your issue..
be sure only that part of the code is being executed for your problem..