dropdown value to db codeigniter - php

I am working with CodeIgniter and have some problems with the database. I populate my dropdown list with customer names from table 'customers' in DB. I need to select the name from the dropdown, to input the name of the project and to add it to DB table 'projects', but in this table, I should have not the name of the customer, but its id. how can I implement it? And when I want to send not the id, but the name - I have an error: that customerName has NULL value.
Here is my controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Add extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('url', 'form'));
$this->load->model("addmodel");
$this->load->library('session');
}
private function view($page, $data=false) {
$this->load->view("header.php", $data);
$this->load->view($page, $data);
$this->load->view("footer.php", $data);
}
public function add_project() {
$this->load->database();
$data['customers']=$this->addmodel->select_customer();
$this->view('add/add_project.php', $data);
}
public function new_project() {
$this->load->database();
$data['customers']=$this->addmodel->select_customer();
$this->load->view("add/add_project.php", $data);
$projectName = $this->input->post("projectName");
$customerID = $this->input->post("customerName");
$this->addmodel->add_project($projectName, customerID);
redirect("add_project", "refresh");
}
}
Here is my model
<?php
class AddModel extends CI_Model {
public function __construct() {
$this->load->database();
}
function add_project($projectName, $customerID) {
$this->db->insert('projects', array(
'projectName' => $projectName,
'customerID' => $customerID
));
}
function select_customer() {
$query = $this->db->get('customers');
return $query;
}
}
?>
and, finally, here is my view
<?php echo form_open('new_project'); ?>
<div id="form-main">
<div id="form-div">
<form class="form" id="form1">
<select class="feedback-input" id="customer_selecter">
<option name="customerName">Select customer</option>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
<p class="projectName">
<input type="text" name="projectName" placeholder="Project name" required class="feedback-input" id="projectName" />
</p>
<div class="submit">
<button type="submit" id="button-red">Add</button>
<div class="ease"></div>
</form>
</div>
</form>
</div>

Change option value to $row->customerName, so that you can get customer name
<select class="feedback-input" id="customer_selecter" name="customerName>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>

Change your Html code select box name like this:
<select class="feedback-input" id="customer_selecter" name="customerName">
<option >Select customer</option>
// <?php
foreach($customers->result() as $row){
echo '<option value="'.$row->customerID.'">'.$row->customerName.'</option>';
}
?>
</select>
And need to pass corrct variable for customerId with $ like this:
$this->addmodel->add_project($projectName, $customerID);

Related

Form validation does not work properly in codeIgniter

I have created an employee controller for inserting data into database and I would like to set validation before inserting data into the database.
I have tried following way but every time it shows message field is required even though I have filled values in text box.
1) Controller
<?php
class Employee_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
/* Load form validation library */
$this->load->library('form_validation');
$this->load->library('email');
$this->load->library('session');
$this->load->helper('form');
$this->load->database();
}
function add_employee(){
$this->load->view('Employee_add');
}
function insert_emp(){
$this->form_validation->set_rules('name', 'Name', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('Employee_add');
} else {
$this->load->model('Employee_model');
$inserData = array(
'name' =>$_POST['emp_name'],
'salary' => $_POST['emp_salary']
);
$this->Employee_model->insert_employee($inserData);
$emp_list['emp_records']= $this->Employee_model->list_employee();
$this->load->view('Employee_list',$emp_list);
}
}
}
2) Model code ( following is my model code )
<?php
class Employee_model extends CI_Model {
function __construct() {
parent::__construct();
}
function insert_employee($inserData){
$this->db->insert('employee',$inserData);
return true;
}
}
?>
3) View
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class="center">
<form action="<?php echo site_url('Employee_controller/insert_emp');?>" method="post">
<?php echo validation_errors(); ?>
<div class="element">
<label>Name</label>
<input type="text" name="emp_name" value="">
</div>
<div class="element">
<label>Salary</label>
<input type="text" name="emp_salary" value="">
</div>
<input type="submit" name="submit" value="Add Employee">
</form>
</div>
</body>
</html>
</body>
</html>
Check your $this->form_validation->set_rules method, you are passing name when your fields are emp_name and emp_salary

Search product data not working in codeigniter

I tried to make a search bar so users will be able to search products by searching on the product name but its not working and I'm not able to search a product.
This is my controller function:
<?php
class SearchController extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Search_model');
}
public function index(){
$data = array();
}
function search_keyword(){
$keyword = $this->input->post('keyword');
$data['results'] = $this->Search_model->search($keyword);
//laad de view
$this->load->view('result_view',$data);
}
}
?>
This is my model function:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Search_model extends CI_Model {
function __construct(){
parent::__construct();
}
function search($keyword) {
$this->db->select('product_naam');
$this->db->like('product_naam',$keyword);
$query = $this->db->get('products');
return $query->result_array();
}
}
?>
And this is my view file with searchbar:
<?php include_once('templates/header.php'); ?>
<center>
<h2>Zoek een cadeau</h2><br>
<form class="navbar-form" role="search" action="<?php echo base_url(); ?>SearchController/search_keyword" method="POST">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="keyword" size="30px; ">
<div class="input-group-btn">
<button class="btn btn-default " type="submit" value = "Search"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
</center>
<div class="clearfix"></div>
<table>
<?php foreach($results as $row){ ?>
<tr>
<td><?php echo $row->product_naam?></td>
</tr>
<?php } ?>
</table>
<?php include_once('templates/footer.php'); ?>
I hope someone can help me with this problem!,
thanks
I don't think like() will help you much with your particular needs. Your best bet, I guess, would be to bypass the problem and use a where function containing your LIKE clause:
$this->db->select('product_naam')->from('products')
$this->db->where("product_naam LIKE '%$keyword%'")->get();
Or if you want pure ci you have to change your 'like' line :
$this->db->like('product_naam',$keyword);
For %$query you can use
$this->db->like('product_naam',$keyword,'before');
and for $query% you can use
$this->db->like('product_naam',$keyword,'after');
Because you use $query->result_array();, items should be taken by
<td><?php echo $row['product_naam']?></td>
result_array()
This method returns the query result as a pure array, or an empty
array when no result is produced
CI documentation

Cant validate form data or insert into database (Codeigniter)

I am working with Codeigniter framework these days. I am trying to validate my form data using codeigniter's form_validation library. The problem is I cant get it to work. I dont know where the problem is no error is being shown.
Here is the view page I am calling controller core and function postad in form action
<div class="container table-responsive" id="con1">
<div class="row" id="h4_subad">
<div class="col-md-4"><h4>Submit an Ad</h4></div>
</div>
<form class="form-horizontal" role="form" method="get" action="/core/postad">
<table id="form_table" class="table-responsive table table-striped" id="table_form">
<td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>
<tr> <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
<option>Electronics</option>
<option>Mobiles & Accessories</option>
<option>Computers & Accessories</option>
<option>Kitchen Applicances</option>
<option>Clothing</option>
</select></td></tr>
<tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>
<tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
</table>
</form>
</div>
Controller for this view
class Core extends CI_Controller {
public function index() {
$this->load->view('header1');
$this->load->view('body');
$this->load->view('footer');
}
public function product() {
$this->load->view('header1');
$this->load->view('product');
$this->load->view('footer');
}
public function contact() {
$this->load->view('header1');
$this->load->view('contact');
$this->load->view('footer');
}
public function aboutus() {
$this->load->view('header1');
$this->load->view('aboutus');
$this->load->view('footer');
}
public function reg() {
$this->load->view('header1');
$this->load->view('reg');
$this->load->view('footer');
$this->input->get('username');
}
public function postad() {
$this->load->view('header1');
$this->load->view('postad');
$this->load->view('footer');
$this->load->model('insert_model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
//Validating Name Field
$this->form_validation->set_rules('txt_area', 'Text Area', 'required|min_length[5]|max_length[15]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('postad');
}
else {
//Setting values for tabel columns
$data = array(
'description' => $this->input->post('txt_area'),
);
//Transfering data to Model
$this->insert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('insert_view', $data);
}
}
}
Model for database connectivity
<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
$this->db->insert('post_ad', $data);
}
}
?>
Only trying it for just one field txt_area
I dont want to use codeigniter's form helper as I am using bootstrap form.
You should echo the validation_errors(); as below in your html . For more information you can vist this
<?php echo validation_errors(); ?>
<div class="row" id="h4_subad">
<div class="col-md-4"><h4>Submit an Ad</h4></div>
</div>
<form class="form-horizontal" role="form" method="get" action="/core/postad">
<table id="form_table" class="table-responsive table table-striped" id="table_form">
<td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>
<tr> <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
<option>Electronics</option>
<option>Mobiles & Accessories</option>
<option>Computers & Accessories</option>
<option>Kitchen Applicances</option>
<option>Clothing</option>
</select></td></tr>
<tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>
<tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
</table>
</form>
</div>
You need to use validation_errors(); in posted.php file (HTML) for display error message which you validate.
<?php echo validation_errors(); ?>
Form Validation User Manual
Your button to submit the form is incorrect. When you try to submit a form make sure to set the type to submit too.
<button type="submit">Submit form</button>
or
<submit>Submit form</submit>

Display the result of the search bar in codeigniter

Good day to all I have trouble in codeigniter it suppose to display the data (the first name and the student id of the student) in my oracle database depending on what I type in the search box. for example i the the name of student to the search box when i click the submit button it will display the data of that student. i try to search some code and use it but it displayed error 404 page not found. here is the code
This is for the view mysearch.php
<form action="<?php echo site_url('search/search_keyword');?>" method = "post">
<input type="text" name = "keyword" />
<input type="submit" value = "Search" />
</form>
This is for the controller search.php
Class Search Extends CI_Controller
{
function __Construct()
{
parent::__Construct();
$this->load->database();
$this->load->model('mymodel');
}
function search_keyword()
{
$keyword = $this->input->post('keyword');
$data['results'] = $this->mymodel->search($keyword);
$this->load->view('result_view', $data);
}
}
This is for the Model mymodel.php
Class Mymodel Extends CI_Model
{
function __construct()
{
parent::__construct();
}
function search($keyword)
{
$this->db->like('name',$keyword);
$query = $this->db->get('tablename');
return $query->result();
}
}
This is for the view result_view.php
<table>
<?php
foreach ($results as $row) {
?>
<tr>
<td><?php echo $row->FIRST_NAME?></td>
<td><?php echo $row->STUDENT_ID?></td>
</tr>
<?php
}
?>
</table>

Unidentified Property in Control in my codeigniter framework

I am new to frameworks and trying to send values to database table, I have done it simply but now I am using templates (the famous alemsaeed free AdminLTE) now after setting BASEPATH and giving database connection in database.php,I am still getting errors.
However the code looks fine but I don't know please kindly tell me the reason it's my 2nd week in codeigniter.
MODEL
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mdl extends CI_Model {
function __construct() {
parent::__construct();
}
function form_insert($data){
$data = array(
'name' => $this->input->post('name'),
'fname' => $this->input->post('fname'),
'dob' =>$this->input->post('dob')
);
$this->db->insert('folio', $data);
}
}
?>
Controller
public function index() {
$this->load->helper(array('form','url'));
$this->load->view('myself');
}
public function eid() {
$this->load->helper(array('form','url'));
$this->load->database();
$this->load->model('mdl');
$this->model->form_insert();
$this->load->view('edu');
}
}
?>
View
Note:i have shorted the whole view code because it's a template
For Form Loading the usual
<?php
echo form_open(site_url().'/ctrl/eid/');
$attributes = array('class' => 'sidebar-form');
?>
<label>Enter Your Name</label>
<input type="text" class="form-control" placeholder="YourName" name="name">
<label>Father Name</label>
<input type="text" class="form-control" name="fname" placeholder="father name">
<input type="date" class="form-control" name="dob" placeholder="DD/MM/YYYY" >
<button type="submit" class="btn btn-info pull-right">Submit</button>
</div>
<div class="box-footer">
Copyright © 2015-2016 PIET MULTAN.</strong> All rights reserved.
</div>
</div>
Remove argument $data from form_insert method of model. Like: public function form_insert() {/* rest of code here */} because you are not passing anything from controller. Although I would advice you that you do that passing from controller like:
public function eid()
{
//controller
$data = array();
$data['name'] => $this->input->post('name'),
$data['fname'] => $this->input->post('fname'),
$data['dob'] =>$this->input->post('dob'),
$this->load->helper(array('form','url'));
$this->load->database();
$this->load->model('mdl');
if ( $this->model->form_insert($data) )
{
//success view
}
else
{
//insert fail view
}
}
function form_insert($data)
{
//model
$this->db->insert('folio', $data);
return $this->db->affected_rows() > 0;
}
In Controller
function __construct()
{
parent::__construct();
//load default helpers in this
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('mdl');
}
public function index()
{
$this->load->view('myself');
}
public function eid()
{
$name = $_POST['name'];
$fname = $_POST['fname'];
$dob = $_POST['dob'];
if(!empty($name) || !empty($fname) || !empty($dob))
{
$this->Mdl->form_insert($name, $fname, $dob);//Model name should be Mdl
$this->load->view('edu');
}
else
{
//fields are empty
}
}
In Model
function form_insert($name, $fname, $dob)
{
$data = array(
'name' => $name,
'fname' => $fname,
'dob' => $dob
);
$this->db->insert('folio', $data);
}
In View (Form should be)
<form action="<?php echo base_url();?>index.php/ctrl/eid" method="post" class="sidebar-form">
<label>Enter Your Name</label>
<input type="text" class="form-control" placeholder="YourName" name="name">
<label>Father Name</label>
<input type="text" class="form-control" name="fname" placeholder="father name">
<label>Date Of Birth</label>
<input type="date" class="form-control" name="dob" placeholder="DD/MM/YYYY" >
<button type="submit" class="btn btn-info pull-right">Submit</button>
</form>
The Problem was in the Controller
it should be the modelfileName not model in the code where we load the function
$this->mdl->form_insert();
And this did work :D
The Next problem was in the sitemap
it was this before
$config['site_url']='../../folio/';
instead of this
$config['site_url']='http://localhost/folio/index.php/';
But Once Again i appreciate all for your help it really means to me a lot .
THK_STACOVRFLW

Categories