I have made a symfony form and I used:
$conn->insert('Invoiceshasitems', array('Invoiceitemsid' => '$items'));
for data insertion but it not works ho i do insert data from symfony controller.
here is my code:
$itemscounter = $request->request->get('itemscounter');
if(isset($_SESSION['invoiceid'])) {
$invoiceid=$_SESSION['invoiceid'];
//echo $invoiceid;
//exit;
}
//$entity= new Invoiceshasitems();
if($request->isXmlHttpRequest()) {
if($itemscounter > 1){
for($i=1; $i<=$itemscounter; $i++){
if($i==1){
$items = $_POST['items'];
}else {
$items.$i = $_POST['items'.$i];
}
}
}else{
$items = $_POST['items'];
$conn->insert('Invoiceshasitems', array('Invoiceitemsid' => '$items'));
}
}
You can use native query in Doctrine, there is some docs here: http://docs.doctrine-project.org/en/latest/reference/native-sql.html
For your example the code is something like that:
$rsm = new ResultSetMapping();
$query = $this->_em->createNativeQuery('INSERT INTO Invoiceshasitems SET Invoiceitemsid = ?', $rsm);
$query->setParameter(1, $items);
$result = $query->getResult();
Related
Im trying get a list of userIds from the videos table. This table contains the media files that get uploaded the users. This is my code
$this->db->select("videos.user_id as userId");
$this->db->limit('10', $document['offset']);
$this->db->group_by('userId');
$this->db->order_by('id','desc');
$recentUploads = $this->db->get('videos')->result_array();
if (!empty($recentUploads))
{
foreach ($recentUploads as $record)
{
$totalPostMedia = $obj->totalPostMedia($record);
$record['totalPostMedia'] = $totalPostMedia;
$resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
$record['uploadBy'] = $resData['username'];
$record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];
$Mydata[] = $record;
}
}
The result get is missing some of the userIds from the table. I have tried $this->db->distinct() as well. Still got the same result. The only way i get a result with no duplicates is when i remove $this->db->order_by('id','desc'); or make it asc instead of desc. But i want to get the latest records from the table. how do i do this? Am i doing something wrong? any help would be much appreciated.
try this
$this->db->select("videos.user_id as userId");
$this->db->from("videos");
$this->db->group_by('userId');
$this->db->order_by('id','desc');
$this->db->limit('10', $document['offset']);
$recentUploads = $this->db->get()->result_array();
if (count($recentUploads)>0)
{
foreach ($recentUploads as $record)
{
$totalPostMedia = $obj->totalPostMedia($record);
$record['totalPostMedia'] = $totalPostMedia;
$resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
$record['uploadBy'] = $resData['username'];
$record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];
$Mydata[] = $record;
}
}
in your request (i mean select videos.user_id as userId) in the group_by ligne you make userId to do the group by traitement. your userId alias is not knowing as colum name that can do any traitement of it.
for that replace your userId by videos.user_id in your group by ligne.
your code will be like this to work for you
$this->db->select("videos.user_id as userId");
$this->db->limit('10', $document['offset']);
$this->db->group_by('videos.user_id');
$this->db->order_by('id','desc');
$recentUploads = $this->db->get('videos')->result_array();
if (!empty($recentUploads))
{
foreach ($recentUploads as $record)
{
$totalPostMedia = $obj->totalPostMedia($record);
$record['totalPostMedia'] = $totalPostMedia;
$resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
$record['uploadBy'] = $resData['username'];
$record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];
$Mydata[] = $record;
}
}
use max(id)
as in $this->db->select("videos.user_id as userId,max(id)");`
so you might try:
$this->db->select("videos.user_id as userId, max(id)");
$this->db->limit('10', $document['offset']);
$this->db->group_by('userId');
$this->db->order_by('id','desc');
$recentUploads = $this->db->get('videos')->result_array();
if (!empty($recentUploads))
{
foreach ($recentUploads as $record)
{
$totalPostMedia = $obj->totalPostMedia($record);
$record['totalPostMedia'] = $totalPostMedia;
$resData = $this->db->select("username, profileImage")->from('users')->where('id', $record['userId'])->get()->row_array();
$record['uploadBy'] = $resData['username'];
$record['profileImage'] = "http:...com/profileImage/".$resData['profileImage'];
$Mydata[] = $record;
}
}
https://stackoverflow.com/a/14770936/1815624
In foreach loop i am getting warning Invalid argument supplied for foreach() below is my loop code and modal code please let me know where i done wrong
Controller
$concond = array("con_id"=>1,"con_status"=>1);
$this->data['contact']=$this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome',$this->data);
foreach Loop
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot->con_addr_line_1;
$footadr2 = $foot->con_addr_line_2;
$footcity = $foot->con_city;
$footstate = $foot->con_state;
$footcountry = $foot->con_country;
$footpin = $foot->con_pincode;
$footp1 = $foot->con_phone_1;
$footp2 = $foot->con_phone_2;
$footp3 = $foot->con_phone_3;
$footp4 = $foot->con_phone_4;
$footemail = $foot->con_email_id;
}
}
modal
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result();
}
Seems like I got your problem!
You should try to do it like this:
$data['contact'] = $this->Frontend_model->get_contact($concond);
$this->load->view('frontend/clienthome', $data);
Try in model
function get_contact($con_id, $con_stat)
{
$this->db->where('con_id', $con_id);
$this->db->where('con_status', $con_stat);
$query = $this->db->get('is_addres_contact');
return $query->result();
}
Controller
$con_id = '1';
$con_stat = '1';
$this->data['contact'] = $this->frontend_model->get_contact($con_id, $con_stat);
$this->load->view('frontend/clienthome',$this->data);
Can you just try this,
In model,
function get_contact($concond)
{
$this->db->select('*');
$this->db->from('is_addres_contact');
$this->db->where($concond);
$query = $this->db->get();
return $query->result_array();
}
In View
if($contact)
{
foreach($contact as $foot)
{
$footadr1 = $foot['con_addr_line_1'];
$footadr2 = $foot['con_addr_line_2'];
$footcity =$foot['con_city'];
$footstate = $foot['con_state'];
$footcountry = $foot['con_country'];
$footpin = $foot['con_pincode'];
$footp1 = $foot['con_phone_1'];
$footp2 = $foot['con_phone_2'];
$footp3 = $foot['con_phone_3'];
$footp4 = $foot['con_phone_4'];
$footemail = $foot['con_email_id'];
}
}
How to implode and insert values into the database in CodeIgniter?
I am creating multiple choice quiz script using CodeIgniter framework. I want to store user results like this:
id userid q_id answer_id time_taken
1 1 1,2,3,4,5 2,3,4,5,3 4,5,7,6,7
in my controller:
public function insert_result()
{
$this->load->model('quiz_models');
$user_id=$this->input->post('user_id');
$qq_id=$this->input->post('questionid');
$answer_id=$this->input->post('AnswerID');
$time_taken=$this->input->post('timetaken');
$question_no=$this->input->post('question_no');
$bd = "$question_no";
switch ($bd) {
case"1":
$data=array('user_id'=>$user_id,
'q_id'=>$qq_id,
'answer_id'=>$answer_id,
'time_taken'=>$time_taken);
$this->quiz_models->insert_result($data);
break;
case"2":
quiz_test();
break;
case"3":
quiz_test();
break;
case"4":
quiz_test();
break;
case"5":
quiz_test();
$this->session->unset_userdata('lastids');
break;
default:
echo "something is wrong";
}
}
public function quiz_test()
{
$this->load->model('quiz_models');
$quiz=$this->quiz_models->quiz_test();
foreach($quiz as $row){
$qid=$row->q_id;
$ans=$row->answer_id;
$time=$row->time_taken;
$a = array("$qq_id","$qid");
$b = array("$answer_id","$ans");
$c = array("$time_taken","$time");
$comma = implode(",",$a);
$comma1 = implode(",",$b);
$comma2 = implode(",",$c);
$data=array('q_id'=>$comma,
'answer_id'=>$comma1,
'time_taken'=>$comma2);
$this->quiz_model->update_result($data);
}
}
}
and Model:
function insert_result($data)
{
$this->dbb->insert('results',$data);
$sectio=$this->db->insert_id();
$this->session->set_userdata('lastids',$sectio);
}
function quiz_test()
{
$ses_id = $this->session->userdata('lastids');
$sql = "SELECT q_id, answer_id, time_taken FROM results WHERE id='$ses_id'";
$query = $this->dbb->query($sql);
$result = $query->result();
return $result;
}
function update_result($data){
$ses_id = $this->session->userdata('lastids');
$this->db->where('id',$ses_id);
$this->db->update('results',$data);
}
when i run it nothing happened,not showing any error where do i mistake?
pls help me what am i doing wrong
First of all - i think you've a major problem in your DB structure
Normalize your Data
You should prevent to store your information in the table like that.
It should be possible to normalize your data properly. If you dont know
how to do that the following link could be interesting:
Normalization in MYSQL
However a possible solution would be to structure your data:
In order to do that - create a save Method in your Model to split between update and insert - this model could look like
class Quiz_Models
{
private $arrPostData;
public function save($arrPostData = false)
{
$this->arrPostData = (!$arrPostData) ? $this->input->post() : $arrPostData;
$id = $this->session->userdata("lastids");
if ($id)
{
$query = $this->db
->select("*")
->from("results")
->where("id",$id)
->get();
if ($query->num_rows() == 1)
{
$this->update($query->row(0));
}
else return false;
}
else
{
$this->insert();
}
if ($this->arrPostData['question_no'] == 10) $this->session->unset_userdata("lastids");
}
private function update($objData)
{
$objCollection = new Quiz_Collection();
$objCollection->userId = $objData->userid;
$objCollection->id = $objData->id;
$arrData = explode($objData->q_id);
foreach($arrData AS $key => $quizId)
{
$objQuiz = new stdClass();
$objQuiz->q_id = $quizId;
$objQuiz->answer_id = explode($objData->answer_id)[$key];
$objQuiz->time_taken = explode($objData->answer_id)[$key];
$objCollection->append($objQuiz);
}
$objQuizFromPost = new stdClass();
$objQuizFromPost->q_id = $this->arrPostData["questionid"];
$objQuizFromPost->answer_id = $this->arrPostData['AnswerID'];
$objQuizFromPost->time_taken = $this->arrPostData['timetaken'];
$objCollection->addQuizFromPost($objQuizFromPost);
$this->db
->where("id",$objCollection->id)
->update("results",$objCollection->getDbData());
}
private function insert()
{
$objCollection = new Quiz_Collection();
$objCollection->userId = $this->arrPostData['user_id'];
$objQuizFromPost = new stdClass();
$objQuizFromPost->q_id = $this->arrPostData["questionid"];
$objQuizFromPost->answer_id = $this->arrPostData['AnswerID'];
$objQuizFromPost->time_taken = $this->arrPostData['timetaken'];
$objCollection->addQuizFromPost($objQuizFromPost);
$this->db->insert("results",$objCollection->getDbData());
$this->session->set_userdata("lastids", $this->db->insert_id());
}
}
as an addition you need a Collection Object (put this below your model)
class Quiz_Collection extends Array_Object
{
public $userId = 0;
public $id = 0;
public function addQuizFromPost($objQuiz)
{
if (intval($objQuiz->q_id) > 0)
{
foreach($this AS $key => $obj)
{
if ($obj->q_id == $objQuiz->q_id)
{
$this->offsetSet($key, $objQuiz);
return true;
}
}
$this->append($objQuiz);
return true;
}
return false;
}
public function sortQuizData($objA, $objB)
{
if ($objA->q_id == $objB->q_id) return 0;
return ($objA->q_id < $objB->q_id) ? -1 : 1;
}
public function getDbData()
{
$this->uasort(array($this,"sortQuizData"));
$arrData = $this->getArrayCopy();
$arrDbData = [
"userid" => $this->userId,
"q_id" => implode(array_map(function($obj){ return $obj->q_id;},$arrData),","),
"answer_id" => implode(array_map(function($obj){ return $obj->answer_id;},$arrData),","),
"time_taken" => implode(array_map(function($obj){ return $obj->time_taken;},$arrData),","),
];
return $arrDbData;
}
}
pS: this is just an instruction how you can do that in a proper way. Pls study this code. If you still don't understand whats going on, feel free to ask.
Simple Questions, if you Phalcon users .. you know what i want trying to do.
$trueFind = ProductOrderTransaction::find(["conditions"=>"protPthdId = ".$id]);
$trueFind->setTransaction($transaction);
$trueFind->protMomsId = $monitId;
$trueFind->protMomsName = $monitName;
if (!$trueFind->update()) {
foreach ($trueFind->getMessages() as $message) {
$this->flash->error($message);
$transaction->rollback($message->getMessage());
}
}
I just want to do this query in orm Phalcon :
UPDATE product_order_transaction set protMomsId = '$monitId' , protMomsName = '$monitName' WHERE protPthdId='$id'
fail -> rollback.. success -> commit.
Something like this?
$items = ProductOrderTransaction::find([
'conditions' => 'protPthdId = :id:',
'bind' => ['id' => $id]
]);
foreach($items as $item){
$this->db->begin();
$item->protMomsId = $monitId;
$item->protMomsName = $monitName;
$update = $item->update();
if(!$update){
$this->db->rollback();
continue;
}
$this->db->commit();
}
In my Codeigniter project ,table value is not retrieving from database.Am using MySQL (WAMP) as database.Using Select Query i have checked the data in database and its fine there.When updating the same also its retrieving the old value in db.But when retrieving the value in later stage (ie,taking old bill) its not retrieving the value.The problem is happening only on the single field(ie,actual_price).How to solve this error.Here am attaching the screenshot and controller code for the same.
Controller Code
function bill_view($billid)
{
if(!$billid) {
redirect('report/bill_report');
}
$salecode =str_replace("_","/",$billid);
$filter ="gm_sale.saleCode ='$salecode'";
$billArray =$this->sale_model->getBillinfo($filter);
$exshowroom='';
$bank ='';
$scheme='';
$wcoNo ='';
$saleId =0;
foreach($billArray as $key=>$val) {
$exshowroom = $val['actual_price'];
$date =$val['saledate'];
$sale_to=$val['saleCustomer'];
$saleUserId=$val['saleUserId'];
$wcoNo = $val['wcoNo'];
$saleId= $val['saleId'];
if(!is_null($val['bank']) && !empty($val['bank'])){
$bank =$val['bank'];
}
if(!is_null($val['scheme_id']) && !empty($val['scheme_id'])){
$array_scheme = unserialize($val['scheme_id']);
///////////////////////////////////////////
foreach ($array_scheme as $val_scheme_id) {
$res_scheme = $this->db->get_where("gm_scheme",array('id'=>(int)$val_scheme_id));
if($res_scheme->num_rows >0){
$arrscheme = $res_scheme->row_array();
if(!empty($scheme)) {
$scheme .= ",";
}
$scheme .= $arrscheme['schemeName'];
}
}
/////////////////////////////////////////////
}
break;
}
$query = $this->db->get_where('gm_users',array('userId'=>(int)$saleUserId));
if($query->num_rows >0) {
$arrUser =$query->row_array();
}else{
$arrUser =array();
}
$data['list_product'] = $billArray;
$data['exshowroom']=$exshowroom;
$data['userinfo'] =$arrUser;
$data['saleCode'] =$salecode;
$data['sale_to'] =$sale_to;
$data['added_date'] =$date;
$data['bank'] =$bank;
$data['scheme'] =$scheme;
$data['wcoNo'] =$wcoNo;
$data['saleId'] =$saleId;
$this->load->view('header_login');
$this->load->view('report/bill_view',$data);
//print_r($billArray);
$this->load->view('footer_login');
}
Model Code
function getBillinfo($filter=''){
$this->db->select('*,gm_sale.added_date as saledate');
$this->db->from('gm_sale',FALSE);
$this->db->join('gm_products',"gm_sale.productId=gm_products.productId",FALSE);
$this->db->join('gm_model',"gm_products.model_id=gm_model.id",FALSE);
$this->db->join('gm_banks',"gm_sale.bank_id=gm_banks.bank_id","LEFT");
if($filter<>"")
$this->db->where($filter,'',FALSE);
$this->db->order_by('gm_sale.saleId',"desc");
$query = $this->db->get();
print_r($query);
if($query->num_rows>0) {
$arrRow =$query->result_array();
print_r($arrRow);
return($arrRow);
}
return(array());
}
Your code that you have in the controller doing DB stuff should be in the model.
The controller does not have context to
$this->db
modify your joins (3rd param) to retrieve values in actual_price