Codeigniter - inserting into multiple tables - php

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'); ?>">

Related

Codeigniter - Is it possible to add multiple dynamic datas to view

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);
}

Codeigniter Searchbar

Searchbar is working in codeigniter website but it does not load suggestion products. As I want if someone write 'dell' in searchbay he should get dell laptops suggestions below. and one another thing 'enter' button does not work for search one have to click the search icon to search the product.code is below
Controller
function text_search(){
if ($this->crud_model->get_settings_value('general_settings','vendor_system') !== 'ok') {
$search = $this->input->post('query');
$category = $this->input->post('category');
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}else{
$type = $this->input->post('type');
$search = $this->input->post('query');
$category = $this->input->post('category');
if($type == 'vendor'){
redirect(base_url() . 'index.php/home/store_locator/'.$search, 'refresh');
} else if($type == 'product'){
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}
}
}
Model
function get_type_name_by_id($type, $type_id = '', $field = 'name')
{
if ($type_id != '') {
$l = $this->db->get_where($type, array(
$type . '_id' => $type_id
));
$n = $l->num_rows();
if ($n > 0) {
return $l->row()->$field;
}
}
}
function get_settings_value($type, $type_name = '', $field = 'value')
{
if ($type_name != '') {
return $this->db->get_where($type, array('type' => $type_name))->row()->$field;
}
}
View
<div class="header-search">
<?php
echo form_open(base_url() . 'index.php/home/text_search/', array(
'method' => 'post'
));
?>
<input class="form-control" type="text" name="query" placeholder="<?php echo translate('what_are_you_looking_for');?>?"/>
<select
class="selectpicker header-search-select cat_select hidden-xs" data-live-search="true" name="category"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="0"><?php echo translate('all_categories');?></option>
<?php
$categories = $this->db->get('category')->result_array();
foreach ($categories as $row1) {
if($this->crud_model->if_publishable_category($row1['category_id'])){
?>
<option value="<?php echo $row1['category_id']; ?>"><?php echo $row1['category_name']; ?></option>
<?php
}
}
?>
</select>
<?php
if ($this->crud_model->get_type_name_by_id('general_settings','58','value') == 'ok') {
?>
<select
class="selectpicker header-search-select" data-live-search="true" name="type" onchange="header_search_set(this.value);"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="product"><?php echo translate('product');?></option>
<option value="vendor"><?php echo translate('vendor');?></option>
</select>
<?php
}
?>
<button class="shrc_btn"><i class="fa fa-search"></i></button>
</form>
</div>
<!-- /Header search -->

update multiple form using codeiginiter

I want to update data for multiple users using codeginiter, I used the bellow script but it only updates for one user.
<?php echo form_open(base_url() . 'get/save_payment', array('role' => 'form')); ?>
<?php foreach ($mature as $key => $value) { ?>
<input type="text" class="form-control" value="<?php echo $value->id; ?>" readonly name="id[]">
<input type="text" class="form-control" value="<?php echo $value->name; ?>" readonly name="name[]">
<input type="text" class="form-control" value="0" readonly name="amount[]">
<?php } ?>
<button type="submit" class="btn btn-success">Confirm Request</button>
<?php echo form_close(); ?>
CONTROLLER
public function save_payment() {
$this->load->library('form_validation');
$this->form_validation->set_rules('id[]', 'id', 'required|trim');
$this->form_validation->set_rules('name[]', 'name', 'trim');
$this->form_validation->set_rules('amount[]', 'Amount', 'required|max_length[4]|trim|strip_tags');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('id', set_value('id'));
$this->session->set_flashdata('name', set_value('name'));
$this->session->set_flashdata('amount', set_value('amount'));
$this->payment();
} else {
$id = set_value('id[]');
$data['amount'] = set_value('amount[]');
$data['name'] = set_value('id[]');
$this->dashboard->update_ph($data,$id);
}
}
Here is the MODEL :
public function update_ph($data,$id)
{
$this->db->where('id', $id);
$this->db->update('gw_ph', $data);
}
Kindly explain on how I can go through this code and get it working without error.
Thanks
Codeigniter use 'name' attributte for get data from 'input' and you use same 'name' attributte for all of your readonly input

Editing and updating an item in CodeIgniter

I'm having trouble to edit and update an item in my first CodeIgniter project. When I click on the edit button, first of all, it loads the view with the inputs filled with the informations of that item on the database, but it doesn't load the bootstrap that I'm using.
Then, when I click the "Edit" button, returns lots of errors saying that:
my function on the controllers is missing an argument and the variable is undefined, and
my view has an undefined offset and is trying to get property of non-object.
All the other commands for the CRUD are working, except this.
The version of CodeIgniter I'm using is 2.2.6
Codes:
Model:
function edit($id)
{
$this->db->where('id', $id);
$query = $this->db->get('products');
return $query->result();
}
function update($data)
{
$this->db->where('id', $data['id']);
$this->db->set($data);
return $this->db->update('products');
}
Controller:
function edit($id)
{
$data['title'] = "Edit Product";
$data['product_data'] = $this->model->edit($id);
$this->load->view('productEdit', $data);
}
function update()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<span>', '</span>');
$validations = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|max_length[255]'
),
array(
'field' => 'price',
'label' => 'Price',
'rules' => 'trim|required|max_length[255]'
),
array(
'field' => 'stock_quantity',
'label' => 'In Stock',
'rules' => 'trim|required|max_length[255]'
)
);
$this->form_validation->set_rules($validations);
if ($this->form_validation->run() === FALSE) {
$this->edit($this->input->post('id'));
} else {
$data['id'] = $this->input->post('id');
$data['name'] = $this->input->post('name');
$data['price'] = $this->input->post('price');
$data['stock_quantity'] = $this->input->post('stock_quantity');
if ($this->model->update($data)) {
redirect('products');
} else {
log_message('error', 'Error');
}
}
}
View:
<?php echo form_open('products/edit/', 'id="form-products"'); ?>
<input type="hidden" name="id" value="<?php echo $product_data[0]->id; ?>"/>
<label for="nome">Product Name:</label><br/>
<input type="text" name="name" value="<?php echo $product_data[0]->name; ?>"/>
<div class="error"><?php echo form_error('name'); ?></div>
<label for="email">Price:</label><br/>
<input type="text" name="price" value="<?php echo $product_data[0]->price; ?>"/>
<div class="error"><?php echo form_error('price'); ?></div>
<label for="email">In Stock:</label><br/>
<input type="text" name="stock_quantity" value="<?php echo $product_data[0]->stock_quantity; ?>"/>
<div class="error"><?php echo form_error('stock_quantity'); ?></div>
<input type="submit" name="update" value="Update" />
<?php echo form_close(); ?>
Thank you for your time.
Try this one
Update your model
function edit($id)
{
$this->db->where('id', $id);
$query = $this->db->get('products');
return $query->row(0);
}
and then in your view.
<input type="hidden" name="id" value="<?php echo $product_data->id; ?>"/>
also change your form action
products/update/

set value default codeigniter

I have this function (get_profile_info)
function get_profile_info()
{
$this->db->where('username',$this->session->userdata('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() == 1){
foreach ($get_profile_info->result() as $row)
{
echo $row->firstname;
echo $row->lastname;
}
}
}
The question is how do i put this results: firstname and lastname into input form default set_value: echo "Firstname".form_input('firstname2');
Try this code:
foreach($get_profile_info->result() as $row)
{
$data = array(
'name' => 'firstname',
'id' => 'firstname',
'value' => $row->firstname,
);
echo form_input($data);
// do the same for lastname
}
Set input value of your view file like this.
<input type="text" value="<?php echo $data['firstname']; ?>" />
you have to pass data array from controller.
Try this
function get_profile_info()
{
$this->db->where('username',$this->session->userdata('username'));
$get_profile_info = $this->db->get('memberships');
if($get_profile_info->num_rows() == 1){
return $get_profile_info->row();
}
function your_controller
{
$data['row'] = get_profile_infor()
$this->load->view("yourview",$data)
}
IN View
<input type="text" name="firstname" value="<?php echo set_value('firstname',$row->firstname)?>" />
<input type="text" name="lastname" value="<?php echo set_value('lastname',$row->lastname)?>" />

Categories