从文本文件为多个输入类型文本字段创建值

时间:2013-07-20 23:16:25

标签: php

我有100个输入类型文本框,并希望从txt文件中显示每个框的值。 我无法在PHP中弄清楚它并认为它在JavaScript中可能更容易。但我不熟悉JavaScript。我的文本字段显示为:

<input type="text" size="13" name="contacts[]" id="contact0">
<input type="text" size="13" name="contacts[]" id="contact1">
<input type="text" size="13" name="contacts[]" id="contact2">
<input type="text" size="13" name="contacts[]" id="contact3">

所以我需要添加来自contacts.txt的文本框的值,这些文本框中的人名是逐行的。 显示为:

<?php include 'includethis.php' ?>
<input type="text" size="13" name="contacts[]" id="contact0" value="David">
<input type="text" size="13" name="contacts[]" id="contact1" value="Erick">
<input type="text" size="13" name="contacts[]" id="contact2" value="John">
<input type="text" size="13" name="contacts[]" id="contact3" value="Frank">

这是includethis.php文件,它写入所有名称以将name标签替换为index.php

$filename = 'pics.txt';
$handle = fopen($filename, 'r');
$datain = fread($handle, filesize($filename));
$names_array = explode("\n", $datain);

$count = 0;
$counter = 0;
foreach($names_array as $show){
if($count < 4)
{
echo '<img src="images/'.$show.'">';
$count++;

}
else
{

$count = 0;   
echo '<br><input type="text" size="13" name="contacts[]" id="contact'.$counter++.'"><input type="text" size="13" name="contacts[]" id="contact'.$counter++.'"><input type="text" size="13" name="contacts[]" id="contact'.$counter++.'">';
echo '<br>'.'<img src="images/'.$show.'">';
$count++;
}
}

1 个答案:

答案 0 :(得分:0)

$filename = 'pics.txt';
$handle = fopen($filename, 'r');
$datain = fread($handle, filesize($filename));
$names_array = explode("\n", $datain);
//contacts.txt
$filename2 = "contacts.txt";
$contact_data = file_get_contents($filename2);
$contact_array = explode("\n",$contact_data);
$contact_max = count($contact_array) - 2;
$count = 0;
$counter = 0;
foreach($names_array as $show){
if($count < 4) {
echo '<img src="images/'.$show.'">';
}
else
{
echo '<br>';
for($y=0;$y<3;$y++) {
  if($counter < $contact_max) {
    $contact = (isset($contact_array[$counter]))?$contact_array[$counter]:"";
    echo '<input type="text" size="13" name="contacts[]" value="'.$contact.'" id="contact'.$counter.'" />';
    $counter++;
  }
}

echo '<br>'.'<img src="images/'.$show.'">';
$count=0;
}
$count++;
}