i have problem with fetching data array in laravel inside controller - php

i have problem with fetching data after using ajax how can i fetch data and pass it to another array when i pass one field using ->value() its working but i want to pass multiple data . any help please !
code:
public function GetCoursId(Request $request)
{
/*
$idcours = $request->input('idcours');
$fcours = DB::table('cours')->where('id_cours', $idcours)->value('field_cours') ;
$idmod = DB::table('cours')->where('id_cours', $idcours)->value('id_module') ; */
$output = array(
'field_cours' => $fcours,
'id_module' => $idmod,
'idcours' => $idcours
);
echo json_encode($output); */
$idcours = $request->input('idcours');
$PerCours = DB::table('cours')->where('id_cours', $idcours)->get() ;
$output = array(
'field_cours' => $PerCours->field_cours,
'id_module' => $PerCours->id_module,
'idcours' => $idcours
);
echo json_encode($output);
}
}

From your code it seems you have only one item in your collection and you want to get data from it. So use first() instead of get()
$idcours = $request->input('idcours');
$PerCours = DB::table('cours')->where('id_cours', $idcours)->first() ;
$output = array(
'field_cours' => $PerCours->field_cours,
'id_module' => $PerCours->id_module,
'idcours' => $idcours
);
echo json_encode($output);
You will able to get data now like
console.log(data.field_cours);

You can use this code like this:
public function GetCoursId(Request $request)
{
$idcours = $request->input('idcours');
$PerCours = DB::table('cours')->where('id_cours', $idcours)->get() ;
foreach($PerCours as $PerCour) {
$field_cours[] = $PerCour['field_cours'];
$id_module[] = $PerCour['id_module'];
}
$output = array(
'field_cours' => $field_cours,
'id_module' => $id_module,
'idcours' => $idcours
);
echo json_encode($output);
}

its working when i pass echo json_encode($PerCours);
console.log(data['0'].field_cours) ;
there is a way to display directly :
console.log(data.field_cours) ;

Related

php codeigniter i want to redirect this function print_view and also pass the data

php codeigniter i want to redirect this function print_view and also pass the data $this->db->insert('invoice_main',$datas);$result = $this->db->insert_id();
Public function insert_invoice(){
$item_name = $_POST['item'];
$rate = $_POST['rate'];
$quantity = $_POST['quantity'];
$tax = $_POST['tax'];
$amount = $_POST['amount'];
$invoice_id = $this->input->post('invoice_id');
//$invoice_id++;
$datas = array(
'user_id' => $this->session->userdata('user_id'),
'invoice_id' => $this->input->post('invoice_id'),
'pt_opnum' => $this->input->post('pt_opnum'),
'pt_uhid' => $this->input->post('pt_uhid'),
'doc_name' => $this->input->post('doc_name'),
'status'=>1
);
$this->db->insert('invoice_main',$datas);
$result = $this->db->insert_id();
for ($i=0; $i <count($item_name) ; $i++) {
$data=array(
'invoice_id' =>$invoice_id,
'pt_name'=> $this->input->post('pt_opnum'),
'date'=>date('d-m-Y'),
'name'=>$item_name[$i],
'rate'=>$rate[$i],
'quantity'=>$quantity[$i],
'tax'=>$tax[$i],
'amount'=>$amount[$i],
'sub_total'=>$_POST['sub_total'],
'o_tax'=>$_POST['o_tax'],
'grand_total'=>$_POST['grand_total'],
'status'=>1
);
$this->db->insert('invoice_details',$data);
}
return($this->db->affected_rows()!=1)?false:true;
$this->print_view($result);
}
public function print_view($result){
redirect(base_url()."invoice/print_view/".$result);
}
You can pass the any data with flashdata.
$this->session->set_flashdata('something',$result);
On the other page you can access it
$something = $this->session->flashdata('something);
But on your case you don't have to use flash data, you can get the params from url and you can search on your DB.

data getting inserted twice in database - Codeigniter

So I've spent hours on trying to fix this one. every time I upload a file. all the data/id3 tags are being inserted twice on my database. before it was working properly now it has this bug and I badly need some help now.
In my controller I am executing update at the same time since I want also to update the data inserted into database with the details from the id3 tags.
Controller
public function save()
{
$id = $this->session->userdata('user_id');
$status = $this->input->post('status');
$original_file = $this->input->post('original_file');
$revised_file = $this->input->post('revised_file');
$song_data = array(
'user_id' => $id,
'song_info_status' => $status,
'song_info_file_name' => $original_file,
'song_info_revised_file_name' => $revised_file,
'song_info_date_added' => 'now()',
'song_info_date_updated' => 'now()'
);
$song_info_id = $this->song_info_model->save($song_data);
$composer_data = array(
'composer_name' => ''
);
$composer_id = $this->composer_model->save($composer_data);
$song_composer_data = array(
'song_info_id' => $song_info_id,
'composer_id' => $composer_id
);
$this->song_composer_model->save($song_composer_data);
$status_data = array(
'song_info_id' => $song_info_id
);
$status_id = $this->status_model->save($status_data);
$tags_info = array();
$tags_info = $this->getTagsInfo(FCPATH ."temp" . "/" .$revised_file);
if(isset($tags_info['Author']))
{
// echo iconv("UTF-8","EUC-JP", $tags_info['Author']);
//echo $str = mb_convert_encoding($tags_info['Author'], "UTF-8", "auto");
$status_data = array(
'status_artist_name' => $tags_info['Author']
);
$this->status_model->update($status_data, $status_id);
}
if(isset($tags_info['Title']))
{
$song_data = array(
'song_info_original_title' => $tags_info['Title']
);
$this->song_info_model->update($song_data, $song_info_id);
}
rename($this->source.$revised_file, $this->destination.$revised_file);
}
In my model I am just getting the data I have from my controller. there's no loop or anything and I really can't find the culprit.
Model
public function save($data)
{
$this->db->insert('song_info', $data);
return $this->db->insert_id();
}
public function update($data, $id)
{
$this->db->where('song_info_id', $id);
$this->db->update('song_info', $data);
}

about laravel form calculation

I had encountered the calculation in Laravel or maybe PHP stuff, I would like to ask for solution or maybe some others approach to get the things done.
These are the codes in the Model
public function getRedeemCount($member_id){
$count = DB::table('stamps')->where('user_id', '=', $member_id)->select('stamp_counter')->first();
return $count;
}
public function getFreeOfferCount($member_id){
$free_offer_count = DB::table('stamps')->where('user_id', '=', $member_id)->select('free_offer_counter')->first();
return $free_offer_count;
}
These are the codes in the Controller
public function redeemFreeOffer(Request $request)
{
$free_offer_counter = $request->input('minus_free_offer_counter');
$member_id = $request->input('sub_id');
$total_stamp_count = $this->userModel->getRedeemCount($member_id);
$free_offer_count = $this->userModel->getFreeOfferCount($member_id);
if(!$free_offer_count){
$insert_arr = array(
'user_id' => $member_id,
'free_offer_counter' => $free_offer_counter,
'created_at'=>date("Y-m-d H:i:s")
);
$stamps = DB::table('stamps')->insert($insert_arr);
if($stamps){
$arr = array('status' => 1,'message'=>'success','value'=>$free_offer_counter);
}
}
else {
$total_stamp_count->stamp_counter;
$total_free_offer_count = floor($total_stamp_count->stamp_counter/10);
$current_offer_count = $free_offer_count->free_offer_counter - $free_offer_counter;
$update_arr = array(
'free_offer_counter' => $current_offer_count,
'updated_at' => date("Y-m-d H:i:s")
);
$stamps = DB::table('stamps')->where('user_id', $member_id)->update($update_arr);
if ($stamps) {
$arr = array('status' => 1, 'message' => 'success', 'value' => $current_offer_count);
}
}
echo json_encode($arr);
}
Since I am using jQuery AJAX, so there's too much code, but I think it should be enough information for now. If it is insufficient, I will provide more in future.
The problem is how do I inject $total_free_offer_count into $current_offer_count? I do not have any columns that stored $total_free_offer_count.
$total_stamp_count->stamp_counter;
$total_free_offer_count = floor($total_stamp_count->stamp_counter/10);
$current_offer_count = $free_offer_count->free_offer_counter - $free_offer_counter;

Not able to update mongo document with following code

Im using following code to update a mongo document. But its not working when I use $inc in order to increment a counter. It works well if no inc is used.
$arrWhere = array('epa_id' => $objData->request->memo->epa_id);
$arrSet = array(
'pa_response'=>$objData,
'response_status'=>'N',
'modified_datetime'=> getServerTimeStamp(),
array('$inc'=>array('revision_id'=>1)),
);
$arrResult = $objPriorAuth->updatePA($arrWhere, $arrSet);
public function updatePA($arrWhere,$arrSet)
{
global $medDB;
$strCollection = EPA_MASTER;
$dbCollection = $medDB->$strCollection;
$arrReturn = array();
//dump($arrSet);
try
{
$dbCollection->update(
$arrWhere,
array('$set' =>$arrSet),
array('multiple' => true)
);
dump($medDB->lastError());
}
catch(MongoCursorException $mce)
{
$arrReturn['error'] = $mce->getMessage();
logError($mce->getMessage(),DB_ERROR_LOG_FILE_PATH);
}
return $arrReturn;
}
It's wrong syntax, you can't use inc inside set. Try out the following code:
Change options:
$arrSet = array(
'pa_response'=>$objData,
'response_status'=>'N',
'modified_datetime'=> getServerTimeStamp()
);
$arrInc = array('$inc'=>array('revision_id'=>1));
Change update query:
array('$set' => $arrSet, '$inc' => $arrInc);

jQuery Auto Complete Return Data - Showing the ID aswell as label

I am using Codeigniter to create an Autocomplete for user names:
Using the parameter : search-user?term=s I get the following data back as Json :
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"}
However when I am running the auto select, On the search for say "St" it brings back by name in the options, But also the ID as well.
How do I prevent this from happening? I suspect my loop could be the issue.
My PHP Function is as follows :
function search_user()
{
$term = $this->input->get('term');
$user_search = $this->vendor_model->search_user($term);
$user['response'] = 'false';
if(count($user_search) > 0)
{
$user['response'] = 'true';
$user = array();
foreach($user_search as $user)
{
$user = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
echo json_encode($user);
}
}
}
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"} isn't valid JSON.
Try not echoing each $user's information separately - instead, build a new array of users and json_encode() that array. Example:
foreach($user_search as $user) {
$users[] = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
}
echo json_encode($users);

Categories