update query not updating table in transactions - php

I m trying to get and update data at same time but after getting data transaction is rolled back, because table is not updating.
Controller -
public function addAmount()
{
$this->form_validation->set_rules('balance_id', 'Balance Id', 'required|trim');
$this->form_validation->set_rules('farmer_id', 'farmer_id', 'required|trim');
$this->form_validation->set_rules('amount', 'amount', 'required|trim');
$this->form_validation->set_rules('amount_discount', 'discount', 'required|trim');
$this->form_validation->set_rules('payment_mode', 'payment mode', 'required|trim');
if ($this->form_validation->run() == false) {
$data = array(
'amount' => form_error('amount'),
'balance_id' => form_error('balance_id'),
'farmer_id' => form_error('farmer_id'),
'amount_discount' => form_error('amount_discount'),
'payment_mode' => form_error('payment_mode'),
);
$array = array('status' => 'fail', 'error' => $data);
echo json_encode($array);
} else {
$id =$this->generate_otp();
$data = array(
'farmer_id' => $this->input->post('farmer_id'),
'balance_id' => $this->input->post('balance_id'),
'amount_paid' => $this->input->post('amount'),
'paying_date' => $this->input->post('date'),
'amount_discount' => $this->input->post('amount_discount'),
'description' => $this->input->post('description'),
'payment_mode' => $this->input->post('payment_mode'),
'payment_id' =>$id,
);
$inserted_id = $this->advance_model->amount_deposit($data);
echo '<pre>'; print_r($inserted_id); echo ("</pre>"); exit();
$array = array('status' => 'success', 'error' => '');
echo json_encode($array);
}
}
Model:
public function amount_deposit($data)
{
$this->db->trans_start(); // Query will be rolled back
$paid_amount = $data['amount_paid'];
$this->db->insert('tbl_pay_amount', $data);
$inserted_id = $this->db->insert_id();
if ($data['balance_id'] != null) {
$query = $this->db->select('balance,balance_id,reason')
->from('tbl_balance')
->where('balance_id', $data['balance_id'])
->get();
return $query->row();
if(!empty($query)){
$b =$query['balance'];
$balance =$b - $paid_amount;
}
$this->db->update('tbl_balance', array('balance' => $balance));
}
$this->db->trans_complete(); # Completing transaction
if ($this->db->trans_status() === false) {
$this->db->trans_rollback();
return false;
} else {
$this->db->trans_commit();
return json_encode(array('invoice_id' => $inserted_id, 'sub_invoice_id' => 1));
}
}
When amount to be added the balance amount need to be update automatically, but now transaction is roll back because not able to update balance amount after getting it from same table, thanks in advance.

Just added foreach loop and removed return statement now it's working fine.
public function amount_deposit($data)
{
$this->db->trans_start(); // Query will be rolled back
$paid_amount = $data['amount_paid'];
$this->db->insert('tbl_pay_amount', $data);
$inserted_id = $this->db->insert_id();
if ($data['balance_id'] != null) {
$query = $this->db->select('balance,balance_id,reason')
->from('tbl_balance')
->where('balance_id', $data['balance_id'])
->get();
$result = $query->result();
}
if (!empty($result) && isset($result)) {
foreach ($result as $key => $balance_value) {
$balance_id = $balance_value->balance_id;
$balanceAmount = $balance_value->balance;
$balance = $balanceAmount - $paid_amount;
}
$this->db->where('balance_id', $balance_id);
$this->db->update('tbl_balance', array('balance' => $balance));
}
$this->db->trans_complete(); # Completing transaction
if ($this->db->trans_status() === false) {
$this->db->trans_rollback();
return false;
} else {
$this->db->trans_commit();
return json_encode(array('invoice_id' => $inserted_id, 'sub_invoice_id' => 1));
}
}

Related

getting multiple customer details In codeigniter

i have written this code to receive data from the Android device. it was inserted just one customer data I need to receive multiple customer details if app goes offline. but it was inserting one data into DB in offline mode also.how can i change this for multiple customer data insertions.
function index_post($customerID = false) {
if ($customerID) {
//update the record
$updateData = array();
$allowedparams = array('streetid' => 'streetid', 'name' => 'name', 'mobile' => 'mobile', 'adhaar' => 'adhaar', 'profession' => 'profession', 'address' => 'address', 'pincode' => 'pincode', 'nearby' => 'nearby', 'paddress' => 'paddress', 'isOwned' => 'isOwned');
foreach ($allowedparams as $k => $v) {
if (!$this->IsNullOrEmptyString($this->post($k, true))) {
$updateData[$v] = $this->post($k, true);
}
}
if ($this->model_customer->update($customerID, $updateData)) {
$data = array('status' => true, 'messsage' => 'cusotmer updated succesfully');
$http_code = REST_Controller::HTTP_OK;
} else {
$data = array('status' => false, 'messsage' => 'cusotmer failed to update.');
$http_code = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
}
} else {
//insert the record
$allowedparams = array('streetid' => 'streetid', 'name' => 'name', 'mobile' => 'mobile', 'adhaar' => 'adhaar', 'profession' => 'profession', 'address' => 'address', 'pincode' => 'pincode', 'cycle' => 'cycle', 'nearby' => 'nearby', 'paddress' => 'paddress', 'isOwned' => 'isOwned');
$requiredParam = array('streetid', 'name', 'mobile', 'cycle');
$insertdata = array();
foreach ($allowedparams as $k => $v) {
if (in_array($k, $requiredParam)) {
//check if its not null
if ($this->post($k) == null || trim($this->post($k)) == '') {
$data = array('status' => false, 'message' => $k . ' parameter missing or empty');
$http_code = REST_Controller::HTTP_BAD_REQUEST;
break;
}
}
$insertData[$v] = $this->post($k, true);
}
if ($customerID = $this->model_customer->create($insertData)) {
$data['customerID'] = $this->_frameCustomer2($this->model_customer->get($customerID)); //you need to put
$http_code = REST_Controller::HTTP_OK;
} else {
$data = array('status' => false, 'message' => 'unable to create customer');
$http_code = REST_Controller::HTTP_INTERNAL_SERVER_ERROR;
}
}
$this->response($data, $http_code);
}
private function _frameCustomer2($c) { //get value from index_get
$data = array();
$data['id'] = $c->id;
$data['name'] = $c->name;
$data['street'] = array('id' => $c->streetid);
$data['creationDate'] = $c->creationdate;
$data['mobile'] = $c->mobile;
$data['adhaar'] = $c->adhaar;
$data['profession'] = $c->profession;
$data['isOwned'] = ($c->isOwned == 1) ? true : false;
$data['address'] = $c->address;
$data['pincode'] = $c->pincode;
$data['status'] = $c->status;
$data['cycle'] = $c->cycle;
$data['balance'] = $c->balance;
$data['creditAvailable'] = $c->creditbalance;
$data['nearby'] = $c->nearby;
$data['accountNumber'] = $c->accountnumber;
$data['permanentAddress'] = $c->paddress;
$data['lastVisit'] = $this->model_customer->lastVisit($c->id);
return $data;
}
and my part of model function is
function create($insertdata = array()) { //new customer insert
if ($this->db->insert('customer', $insertdata)) {
return $this->db->insert_id();
} else {
return false;
}
}
function update($customerID = 0, $updateData = array()) {
$this->db->where('id', $customerID);
if ($this->db->update('customer', $updateData) && $this->db->affected_rows() == 1) {
return true;
} else {
return false;
}
Instead of customer Id, you can ask the mobile developers to send data in the form of array. In both online and offline. In case of online there will be just one element in the request array.
function index_post() {
$request_data = $this->request->body;
foreach($request_data as $key => $value)
{
//Check if customer id is present
if(Customer present)
{
Update the record
} else {
Insert the record
}
}
}

Codeigniter multiple where condition

I have a problem i only want to select a specific row/s with user_id = $this->session->id , status = 4 hours, 8 hours and work from home.
EDIT:
all my status is
Work From Home
8 hours
4 hours
Vacation Leave
Sick Leave
but what happened is it only returns all the Work From Home of the specific user_id and not included the 4 hours and 8 hours status
Controller
$order_by = "id desc";
$where = ['user_id' => $this->session->id,'status' => '4 hours', 'status' => '8 hours','status' => 'Work From Home'];
$this->Crud_model->fetch('record',$where,"","",$order_by);
MODEL
public function fetch($table,$where="",$limit="",$offset="",$order=""){
if (!empty($where)) {
$this->db->where($where);
}
if (!empty($limit)) {
if (!empty($offset)) {
$this->db->limit($limit, $offset);
}else{
$this->db->limit($limit);
}
}
if (!empty($order)) {
$this->db->order_by($order);
}
$query = $this->db->get($table);
if ($query->num_rows() > 0) {
return $query->result();
}else{
return FALSE;
}
}
Try the following:
Controller:
$order_by = "id desc";
$where = ['user_id' => $this->session->id];
$where_in = ['name' => 'status', 'values' => ['4 hours', '8 hours', 'Work From Home']];
$this->Crud_model->fetch('record',$where,"","",$order_by, $where_in);
Model:
public function fetch($table,$where="",$limit="",$offset="",$order="", $where_in = false){
if (!empty($where)) {
$this->db->where($where);
}
if (!empty($limit)) {
if (!empty($offset)) {
$this->db->limit($limit, $offset);
}else{
$this->db->limit($limit);
}
}
if (!empty($order)) {
$this->db->order_by($order);
}
if($where_in)
{
$this->db->where_in($where_in['name'], $where_in['values']);
}
$query = $this->db->get($table);
if ($query->num_rows() > 0) {
return $query->result();
}else{
return FALSE;
}
}
This uses where_in functionality to get all records for those status values.
Associate Array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
Or if you want to do something other than = comparison
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array);

php Prestashop- where can I find the following functions

In a Prestashop Ecommerce website I have a module that was created specially to compare and set products price in my website,
I need some help understand some of his PHP code.
The followng code set the price of a product in my website after scanning and finding the minumum price in other website link providede at the product backend page:
My Question is: How does this code works? where is the part of the code that scan the other website and get the minumum price in it?
<?php
class ProductCMP extends Module
{
public function __construct()
{
$this->name = 'productcmp';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'biznesownia';
parent::__construct();
$this->displayName = $this->l('ProductCMP', FALSE, NULL);
}
public function install()
{
return parent::install()
and $this->registerHook('displayAdminProductsExtra')
and $this->registerHook('actionAdminProductsControllerSaveAfter');
}
public function hookDisplayAdminProductsExtra($params)
{
$product = new Product((int) Tools::getValue('id_product'), false);
$tpl = $this->context->smarty->createTemplate(dirname(__FILE__).'/templates/tab.tpl', $this->context->smarty);
$tpl->assign('product', $product);
return $tpl->fetch();
}
public function hookActionAdminProductsControllerSaveAfter($params)
{
# set data
$id_product = (int) Tools::getValue('id_product');
$product = new Product($id_product, false, (int) $this->context->language->id);
$product->compare_active = (bool) Tools::getValue('compare_active', false);
$product->compare_link = (string) Tools::getValue('compare_link', '');
$product->compare_price = (string) Tools::getValue('compare_price', 0);
return $product->save();
}
public function compare()
{
define('PRODUCT_COMPARE_EMAIL', 'uditools#gmail.com');
set_time_limit(60*60);
# get list
$products = Db::getInstance()->executeS("
SELECT p.`id_product`, pl.`name`
FROM `"._DB_PREFIX_."product` p
LEFT JOIN `"._DB_PREFIX_."product_lang` pl ON (p.`id_product` = pl.`id_product` ".Shop::addSqlRestrictionOnLang('pl').")
WHERE
pl.`id_lang` = ".(int)$this->context->language->id."
AND p.`compare_active`=1
ORDER BY p.`id_product`
");
# job
$report = array();
foreach($products as $p)
{
try
{
$result = $this->_compareProduct((int) $p['id_product']);
if($result && $result['changed'])
{
$result['id_product'] = $p['id_product'];
$result['name'] = $p['name'];
$report[] = $result;
}
}
catch(Exception $e)
{
$report[] = array(
'id_product' => $p['id_product'],
'name' => $p['name'],
'res' => false,
'msg' => 'Exception occured',
);
}
}
# if there is nothing to report
if(!$report)
return true;
# output
$output = '';
foreach($report as $row)
{
$link = $this->context->link->getProductLink($row['id_product']);
$output .= $row['id_product'] .' - '. $row['name'] .' '. ($row['res'] ? 'Ok!' : 'Error!') .' : '. $row['msg'] ."\r\n";
}
# send mail
$tpl_vars = array(
'{report_txt}' => strip_tags($output),
'{report_html}' => nl2br($output),
);
Mail::Send(
$this->context->language->id,
'report',
'Price update report',
$tpl_vars,
PRODUCT_COMPARE_EMAIL,
'',
Configuration::get('PS_SHOP_EMAIL'),
Configuration::get('PS_SHOP_NAME'),
null,
null,
dirname(__FILE__).'/mails/',
null,
$this->context->shop->id
);
return true;
}
public function compareTest($id_product)
{
$report = $this->_compareProduct($id_product);
var_dump($report);
die;
}
protected function _compareProduct($id_product)
{
# load product
$product = new Product($id_product, false, $this->context->language->id, $this->context->shop->id);
if(!Validate::isLoadedObject($product))
return false;
if(!$product->compare_link)
return false;
$oldPrice = (float) $product->price;
//******
# request
$data = file_get_contents($product->compare_link);
# analize price
if(!preg_match('!data-free-data=\'([^\']+)\'!i', $data, $matches))
{
return array(
'res' => false,
'msg' => 'Price not found!',
);
}
//******
$prices = json_decode($matches[1], true);
$newPrice = (float) $prices['minPrice'];
if(!$newPrice)
{
return array(
'res' => false,
'msg' => 'Zero price!',
);
}
# check change
$changed = $oldPrice != $newPrice;
if(!$changed)
{
return array(
'res' => false,
'msg' => 'The price is the same.',
);
}
$newPrice += (float) $product->compare_price;
# check change
$changed = $oldPrice != $newPrice;
if(!$changed)
{
return array(
'res' => false,
'msg' => 'The price is the same.',
);
}
# update
$product->price = $newPrice;
if(!$product->save())
{
return array(
'res' => false,
'msg' => 'Not saved!',
);
}
return array(
'res' => true,
'changed' => $changed,
'msg' => 'Updated! From: '.round($oldPrice, 2).' To: '.round($newPrice, 2),
);
}
}
There are needed function
protected function _compareProduct($id_product)
{
# load product
$product = new Product($id_product, false, $this->context->language->id, $this->context->shop->id);
if(!Validate::isLoadedObject($product))
return false;
if(!$product->compare_link)
return false;
$oldPrice = (float) $product->price;
//******
# request
$data = file_get_contents($product->compare_link);
# analize price
if(!preg_match('!data-free-data=\'([^\']+)\'!i', $data, $matches))
{
return array(
'res' => false,
'msg' => 'Price not found!',
);
}
//******
$prices = json_decode($matches[1], true);
$newPrice = (float) $prices['minPrice'];
if(!$newPrice)
{
return array(
'res' => false,
'msg' => 'Zero price!',
);
}
# check change
$changed = $oldPrice != $newPrice;
if(!$changed)
{
return array(
'res' => false,
'msg' => 'The price is the same.',
);
}
$newPrice += (float) $product->compare_price;
# check change
$changed = $oldPrice != $newPrice;
if(!$changed)
{
return array(
'res' => false,
'msg' => 'The price is the same.',
);
}
# update
$product->price = $newPrice;
if(!$product->save())
{
return array(
'res' => false,
'msg' => 'Not saved!',
);
}
return array(
'res' => true,
'changed' => $changed,
'msg' => 'Updated! From: '.round($oldPrice, 2).' To: '.round($newPrice, 2),
);
}

WooCommerce: Check if coupon is valid

I am trying to check if a coupon is still valid (hasn't reached its usage limit) and display content under this condition.
The reason for this is that I want to be able to hand out a coupon code to particular visitors, but obviously don't want to hand out a coupon that has already reached it's usage limit.
I am trying to achieve this with PHP and imagine the code to be something like this:
<?php if (coupon('mycouponcode') isvalid) {
echo "Coupon Valid"
} else {
echo "Coupon Usage Limit Reached"
} ?>
Any help here would be great :)
I believe the recommended way encouraged by WooCommerce is to use the \WC_Discounts Class. Here is an example:
function is_coupon_valid( $coupon_code ) {
$coupon = new \WC_Coupon( $coupon_code );
$discounts = new \WC_Discounts( WC()->cart );
$response = $discounts->is_coupon_valid( $coupon );
return is_wp_error( $response ) ? false : true;
}
Note: It's also possible to initialize the WC_Discounts class with a WC_Order object as well.
It's important to remember doing that inside the wp_loaded hook at least, like this:
add_action( 'wp_loaded', function(){
is_coupon_valid('my-coupon-code');
});
There is an apparent simpler method is_valid() from the WC_Coupon class but it was deprecated in favor of WC_Discounts->is_coupon_valid()
$coupon = new WC_Coupon( 'my-coupon-code' );
$coupon->is_valid();
$code = 'test123';
$coupon = new WC_Coupon($code);
$coupon_post = get_post($coupon->id);
$coupon_data = array(
'id' => $coupon->id,
'code' => $coupon->code,
'type' => $coupon->type,
'created_at' => $coupon_post->post_date_gmt,
'updated_at' => $coupon_post->post_modified_gmt,
'amount' => wc_format_decimal($coupon->coupon_amount, 2),
'individual_use' => ( 'yes' === $coupon->individual_use ),
'product_ids' => array_map('absint', (array) $coupon->product_ids),
'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids),
'usage_limit' => (!empty($coupon->usage_limit) ) ? $coupon->usage_limit : null,
'usage_count' => (int) $coupon->usage_count,
'expiry_date' => (!empty($coupon->expiry_date) ) ? date('Y-m-d', $coupon->expiry_date) : null,
'enable_free_shipping' => $coupon->enable_free_shipping(),
'product_category_ids' => array_map('absint', (array) $coupon->product_categories),
'exclude_product_category_ids' => array_map('absint', (array) $coupon->exclude_product_categories),
'exclude_sale_items' => $coupon->exclude_sale_items(),
'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2),
'maximum_amount' => wc_format_decimal($coupon->maximum_amount, 2),
'customer_emails' => $coupon->customer_email,
'description' => $coupon_post->post_excerpt,
);
$usage_left = $coupon_data['usage_limit'] - $coupon_data['usage_count'];
if ($usage_left > 0) {
echo 'Coupon Valid';
}
else {
echo 'Coupon Usage Limit Reached';
}
The code is tested and fully functional.
Reference
WC_API_Coupons::get_coupon( $id, $fields )
add_action( 'rest_api_init', 'coupon' );
function coupon($request) {
register_rest_route('custom-plugin', '/coupon_code/',
array(
'methods' => 'POST',
'callback' => 'coupon_code',
)
);
}
function coupon_code($request)
{
$code = $request->get_param('coupon');
$applied_coupon = $request->get_param('applied_coupon');
$cart_amount = $request->get_param('cart_amount');
$user_id = $request->get_param('user_id');
// $code = 'test123';
$coupon = new WC_Coupon($code);
$coupon_post = get_post($coupon->id);
$coupon_data = array(
'id' => $coupon->id,
'code' => esc_attr($coupon_post->post_title),
'type' => $coupon->type,
'created_at' => $coupon_post->post_date_gmt,
'updated_at' => $coupon_post->post_modified_gmt,
'amount' => wc_format_decimal($coupon->coupon_amount, 2),
'individual_use' => ( 'yes' === $coupon->individual_use ),
'product_ids' => array_map('absint', (array) $coupon->product_ids),
'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids),
'usage_limit' => (!empty($coupon->usage_limit) ) ? $coupon->usage_limit : null,
'usage_count' => (int) $coupon->usage_count,
'expiry_date' => (!empty($coupon->expiry_date) ) ? date('Y-m-d', $coupon->expiry_date) : null,
'enable_free_shipping' => $coupon->enable_free_shipping(),
'product_category_ids' => array_map('absint', (array) $coupon->product_categories),
'exclude_product_category_ids' => array_map('absint', (array) $coupon->exclude_product_categories),
'exclude_sale_items' => $coupon->exclude_sale_items(),
'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2),
'maximum_amount' => wc_format_decimal($coupon->maximum_amount, 2),
'customer_emails' => $coupon->customer_email,
'description' => $coupon_post->post_excerpt,
);
if ($coupon_data['code'] != $code) {
$response['status'] = "failed";
$response['message'] = "COUPON ".$code." DOES NOT EXIST!";
return new WP_REST_Response($response);
}
$Acoupon = new WC_Coupon($applied_coupon);
// print_r($Acoupon);exit();
if($Acoupon->individual_use == 'yes'){
$response['status'] = "failed";
$response['message'] = "SORRY, COUPON ".$applied_coupon." HAS ALREADY BEEN APPLIED AND CANNOT BE USED IN CONJUNCTION WITH OTHER COUPONS.";
$response['result'] = $Acoupon->individual_use;
return new WP_REST_Response($response);
}
if ($coupon_data['minimum_amount'] > $cart_amount) {
$response['status'] = "failed";
$response['message'] = "THE MINIMUM SPEND FOR THIS COUPON IS $".$coupon_data['minimum_amount'].".";
return new WP_REST_Response($response);
}
if ($coupon_data['maximum_amount'] < $cart_amount) {
$response['status'] = "failed";
$response['message'] = "THE MAXIMUM SPEND FOR THIS COUPON IS $".$coupon_data['maximum_amount'].".";
return new WP_REST_Response($response);
}
if ($coupon_data['exclude_sale_items'] == true) {
$response['status'] = "failed";
$response['message'] = "SORRY, THIS COUPON IS NOT VALID FOR SALE ITEMS.";
return new WP_REST_Response($response);
}
$usage_left = $coupon_data['usage_limit'] - $coupon_data['usage_count'];
if ($usage_left == 0) {
$response['status'] = "failed";
$response['message'] = "SORRY, COUPON USAGE LIMIT HAS BEEN REACHED.";
return new WP_REST_Response($response);
}
$current_time = date('Y-m-d');
$coupon_Expiry_date = $coupon->expiry_date;
if ($coupon_Expiry_date < $current_time) {
$response['status'] = "failed";
$response['message'] = "THIS COUPON HAS EXPIRED.";
return new WP_REST_Response($response);
}
$user_limite = get_post_meta($coupon_data['id'], 'usage_limit_per_user', true);
$p_id = $coupon_data['id'];
global $wpdb;
$count_of_per_user = $wpdb->get_results("SELECT * FROM wp_postmeta where post_id = $p_id and meta_key = '_used_by' and meta_value = $user_id");
$count = count($count_of_per_user);
if ( $count >= $user_limite) {
$response['status'] = "failed"`enter code here`;
$response['message'] = "COUPON USAGE LIMIT HAS BEEN REACHED.";
return new WP_REST_Response($response);
}enter code here
else{
$response['status'] = "Success";
$response['message'] = "COUPON APPLIED.";
return new WP_REST_Response($response);
}
}
use this function

Insert to db in an iterative way

I want to insert to my database payment table that should be done in this way.
The payment table that I have includes both group_id and member_id as a foreign key related to the table groups and member respectively.
What I want to do is once I hit the button "Pay" it should insert for each and every member_id as a row in the payment table.
Here is my code..
In my controller I have
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$this->load->model('Member_model');
$memberid = $this->Member_model->paymember();
foreach ($memberid->result_array() as $id){
$memberid[] = $id;
$data = array(
'group_id' => $this->input->post('group_id',TRUE),
'member_id' => $memberid,
'paid_by' => $this->input->post('paid_by',TRUE),
'from_date' => $this->input->post('from_date',TRUE),
'to_date' => $this->input->post('to_date',TRUE),
'amount' => $this->input->post('amount',TRUE),
'reference_number' => $this->input->post('reference_number',TRUE),
//'last_updated_by' => $this->input->post('last_updated_by',TRUE),
//'last_update_time' => $this->input->post('last_update_time',TRUE),
);
}
$this->Payment_model->insert($data);
$this->session->set_flashdata('message', 'Record Created Successfully!');
redirect(site_url('payment'));
}
}
And in my model which is the Member_model I've included in the controller..
function paymember(){
$data= array();
$this->db->where('group_id',$this->input->post('group_id'));
$memberid = $this->db->get('member');
if ($memberid->num_rows() > 0) {
foreach ($memberid->result_array() as $row){
$data[] = $row;
}
}
}
Please help me out. Thank you.
Now I've solved the problem.
In my controller there I have got the array value from the model...
public function create_action()
{
$this->_rules();
if ($this->form_validation->run() == FALSE) {
$this->create();
} else {
$this->load->model('Member_model');
$memberid = $this->Member_model->paymember();
if (count($memberid)) {
foreach ($memberid as $id) {
$data = array(
'group_id' => $this->input->post('group_id',TRUE),
'member_id' => $id,
'paid_by' => $this->input->post('paid_by',TRUE),
'from_date' => $this->input->post('from_date',TRUE),
'to_date' => $this->input->post('to_date',TRUE),
'amount' => $this->input->post('amount',TRUE),
'reference_number' => $this->input->post('reference_number',TRUE),
'last_updated_by' => $this->session->userdata('username'),
//'last_update_time' => $this->input->post('last_update_time',TRUE),
);
$this->Payment_model->insert($data);
echo $data;
}
}
$this->session->set_flashdata('message', 'Record Created Successfully!');
redirect(site_url('payment'));
}
}
Also on my previous model paymember method there was a problem which its not returning any result so I've fixed that with this code....
function paymember(){
$data= array();
$this->db->where('group_id',$this->input->post('group_id'));
$query = $this->db->get('member');
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row){
$data[] = $row['member_id'];
}
}
$query->free_result();
return $data;
}
Thank you for ur support!

Categories