Yii Framework Forum: insert multiple records from one input text field - Yii Framework Forum

Jump to content

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

insert multiple records from one input text field Rate Topic: -----

#1 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 24 February 2010 - 07:41 PM

Hello,
I am newly use YII. I want to insert multiple records, like 222-290.
222
223
224
225
...
...
290

one input field

view file
<div class="simple">
<?php echo CHtml::activeLabelEx($model,'record'); ?>
<?php echo CHtml::activeTextField($model,'record'); ?>
</div>

can anyone help me how to save the records
0

#2 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 24 February 2010 - 08:40 PM

Do you want the same field to be repeated dynamically on the page? Or do you want to generate 8 new records for every 1 field entry?

More information would be very helpful
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#3 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 25 February 2010 - 07:33 AM

View Postintel352, on 24 February 2010 - 08:40 PM, said:

Do you want the same field to be repeated dynamically on the page? Or do you want to generate 8 new records for every 1 field entry?

More information would be very helpful

I want to generate 1-100 or more records for 1 field entry. Suppose I have one input text field and user input 1-200 and submit, then controller will insert 1 to 200 records in to the database. I don't understand how to write this in my controller
0

#4 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 25 February 2010 - 07:52 AM

To create multiple records, you could create a while or for loop, inside the loop set $model = new Model();
Then just say, $model->whatever = value; then $model->save();

A better way would be to do a mass insert, dunno if ActiveRecord supports that, but it should be doable with DAO
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#5 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 25 February 2010 - 07:59 AM

View Postintel352, on 25 February 2010 - 07:52 AM, said:

To create multiple records, you could create a while or for loop, inside the loop set $model = new Model();
Then just say, $model->whatever = value; then $model->save();

A better way would be to do a mass insert, dunno if ActiveRecord supports that, but it should be doable with DAO



can you send me an example, I m newly using Yii, so I don't understand that. I really appreciate it.
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 February 2010 - 08:05 AM

@nemo:
Please give more details, exactly what text users should enter into this one single input field.
0

#7 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 25 February 2010 - 08:16 AM

View PostMike, on 25 February 2010 - 08:05 AM, said:

@nemo:
Please give more details, exactly what text users should enter into this one single input field.


User input numeric value in to the text field ( 1523-4343) and submit, then will insert to the database like
1523 one row
1524 2nd row
....
....
0

#8 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 25 February 2010 - 08:23 AM

Probably you'll need to validate user input, but here I assume it is correct:

$model->attributes = $_POST['ModelName'];
list($start, $end) = explode('-', $model->record);
for ($i = $start; $i <= $end; $i++)
{
    $_model = new ModelName;
    $_model->record = $i;
    $_model->save();
}

0

#9 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 25 February 2010 - 08:57 AM

BTW nemo, I would recommend using 2 text fields if you're allowing a range, so your users don't have to adhere to a specific format. Also then you wouldn't have to worry about splitting/exploding the string based on the hyphen.
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#10 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 25 February 2010 - 09:59 AM

View Postintel352, on 25 February 2010 - 08:57 AM, said:

BTW nemo, I would recommend using 2 text fields if you're allowing a range, so your users don't have to adhere to a specific format. Also then you wouldn't have to worry about splitting/exploding the string based on the hyphen.

can you send me an example with controller? It will really help me.
Thanks
0

#11 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 25 February 2010 - 11:10 AM

This is untested, but try a form like:

<div class="form">
<?php echo CHtml::beginForm(); ?>
 
    <div class="row">
        <?php echo CHtml::label('Input Range', false); ?>
        <?php echo CHtml::textField('field1') ?>
    &nbsp; to &nbsp;
        <?php echo CHtml::textField('field2') ?>
    </div>
 
    <div class="row submit">
        <?php echo CHtml::submitButton('Save'); ?>
    </div>
 
<?php echo CHtml::endForm(); ?>
</div><!-- form -->


Then in your controller, check for $_POST['field1'], $_POST['field2'], something like this:

if(!empty($_POST)) {
$start = (int) $_POST['field1'];
$end = (int) $_POST['field2'];

if(empty($end)) {
$end = $start;
}

for ($i = $start; $i <= $end; $i++)
{
    $_model = new ModelName;
    $_model->record = $i;
    $_model->save();
}
}

Need live Yii support? - Join the #yii IRC channel on Freenode!
1

#12 User is offline   nemo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 24-February 10

Posted 25 February 2010 - 11:44 AM

View Postintel352, on 25 February 2010 - 11:10 AM, said:

This is untested, but try a form like:

<div class="form">
<?php echo CHtml::beginForm(); ?>
 
    <div class="row">
        <?php echo CHtml::label('Input Range', false); ?>
        <?php echo CHtml::textField('field1') ?>
    &nbsp; to &nbsp;
        <?php echo CHtml::textField('field2') ?>
    </div>
 
    <div class="row submit">
        <?php echo CHtml::submitButton('Save'); ?>
    </div>
 
<?php echo CHtml::endForm(); ?>
</div><!-- form -->


Then in your controller, check for $_POST['field1'], $_POST['field2'], something like this:

if(!empty($_POST)) {
$start = (int) $_POST['field1'];
$end = (int) $_POST['field2'];

if(empty($end)) {
$end = $start;
}

for ($i = $start; $i <= $end; $i++)
{
    $_model = new ModelName;
    $_model->record = $i;
    $_model->save();
}
}


WOW. Great. Thank you very much.
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