如何将数据传递到文本字段 [英] How to pass the data into the text-fields
问题描述
我正在尝试编写用于解析上传的 .csv
文件的代码.我已经编写了以下代码.控制器以数组的形式生成 CSV 文件的行,我也尝试使用 print_r($buffer);
语句打印,我得到了预期的输出.虽然可以使用 echo $string;
打印字符串,但我无法在保存到数据库之前将它们传递给文本字段进行编辑.
站点控制器
公共函数 actionImport(){$buffer[] = array();if(isset($_POST['submitCSV'])){$filename=$_FILES['filename']['tmp_name'];$fp = fopen("$文件名","r");如果($ fp){$i = 0;while(($buffer[$i++] = fgetcsv($fp,1000,",")) !== false){}if(!feof($fp))echo "Echo: 意外的 fgets() 失败\n";}fclose($fp);未设置($_POST['submitCSV']);}$this->render('index',array('buffer'=>$buffer));}
查看(index.php)
<?php echo CHtml::beginForm('site/import',$method='post',$htmlOptions =array('enctype'=>'multipart/form-data'));?><div class="row"><input type="hidden" name="MAX_FILE_SIZE" value="30000"/><input type="file" name="filename" value="file"><?phpecho CHtml::submitButton('Submit', array('name'=>'submitCSV'));foreach($buffer 作为 $line)foreach($line 作为 $string)echo CHtml::activeTextField($string,$htmlOptions = array('value'=>$string));?><?php echo CHtml::endForm();?>
在您的控制器中,尝试以下操作:
公共函数 actionImport(){如果 (isset($_POST['submitCSV'])) {$filename = $_FILES['filename']['tmp_name'];$fp = fopen("$文件名", "r");如果($ fp){while(($buffer = fgetcsv($fp,1000,",")) !== false){}if(!feof($fp)) {echo "Echo: 意外的 fgets() 失败\n";}}fclose($fp);如果(计数($缓冲)){$this->render('index',array('buffer' => $buffer));}}}
在您看来:
<代码><p><?CHtml::activeTextField($line, array('class' => 'login_textbox', 'style' => 'width:100%', 'value' => $line));?></p><?Endforeach;?>
您将一组数据(由 CSV 行组成)传递给您的视图,然后该视图遍历该数组并为每个数据呈现一个文本字段.确保您启用了 short_tags.
I am trying to write the code for parsing the uploaded .csv
file. I have written the following code. The controller generates the rows of the CSV file in the form of arrays which I have also tried to print with print_r($buffer);
statement and I get the expected output. Though the strings can be printed using echo $string;
, but I am not able to pass them to the text-fields for editing before saving to the database.
SiteController
public function actionImport()
{
$buffer[] = array();
if(isset($_POST['submitCSV'])){
$filename=$_FILES['filename']['tmp_name'];
$fp = fopen("$filename","r");
if($fp){
$i = 0;
while(($buffer[$i++] = fgetcsv($fp,1000,",")) !== false)
{}
if(!feof($fp))
echo "Echo: Unexpected fgets() fail\n";
}
fclose($fp);
unset($_POST['submitCSV']);
}
$this->render('index',array('buffer'=>$buffer));
}
View(index.php)
<div class="form">
<?php echo CHtml::beginForm('site/import',$method='post',$htmlOptions =array('enctype'=>'multipart/form-data')); ?>
<div class="row">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="filename" value="file">
<?php
echo CHtml::submitButton('Submit', array('name'=>'submitCSV'));
foreach($buffer as $line)
foreach($line as $string)
echo CHtml::activeTextField($string,$htmlOptions = array('value'=>$string));
?>
</div>
<?php echo CHtml::endForm(); ?>
In your controller, try something like:
public function actionImport()
{
if (isset($_POST['submitCSV'])) {
$filename = $_FILES['filename']['tmp_name'];
$fp = fopen("$filename", "r");
if($fp) {
while(($buffer = fgetcsv($fp,1000,",")) !== false)
{
}
if(!feof($fp)) {
echo "Echo: Unexpected fgets() fail\n";
}
}
fclose($fp);
if (count($buffer)) {
$this->render('index',array('buffer' => $buffer));
}
}
}
And in your view:
<? foreach ($buffer as $line): ?>
<p><? CHtml::activeTextField($line, array('class' => 'login_textbox', 'style' => 'width:100%', 'value' => $line)); ?> </p>
<? endforeach; ?>
You're passing an array of data (composed of your CSV lines) to your view, which is then iterating over the array and rendering a text field for each one. Make sure you have short_tags enabled.
这篇关于如何将数据传递到文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!