I am having a trouble on what ways will i do, when i change this $this->load->view('profile',['getPlacetype'=>$getPlacetype],$custom_errors ); to this
$this->load->view('profile',['getPlacetype'=>$getPlacetype],$custom_errors );
there will be no undefined index but the selection data will be none
profilecon
public function index(){
$this->load->model('Placetype');
$getPlacetype = $this->Placetype->getPlacetype();
if (!isset($_SESSION['username']) || empty($_SESSION['username'])) {
header("Location: ".base_url()."index.php/login_con/login");
die();
}
$custom_errors["nadate"] = false;
if (isset($_POST['profile'])) {
$this->form_validation->set_rules('mobilenum', 'Mobile Number',
'required');
$this->form_validation->set_rules('homeadd', 'Home Address',
'required');
$this->form_validation->set_rules('startdate', 'Start Date',
'required');
$this->form_validation->set_rules('enddate', 'End Date',
'required');
$custom_errors["nadate"] = ($this->verifydate($_POST["startdate"],
$_POST["enddate"]) < 3 ? false : true);
//if form validation true
if($this->form_validation->run() == TRUE && $this-
>verifydate($_POST["startdate"], $_POST["enddate"]) < 3) {
$data = array(
'username' => $this->session->userdata("username"),
'mobilenum' => $_POST['mobilenum'],
'homeadd' => $_POST['homeadd'],
'startdate' => $_POST['startdate'],
'enddate' => $_POST['enddate'],
);
$_SESSION["profile_data"] = $data;
redirect("shopping_cart","refresh");
}
}
$this->load->view('profile',
['getPlacetype'=>$getPlacetype],$custom_errors );
}
placetype_model
<?php
class Placetype extends CI_MODEL{
public function getPlacetype(){
$query = $this->db->get('placetype');
if($query->num_rows() > 0){
return $query->result();
}
}
}
profile_view
<div class="form-group">
<label for="place_type">City/Province</label>
<select name="place_type">
<option value="place_type">-Please Select One-</option>
<?php if(count($getPlacetype)):?>
<?php foreach($getPlacetype as $getplacetype):?>
<option value=
<?php echo $getplacetype->place_id;?>>
<?php echo $getplacetype->place_type;?>
<?php echo $getplacetype->place_price;?><span> PHP Shipping
Fee</span>
</option>
<?php endforeach;?>
<?php else:?>
<?php endif;?>
</select>
</div>
<button class="btn btn-primary btn-block"
name="profile">Proceed</button>
</div>
</form>
</div>
It's not hard to send lots of data to a view. The thing to understand is how the array you send to the view is used. Put simply, each key in the array will be the name of a variable in the view. The array must be an associative array.
With that in mind
//this line
$getPlacetype = $this->Placetype->getPlacetype();
// should be changed to this
$view_data['getPlacetype'] = $this->Placetype->getPlacetype();
and then
$custom_errors["nadate"] = ($this->verifydate($_POST["startdate"],
// changed to
$nadate = ($this->verifydate($_POST["startdate"],
$_POST["enddate"]) < 3 ? false : true);
You can delete the line
$custom_errors["nadate"] = false;
Change this
if($this->form_validation->run() == TRUE &&
$this->verifydate($_POST["startdate"], $_POST["enddate"]) < 3) {
to
if($nadate === TRUE && $this->form_validation->run() === TRUE) {
If the above block is not executed then create an error message and load the view
$view_data['custom_errors'] = "Start and End dates were wrong";
$this->load->view('profile', $view_data);
The relevant part of the view could be done like this.
<div class="form-group">
<label for="place_type">City/Province</label>
<select name="place_type">
<option value="place_type">-Please Select One-</option>
<?php
if(count($getPlacetype)) :
foreach ($getPlacetype as $getplacetype):
?>
<option value=<?php echo $getplacetype->place_id; ?>>
<?php
echo $getplacetype->place_type;
echo $getplacetype->place_price;
?>
<span> PHP Shipping Fee</span>
</option>
<?php
endforeach;
else:
echo isset($custom_errors) ? $custom_errors : NULL;
endif;
?>
</select>
</div>
Hope this demonstrates how to do what you want.
try this:define array blank.
public function index(){
$data = array();
//here you can use $data and like firstArray you can pass in your view page.you can use multiple array like this and pass then in view.
$data['firstArray']=get data;
$data['secondArray']=get data;
$this->load->view(''profile', $data);
}
Related
How to Save the Checkbox Value Form to MySQL in One Field, can anyone help me..
Here is my code:
function student($param1 = '') {
if ($this->session->userdata('admin_login') != 1) {
redirect('login', 'refresh');
}
$running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
if ($param1 == 'create') {
$data['ekskul_id'] = $this->input->post('ekskul_id');
$this->db->insert('student', $data);
}
}
this is my display code
<div class="form-group">
<label for="field-2" class="col-sm-3 control-label">Ekstrakurikuler</label>
<div class="col-sm-5">
<select data-placeholder="Select" name="ekskul_id[]" multiple class="chosen-select" tabindex="8">
<?php
$ekstra = $this->db->get('ekstrakurikuler')->result_array();
foreach($ekstra as $row):
?>
<option value="<?php echo $row['ekskul_id'];?>"><?php echo $row['ekskul_name'];?></option>
<?php
endforeach;
?>
</select>
</div>
</div>
use below code to store multiple id in one field:
<?php
$ekskul_id= implode(",",$_POST['ekskul_id']);
?>
In your code :
if(isset($this->input->post('ekskul_id'))){
$data['ekskul_id'] = implode(",",$this->input->post('ekskul_id'));
}
I am having problems with a booking form in Codeigniter; when a visitor makes a booking, the details are supposed to be inserted into two tables in a database ('reservation', and 'period', there is an FK from 'reservation' to 'period').
When I try to make the booking, nothing appears in the database, and I can't tell if it a problem with the form, or between the controller and model (there are no errors thrown - just nothing in the db). I tried to follow the logic from this example.
here is my form (gite-booking-form.php):
<html>
<head>
<title>Gite booking form</title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('GiteCtl/giteBookingForm'); ?>
<h5>Select a gite:</h5>
<?php
$gitenames = array('giteA', 'giteB', 'giteC', 'giteD');
$gitename = isset($_POST['gitename']) && in_array($_POST['gitename'], $gitenames) ? $_POST['gitename'] : 'giteA';
echo '<select name="gitename">';
foreach ($gitenames as $option) {
echo '<option value="' . $option . '"' . (strcmp($option, $gitename) == 0 ? ' selected="selected"' : '') . '>' . $option . '</option>';
}
echo '</select>';
?>
<h5>Start date</h5>
<input type="date" value="<?php echo set_value('start_date'); ?>"/>
<h5>End date</h5>
<input type="date" value="<?php echo set_value('end_date'); ?>"/>
<div><input type="submit" value="Submit"/></div>
</form>
</body>
</html>
my model (Gite.php):
<?php
class Gite extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function reservationInsert($table, $data)
{
$query = $this->db->insert($table, $data);
return $this->db->insert_id();
}
}
and the controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class GiteCtl extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Gite');
$this->load->library('calendar');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('Visitors');
}
public function isLoggedIn()
{
$isLoggedIn = $this->session->userdata('isLoggedIn');
if (!isset($isLoggedIn) || $isLoggedIn != TRUE) {
echo 'In order to book a gite, you must first login';
echo 'button: visitor-login.php'; // I will add this later
} else {
$this->giteBookingForm();
}
}
public function giteBookingForm()
{
$this->form_validation->set_rules('gite_selection', 'gite_select', 'required');
$this->form_validation->set_rules('start_date', 'startDate', 'required', array('required' => 'You must choose a %s.'));
$this->form_validation->set_rules('end_date', 'endDate', 'required', array('required' => 'You must provide an %s.')
);
if ($this->form_validation->run() == FALSE) {
$this->load->view('gite-booking-form');
} else {
$start_date = $this->input->post('start_date');
$end_date = $this->input->post('end_date');
$isHauteSaison = 1;
$isWeekend = 1;
$period_data = array(
'start_date' => $start_date,
'end_date' => $end_date,
'isHauteSaison' => $isHauteSaison,
'isWeekend' => $isWeekend
);
$gite_selection = $this->input->post('gite_selection');
$visitor_email = $this->session['visitorName'];
$price = 100.00;
$period_id = $this->Gite->reservationInsert('period', $period_data);
$reservation_data = array(
'gite_selection' => $gite_selection,
'visitor_email' => $visitor_email,
'price' => $price,
'period' => $period_id
);
$insert = $this->Gite->reservationInsert('reservation', $reservation_data);
}
}
}
Thanks,
E.
In the end it was the form; the php for retrieving data from the form was more complicated than I thought:
<h5>Select a gite:</h5>
<?php
$gitenames = array('giteA','giteB','giteC','giteD');
$gitename = isset($_POST['gitename']) && in_array($_POST['gitename'],$gitenames)?$_POST['gitename']:'giteA';
echo '<select name="gitename">';
foreach($gitenames as $option) {
echo '<option value="'.$option.'"'.(strcmp($option,$gitename)==0?' selected="selected"':'').'>'.$option.'</option>';
}
echo '</select>';
?>
<h5>Start date</h5>
<input type="date" class="form-control" name="start_date" value="<?php echo isset($start_date) ? set_value('start_date', date('Y-m-d', strtotime($start_date))) : set_value('start_date'); ?>">
<h5>End date</h5>
<input type="date" class="form-control" name="end_date" value="<?php echo isset($end_date) ? set_value('end_date', date('Y-m-d', strtotime($end_date))) : set_value('end_date'); ?>">
Trying to update college profile which contains all the college info in the database, Some records are manually filled in by data entry, some records have to be updated by the college admin or super admin.
The issue is when I insert an array (Facilities[ ]) into the Db it inserts without any errors, but when i update the same it gives me an error.
The Insertion was done through another controller with insert statement
$company_id = $this->companies_model->add_company($company_array);
which works flawlessly, But update function gives me this error
Error Number: 1054
Unknown column 'Array' in 'field list'
UPDATE pp_companies SET facilities = Array, company_description = 'Established in the year....', company_join = 'Why Join us', WHERE ID = '201'
Filename: C:\xampp\htdocs\jobportal\jp_sys\database\DB_driver.php
Line Number: 287
Facilities = 'Array'? any idea how this shows?
Array ( [0] => Engineering [1] => Management )
sends the value when i check through
print_r($this->input->post('facilities'));
But it doesnt get updated in the table
View
<div class="input-group <?php echo (form_error('company_description'))?'has-error':'';?>">
<label class="input-group-addon">College<br> Description <span>*</span></label>
<textarea class="form-control" name="company_description" id="company_description" rows="8" cols="30" ><?php echo $company_description; ?></textarea>
<?php echo form_error('company_description'); ?> </div>
<div class="input-group <?php echo (form_error('company_join'))?'has-error':'';?>">
<label class="input-group-addon">Why join <br> our College? <span>*</span></label>
<textarea class="form-control" name="company_join" id="company_join" rows="8" cols="30" ><?php echo $company_join; ?></textarea>
<?php echo form_error('company_join'); ?> </div>
<?php echo $facilities ?>
<div class="input-group boxwraper <?php echo (form_error('facilities'))?'has-error':'';?>">
<label class="input-group-addon">Facilities Offered <span></span></label>
<select name="facilities[]" class="js-example-tags" multiple="multiple">
<option value="Library" >Library</option>
<option value="Gym" >Gym</option>
<option value="Canteen" >Canteen</option>
.
.
</select>
</div>
<script type="text/javascript">
$(".js-example-tags").select2({
tags: true
})
</script>
Model
public function add_company($data){
$return = $this->db->insert('pp_companies', $data);
if ((bool) $return === TRUE) {
return $this->db->insert_id();
} else {
return $return;
}
}
public function update_company($id, $data){
$this->db->where('ID', $id);
$return=$this->db->update('pp_companies', $data);
return $return;
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Edit_College extends CI_Controller {
public function index()
{
$row = $this->employers_model->get_employer_by_id($this->session->userdata('user_id'));
$data['row'] = $row;
$data['title'] = $row->company_name.' - Edit Profile';
$this->form_validation->set_rules('facilities[]', 'Facilities', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('company_description', 'Company Description', 'trim|required|strip_all_tags|secure');
$this->form_validation->set_rules('company_join', 'Company Join', 'trim|required|strip_all_tags|secure');
$data['facilities'] = (set_value('facilities'))?set_value('facilities'):$row->facilities;
$data['company_description'] = (set_value('company_description'))?set_value('company_description'):$row->company_description;
$data['company_join'] = (set_value('company_join'))?set_value('company_join'):$row->company_join;
if ($this->form_validation->run() === FALSE) {
$this->load->view('employer/edit_company_profile_view',$data);
return;
}
//for facility addition into db
$arrcategory1 = $this->input->post('facilities');
foreach($arrcategory1 as $val1)
{
$categoryarr1 = $categoryarr1 . $val1. ",";
}
$categoryarr1 = substr(trim($categoryarr1), 0, -1); //rtrim($categoryarr,",")
$company_array = array(
'facilities'=> $arrcategory1,
'company_description' => $this->input->post('company_description'),
'company_join' => $this->input->post('company_join'),
$this->companies_model->update_company($row->company_ID, $company_array);
$this->session->set_flashdata('msg', '<div class="alert alert-success">×<strong>Success!</strong> Your company profile has been updated.</div>');
$this->session->set_userdata('slug',$company_slug);
redirect(base_url('employer/edit_college'));
}
}
OK, so I have looked everywhere for a solution to my problem but found none so far.My code looks like this.I have created a dynamic view page in which edit and add views are loaded dynamically.But the problem arises when i try to retain the value of select dropdown during editing.I would be grateful if someone could help me out.
View
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php if(#$patient_info){
echo '<h1 class="page-header">Edit Patient</h1>';
}else{
echo '<h1 class="page-header">Add Patient</h1>';
}
?>
<?php if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div>
<?php } ?>
<?php if($this->session->flashdata('erroredit')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('erroredit'); ?></div>
<?php } ?>
<?php //echo validation_errors('<div class="alert alert-danger">','</div>'); ?>
<form role="form" method="post" action="<?php echo isset($patient_info) ? site_url('home/patient/edit') .'/' .$patient_info->patientID : site_url('home/patient/new'); ?>">
<div class="form-group">
<?php echo form_error('pname', '<div class="alert alert-danger">', '</div>'); ?>
<label for="pname">Patient Name</label>
<input class="form-control" placeholder="Enter Patient Name" name="pname" value="<?php echo isset($patient_info) ? $patient_info->patientName : ''; ?>">
</div>
<!-- Dropdown menu for selecting clinic -->
<div class="form-group">
<label for="select">Select Clinic Name</label>
<select class="form-control" name="selectClinic">
<option value="none">Select Clinic Below</option>
<?php foreach($allclinic as $key=>$clinic){ ?>
<!--<?php //foreach($clinicByPatient as $clin): ?>-->
<option value="<?php $clinic->clinicID; ?>"
<?php if(isset($patient_info)){
echo 'selected="selected"';
}
?>
>
<?php echo $clinic->clinicName; ?>
</option>
<?php //endforeach; ?>
<?php } ?>
</select>
</div>
<!-- Select Clinic ends-->
<div class="form-group">
<label for="select">Select Dentist</label>
<select class="form-control" name="selectDentist">
<option value="">Select Dentist</option>
<?php foreach($dentistdet as $key=>$dentist){ ?>
<option value="<?php echo $did = $dentist->dentistID;?>"><?php echo $dentist->dentistName; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="<?php echo isset($patient_info) ? 'Update Patient' : 'Add Patient'; ?>" >
</div>
</form>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg btn-block" value="Cancel">
</div>
Edit controller
public function edit(){
$id = $this->uri->segment(4);
$this->load->library('form_validation');
$this->load->model('clinic_model');
$this->load->model('dentist_model');
$data['patient_info'] = $this->patient_model->getPatientById($id);
$data['clinicByPatient'] = $this->patient_model->getPatientByClinic();
$data['allclinic'] = $this->clinic_model->getAllClinics();
// $data['clinicdet'] = $this->patient_model->getPatientByClinic();
if($_POST){
$this->form_validation->set_rules('pname', 'Patient Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('paddress', 'Patient Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('pcontact', 'Patient Contact', 'trim|required|xss_clean');
if($this->form_validation->run()== FALSE){
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout', $data);
}else{
$patientname = $this->input->post('pname');
$patientaddress = $this->input->post('paddress');
$patientcontact = $this->input->post('pcontact');
$select = $this->input->post('selectClinic');
$option = $this->input->post('selectDentist');
$edited = $this->patient_model->editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option);
if($edited){
$this->session->set_flashdata('successedit', 'Successfully updated the record');
redirect('home/patient');
}
}
}else{
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout',$data);
}
}
Model looks like this
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Patient_model extends CI_Model{
public function countPatients(){
$countPatient = $this->db->count_all('patient');
return $countPatient;
}
public function getallpatients(){
$query = $this->db->get('patient');
if($query->num_rows()>0){
return $query->result();
}
else{
return FALSE;
}
}//getallpatients function ends
public function getPatientByClinic(){
$this->db->select('*');
$this->db->from('patient');
$this->db->join('clinic', 'patient.clinicID = clinic.clinicID', 'left');
$this->db->join('dentist', 'patient.dentistID = dentist.dentistID', 'left');
$query = $this->db->get();
if($query->num_rows>0){
return $query->result();
}
}
public function addPatientByClinic($patientname, $patientadd, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientadd,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
return $this->db->insert('patient',$data);
}// method ends
public function deletePatient($id){
$verifyID = array('patientID' => $id);
// $affRows = $this->db->affected_rows();
// $obj = new Patient_model;
if($verifyID){
$this->db->where($verifyID);
$this->db->delete('patient');
if($this->db->affected_rows()){
return TRUE;
}
}
}
public function editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientaddress,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
$query = $this->db->where('patientID', $id)
->update('patient', $data);
if($query){
return true;
}
}//method ends
public function getPatientById($id){
$query = $this->db->where('patientID', $id)
->get('patient');
return $query->row();
}
}//class ends
?>
In the code for your select box, you aren't actually echoing $clinic->clinicID. Thus, when the form is submitted, the value will be empty.
You also need to be careful with how you are choosing which select element will be selected by default - you aren't comparing with anything that will change within the loop. Should you be checking against $clinic->clinicID?
I am new to codeigniter and I'm having a problem in retrieving data from database to drop down list. Can anyone help me with this?
My View:
<?php echo form_open('form/myform'); ?>
<select id="addother" >
<option value="none" selected="selected"> ------Select School------ </option>
<?php foreach($groups as $row) {
echo '<option value="'.$row->id.'">'.$row->name.'</option>';
} ?>
</select>
<div id="addother">
<?php echo form_input(array('id'=>'addother_input', 'name'=>'school', 'placeholder'=>'Enter name of school...')); ?>
<input type="submit" id="add" name="submit" value="+" />
</div>
<?php echo form_close(); ?>
My Controller:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if($this->input->post()){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}
My Model:
function getAll() {
$query = $this->db->get('tblschool');
return $query->result();
}
function addItem($sdata){
return $this->db->insert('tblschool', $sdata);
}
I can't retrieve data from database into the drop down list. I would really appreciate your help. Thanks!
i think that you could be persist the select value when the form is submitted in the "myForm" method. So, in the same method, retrieve the new group values. Try this:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if ($this->form_validation->run() == TRUE){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}