nemo
(Nemoneel)
February 25, 2010, 12:41am
1
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
intel352
(Jon)
February 25, 2010, 1:40am
2
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
nemo
(Nemoneel)
February 25, 2010, 12:33pm
3
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
intel352
(Jon)
February 25, 2010, 12:52pm
4
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
nemo
(Nemoneel)
February 25, 2010, 12:59pm
5
intel352:
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.
mikl
(Mike)
February 25, 2010, 1:05pm
6
@nemo :
Please give more details, exactly what text users should enter into this one single input field.
nemo
(Nemoneel)
February 25, 2010, 1:16pm
7
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
…
…
andy_s
(Arekandrei)
February 25, 2010, 1:23pm
8
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();
}
intel352
(Jon)
February 25, 2010, 1:57pm
9
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.
nemo
(Nemoneel)
February 25, 2010, 2:59pm
10
intel352:
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
intel352
(Jon)
February 25, 2010, 4:10pm
11
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') ?>
to
<?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();
}
}
nemo
(Nemoneel)
February 25, 2010, 4:44pm
12
intel352:
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') ?>
to
<?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.