Yii Framework Forum: Checkboxlist to Save Multiple Records - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Checkboxlist to Save Multiple Records Rate Topic: ***** 1 Votes

#1 User is offline   anoname 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 05-May 11

Posted 05 May 2011 - 02:51 AM

Hello,
I am newly use YII. I want to use checkboxlist to save multiple records. For example there is column/field named "order_number" and "service". One "order_number" can have many "services".

So, this is a form example:

Order_number: ....
Service:
a. ....
b. ....
c. ....
d. ....

When I choose order_number (1) and service (a,b,c) click submit/create, I want to save in database like below:
id order_number service
1 1 a
2 1 b
3 1 c

Is there any solution to solve my problem?
1

#2 User is offline   dcsr 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 13-February 10

Posted 07 June 2011 - 07:58 AM

I'm no expert in yii, but after googling this over half an hour, I ended up doing it manually.

Basically it would depend on what are you planning to do with the data. It's either that you combine the whole array into the same variable using implode

$model = new Post();

if(isset($_POST['Post']))
   $model->attributes = $_POST['Post'];

   $valid = $model->validate();    //<-- I'm just guessing this is how you validate >.<

   if($valid)
       $model->yourCheckBoxItems = implode(",",$model->yourCheckBoxItems);

       if($model->save())
           $this->redirect(blahblah);


or if you are planning to save the checkbox data to another new row, it would be a little longer, but basically it would have the same logic. Never tested it out yet, but you can try it. Shoot back a message if I'm totally wrong

$model = new Post();

if(isset($_POST['Post']))
   $model->attributes = $_POST['Post'];

   $valid = $model->validate();    //<-- I'm just guessing this is how you validate >.<

   if($valid)
       $tempCount = 0; // needed to ensure the correct index
       $tempData = "";

       for($i=0;$i<count($model->yourCheckBox);$i++)
       {
          if(isset($model->yourCheckBox[$i]))
          {
             $tempData[$tempCount] = $model->yourCheckBox[$i];
          }
       }

       for($i=0;$i<count($tempData);$i++)
       {
          $model->setIsNewRecord(true);  // To make sure that this would add new record and not update
          $model->yourCheckBox = $tempData[$i];

          $model->save();
       }

       $this->redirect(blahblah);


I do think that there are better ways to do it, and I haven't yet tried out this:
http://www.yiiframework.com/wiki/81/

It shows how to delete multiple records, wherein the data to be deleted came from some few check boxes. I think that this could also be used in saving items, I'm not sure though since I never had got the opportunity to try this out, correct me if I'm wrong.
1

#3 User is offline   greenlantern 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-August 11

Posted 25 August 2011 - 01:57 AM

View Postdcsr, on 07 June 2011 - 07:58 AM, said:

I'm no expert in yii, but after googling this over half an hour, I ended up doing it manually.

Basically it would depend on what are you planning to do with the data. It's either that you combine the whole array into the same variable using implode

$model = new Post();

if(isset($_POST['Post']))
   $model->attributes = $_POST['Post'];

   $valid = $model->validate();    //<-- I'm just guessing this is how you validate >.<

   if($valid)
       $model->yourCheckBoxItems = implode(",",$model->yourCheckBoxItems);

       if($model->save())
           $this->redirect(blahblah);


or if you are planning to save the checkbox data to another new row, it would be a little longer, but basically it would have the same logic. Never tested it out yet, but you can try it. Shoot back a message if I'm totally wrong

$model = new Post();

if(isset($_POST['Post']))
   $model->attributes = $_POST['Post'];

   $valid = $model->validate();    //<-- I'm just guessing this is how you validate >.<

   if($valid)
       $tempCount = 0; // needed to ensure the correct index
       $tempData = "";

       for($i=0;$i<count($model->yourCheckBox);$i++)
       {
          if(isset($model->yourCheckBox[$i]))
          {
             $tempData[$tempCount] = $model->yourCheckBox[$i];
          }
       }

       for($i=0;$i<count($tempData);$i++)
       {
          $model->setIsNewRecord(true);  // To make sure that this would add new record and not update
          $model->yourCheckBox = $tempData[$i];

          $model->save();
       }

       $this->redirect(blahblah);


I do think that there are better ways to do it, and I haven't yet tried out this:
http://www.yiiframework.com/wiki/81/

It shows how to delete multiple records, wherein the data to be deleted came from some few check boxes. I think that this could also be used in saving items, I'm not sure though since I never had got the opportunity to try this out, correct me if I'm wrong.

0

#4 User is offline   Rohit Suthar 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 12-May 14
  • Location:Mumbai, India

Posted 06 June 2014 - 03:17 AM

View Postanoname, on 05 May 2011 - 02:51 AM, said:

Hello,
I am newly use YII. I want to use checkboxlist to save multiple records. For example there is column/field named "order_number" and "service". One "order_number" can have many "services".

So, this is a form example:

Order_number: ....
Service:
a. ....
b. ....
c. ....
d. ....

When I choose order_number (1) and service (a,b,c) click submit/create, I want to save in database like below:
id order_number service
1 1 a
2 1 b
3 1 c

Is there any solution to solve my problem?



Please follow this articles -

http://www.yiiframew...st-in-the-form/


@rohisuthar
Thanks & regards,

ROHIT SUTHAR
Mumbai(India)

Follow me - @rohisuthar

yii.at

If you found your solutions.... Don't hesitate to click on '+1' button
0

#5 User is offline   sridharv 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 09-July 14

Posted 12 July 2014 - 12:09 AM

Uninitialized string offset: 0
its displays this abv error, please help me! can i need to attach the code? somebody help?
0

#6 User is offline   sridharv 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 09-July 14

Posted 12 July 2014 - 12:25 AM

hi,

I am getting a error while select multiple check box and insert into mysql table ,Please somebody Help

CDbException

Column name must be either a string or an array.

<---------------------------------- This is the error

D:\xampp\htdocs\emerald_erp\framework\db\schema\CDbCommandBuilder.php(828)

816 }
817 if(count($values)===1)
818 {
819 $entries=array();
820 foreach($values[0] as $name=>$value)
821 $entries[]=$prefix.$table->columns[$name]->rawName.($value===null?' IS NULL':'='.$value);
822 return implode(' AND ',$entries);
823 }
824
825 return $this->createCompositeInCondition($table,$values,$prefix);
826 }
827 else
828 throw new CDbException(Yii::t('yii','Column name must be either a string or an array.'));
829 }
830
831 /**
832 * Generates the expression for selecting rows with specified composite key values.
833 * @param CDbTableSchema $table the table schema
834 * @param array $values list of primary key values to be selected within
835 * @param string $prefix column prefix (ended with dot)
836 * @return string the expression for selection
837 */
838 protected function createCompositeInCondition($table,$values,$prefix)
839 {
840 $keyNames=array();

/************* THis is FUNCTION of update query*****************/////

public function actionUpdate($id)
{
$model=$this->loadModel($id);
$menumodel = new MenuRoles;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['Roles']))
{

$model->attributes=$_POST['Roles'];
//$roleid = $model->id;

if($model->save()) {
$menumodel->attributes=$_POST['MenuRoles'];

foreach($menumodel->menu_id as $menuid){

$menumodel->menu_id = $menuid;
$menumodel->role_id = 1;
$menumodel->save();
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('update',array(
'model'=>$model,
'menu'=>$menumodel,
));

/

}
0

#7 User is offline   Niral Shakya 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 15-July 15

Posted 16 September 2015 - 05:19 AM

Same thing in check boxlist.
I want in update case .
Do anyone have idea for it?

This post was helpful
Thanks
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users