2013-07-20 59 views
0

我有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文件写入所有姓名为名称的标签替换到的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++; 
} 
} 
+0

哪里是'contacts.txt'?我的意思是,它在服务器上在线吗?在用户的电脑中?已经在页面中?哪里? – acdcjunior

+0

与'index.php'相同的目录 – blackfilms

+0

它的格式是什么?该程序将如何知道要映射到每个“输入”的线? – acdcjunior

回答

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++; 
} 
+0

与我的编辑 – blackfilms

+0

正常工作您编辑了什么?我看到代码是一样的,只有$ counter + 4被删除? –

+0

需要在3个输入框后打破 – blackfilms

相关问题