implode function not working in codeigniter - arrays

I want to store a checkbox array value by using implode function.when i stored,the database table value as Array (not a integer value show only array)
This is my view file:
<div class="col-sm-12">
<div>
<label class="form-control-label">Select Asset Type</label><br>
<div class="border-checkbox-section">
<div class="border-checkbox-group border-checkbox-group-primary">
<?php $b=0; foreach($assettype as $assettype_info){ $b++;?>
<input type="checkbox" name="assettype_name[]" class="border-checkbox" value="<?php echo $assettype_info->assettype_name; ?>" id="checkbox<?php echo $b;?>">
<label class="border-checkbox-label" for="checkbox<?php echo $b;?>"><?php echo $assettype_info->assettype_name; ?></label>
<?php } ?>
</div>
</div>
</div>
My Controller:
public function insertassetassign()
{
$employee=$_POST['employee'];
$assettype_name = implode(", ", $_POST['assettype_name']) ;
$assign_date = date("Y-m-d",strtotime($_POST['assign_date']));
$_POST['assetassign_status']='1';
$joinon=date('Y-m-d');
$result = $this->insert->insertrecord('assetassign');
if($result)
{
redirect('assets/assetassignment', 'refresh');
}
}
My Model :
Public function insertrecord($Table)
{
$Inputs=$_POST;
$Inputs["joinon"]=date("Y-m-d");
$Keys=array();
$Values=array();
foreach($Inputs as $Inp_key=>$inp_value)
{
if($Inp_key!="submit" && $Inp_key!="PHPSESSID")
{
$Keys[]= $Inp_key;
$Values[]= "'".$inp_value."'";
}
}
$keys=implode(',',$Keys);
$values=implode(',',$Values);
$qry="insert into ".$Table."(".$keys.") values(".$values.")";
$ack=$this->db->query($qry);
if($ack) return true; else return false;
}
I expected to store a array value as id with comma
DB shows as :
Db shows as

modified in controller to get the answer.
public function insertassetassign()
{
$employee=$_POST['employee'];
$assettype_name = implode(",",$_POST['assettype_name']) ;
$_POST['assettype_name'] = $assettype_name;
$assign_date = date("Y-m-d",strtotime($_POST['assign_date']));
$_POST['assetassign_status']='1';
$joinon=date('Y-m-d');
$result = $this->insert->insertrecord('assetassign');
if($result)
{
redirect('assets/assetassignment', 'refresh');
}
}

Related

How to structure a multi array to process and sort WooCommerce Order Item data?

How can I extract details from an order into an array, while on the "Order Edit" page and then loop through them?
My array structure isn't correct as can't handle a 3rd item-detail. So each order item somehow needs to be placed into its own sub-array.
Then I want to be able to sort them on item-name and print them out with an echo.
How to achieve the correct array structure to input them and extract?
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'additional_admin_order_data_block_after_shipping_address', 100 );
function additional_admin_order_data_block_after_shipping_address(){
echo '</div><div class="order_data_column order_packing_column">
<h3>' . esc_html__( 'Packing Items', 'woocommerce' ) . '</h3>';
// here goes your code and content
//custom array to list products:
$products_array = array();
$product_tally = 0;
global $post; // <=== Alway at the beginning
// Get an instance of the WC_Order Object
$order = wc_get_order( $post->ID );
// let's decide what order item types we would like to get
$types = array( 'line_item');
// defaults to line_item which allows to get products from order
foreach( $order->get_items( $types ) as $item_id => $item ) {
// order ID
$item_order_id = $item->get_order_id();
// product only and no bundle 'summary' item
if( empty( $item['woosb_ids']) && $item->is_type( 'line_item' ) ) {
// order item name (product title, name of a shipping method or coupon code)
$item_name = $item->get_name();
if ( array_key_exists($item_name, $products_array)) {
$products_array[$item_name] += $item['qty'];
} else {
$products_array[$item_name] = $item['qty'];
}
$product_tally = $product_tally + $item->get_quantity();
}
}
arsort($products_array);
echo '<div class="woocommerce_order_items" id="packing-table">
<div class="thead">
<div class="tr">
<div class="item sortable">Product</div>
<div class="quantity sortable">Qty</div>
</div>
</div>
<div class="tbody" id="order_line_items">';
//display list:
foreach ($products_array as $title => $quantity) {
echo '<div class="lineitem">
<div class="item">';
echo $title.'</div>
<div class="quantity">'.$quantity.'</div>
</div>';
}
echo '<div class="lineitem tally">
<div class="item">Tally of Items</div>
<div class="quantity">'.$product_tally.'</div>
</div>';
echo '</div>
</div>';
}

Laravel: Updating multiple files in array

I was able to store and save multiple files in the array into the database, as well as updating the form data. However, the problem now is it returns this error when I update the form without updating the file.
count(): Parameter must be an array or an object that implements Countable
Does anyone know what should I do for my update function or value in my blade file so that it won't delete the file elements?
--------------------------------------------- UPDATED ---------------------------------------------
public function update(Request $request, $id)
{
$data = $request->all();
$data = $this->saveEmptyData($data);
$c = Document::find($id);
$fileData = [];
if(count($request->file)>0)
{
foreach($request->file as $file)
{
$path = public_path('storage/documents');
$fileName = time().'_'.$file->getClientOriginalName();
$file->move($path, $fileName);
if ($c->file)
{
File::delete($path.'/'.json_decode($c->file)[0]);
}
$fileData[] = $fileName;
}
$doc = json_encode($fileData);
$c->file = $doc;
$c->name = $request->name;
$c->price = $request->price;
$c->publish = $request->publish;
}
//else {
// redirect()->back()->with('errors', 'File is required!');
//}
$c->save();
return redirect()->route('product.index')->with('success', 'Document updated successfully');
}
Blade
<form method="POST" action="{{ route('document.update', $doc) }}" enctype="multipart/form-data">
<fieldset>
....
<div class="form-group">
<label>Document</label>
<div class="input-group-append">
<label for="upload_file">Upload</label>
<input id="upload_file" type="file" name="file[]" value="{{ isset($doc) ? $doc['file'] : '' }}" multiple>
</div>
</div>
</fieldset>
</form>
if(count($request->file)>0)
{
foreach($request->file as $file)
{
$path = public_path('documents/');
$fileName = time().'_'.$file->getClientOriginalName();
$file->move($path, $fileName);
if ($c->file)
{
File::delete($path.'/'.json_decode($c->file)[0]);
}
$fileData[] = $fileName;
}
$doc= json_encode($fileData); //replace $doc['file'] with $doc
$c->file=$doc;
}
$c->save();
//not required to declare $fileData ;
enctype="multipart/form-data" in form tag
if(count($request->file)>0)
{
foreach($request->file as $file)
{
$path = public_path('documents/');
$fileName = time().'_'.$file->getClientOriginalName();
$file->move($path, $fileName);
if ($c->file)
{
File::delete($path.'/'.json_decode($c->file)[0]);
}
$fileData[] = $fileName;
}
$doc['file'] = json_encode($fileData);
}

Array to String Conversion error in CodeIgniter?

This is the code
My Controller file
public function view_resume($id)
{
$this->load->model('ORB_Model');
$data['get_masterData'] = $this->ORB_Model->get_masterData($id);
}
My Model file:
public function get_educationData($id)
{
return $this->db->get_where('edu_tb',['edu_id'=>$id],2)->result_array();
}
My View File:
<?php
print_r($get_educationData);
die;
// output array()
foreach ($get_educationData as $key){
?>
value="<?php echo $key->['edu_name']; ?>"
<?php
}
?>
Now when i print_r my $get_educationData it will give empty array() only.
my data is not coming from model.
in my view file nothing shown there.
Your controller should be like this :
You have to call get_educationData method also in your controller like this :
public function view_resume($id)
{
$this->load->model('ORB_Model');
$data['get_masterData'] = $this->ORB_Model->get_masterData($id);
$data['get_educationData'] = $this->ORB_Model->get_educationData($id);
/* load view here like this */
$this->load->view('your_view' ,$data);
}
Your model get_educationData should be like this :
public function get_educationData($id)
{
if ( ! empty($id))
{
$query = $this->db->get_where('edu_tb', array('edu_id' => $id));
}
else
{
$query = $this->db->get('edu_tb');
}
if ($query->num_rows() > 0)
{
return $query->result_array();
}
}
Your view should be like this :
Instead of object notation use array since your return data is in array form
<?php
//print_r($get_masterData);
//print_r($get_educationData);
if ( ! empty($get_educationData))
{
foreach ($get_educationData as $item)
{
echo $item['edu_name'];
}
}
?>

CodeIgniter Dropdown menu fetching data from database column

I am having a problem. Please let me know if there is a solution. I am new to codeigniter therefore, sorry in advance if there is a silly one!
I am trying to fetch data from a database. Table name is fw_main_cat which have fields (cat_id, cat_parent_id, cat_level, cat_title, cat_menu_order and cat_status). cat_id is unique.
I want a dropdown menu (in view) to take all the data of a column cat_title through cat_level (which are int). So, how can I do?
Here is my code which I have tried so far.
This is Model :
public function cat_level_one($cat_level)
{
$sql = "Select * from fw_main_cat Where cat_level=? ";
$result = $this->db->query($sql, $cat_level);
if($result->num_rows() > 0)
{
return $result->result_array();
}
else { return false;
}
}
This is Controller :
public function getcategory()
{
if ($this->session->userdata('session_status'))
{
$cat_level_one = $this->admin_cat_model->cat_level_one($cat_level);
$data['cat_level_one'] = $this->admin_cat_model->cat_level_one($cat_level);
$this->session->set_userdata('cat_level', $_POST['cat_level']);
$this->laod_view('admin_view/admin_cat/view_add_category', $data);
}else {
redirect ('admin_view/admin_cat/view_category');
}
}
This is View (dropdown menu):
<li class="full-row">
<select name = 'cat_level_one' id = 'cat_level_one'>
<option value="<?php if(isset ($cat_level_one) && $cat_level_one != ''){ foreach ($cat_level_one as $cat_one){ echo $cat_one->cat_id; } } ?>"
selected="selected">------------Select Category------------</option>
Thanks! for the consideration.
Updated code -
// in model
function cat_level_one($cat_level)
{
$this->db->where('cat_level',$cat_level);
$q = $this->db->get('fw_main_cat');
$data = $q->result_array();
return $data;
}
//controller function
public function getcategory()
{
**if ($this->session->userdata('session_status'))
{
$data['cat_level_one'] = $this->admin_cat_model->cat_level_one($cat_level);**
//Where this post data comes from??
$this->session->set_userdata('cat_level', $_POST['cat_level']);
$this->laod_view('admin_view/admin_cat/view_add_category', $data);
}else {
redirect ('admin_view/admin_cat/view_category');
}
}
//in view
<li class="full-row">
<select name = 'cat_level_one' id = 'cat_level_one'>
<option value="">-- Select Category --</option>
<?php foreach($cat_level_one as $cat_one){ ?>
<option value="<?php echo $cat_one->cat_id; ?>"><?php echo $cat_one->cat_title; ?></option>
<?php } ?>
</select>
</li>

CodeIgniter put 0 instead of string values to a database

I'm trying to build a small blog with CodeIgniter, and for some reason I put form database entries 0 instead of strings. I tried to put it through the controller manually and it works. What's the problem in a form?
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site_model extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_records()
{
$query = $this->db->get('posts');
return $query->result();
}
function add_records($data) {
$this->db->insert('posts', $data);
return;
}
function updata_records($data) {
$this->db->where('postID', $id);
$this->db->updata('posts', $data);
}
function delede_roe() {
$this->db->where('postID', $this->uri->segment(3));
$this->db->delete('posts');
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
the form page:
<div class="grid_11">
<h2>About</h2>
<?php echo form_open('site/creata'); ?>
<label for="title"> title </label>
<input type="text" name="title" id="title">
<label for="content"> content </label>
<input type="text" name="content" id="content">
<br>
<input type="submit" value="send">
<?php echo form_close(); ?>
</div>
the controler:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class site extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see http://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('home');
}
function add_post() {
$this->load->view('add_post');
}
function creata() {
$data = array (
'title' => $this->input->post('title'),
'post' => $this->input->post('content')
);
$this->load->model('site_model');
$this->site_model->add_records($data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
Your controller function should look something like this:
function creata() {
if( $this->input->post(null) ){ //enter if the form is submitted
$data = $this->input->post(null, true); // get all the data of the post
print_r( $data );die; // see the post data
$this->load->model('site_model');
$this->site_model->add_records($data);
$this->session->set_flashdata('msg', 'A msg to show to the user about the insertion');
redirect('site/creata', 'refresh');
}else{ //load the form
$this->load->view('home');
}
}
You form looks okay to me tho. You model may contain one extra line to view the query.
function add_records($data) {
$this->db->insert('posts', $data);
$this->db->last_query(); die; //check the query it's executing.
return;
}

Resources