0

I have 100 input type text boxes and would like to display the value to each box from a txt file. I couldn't figure it out in PHP and thought it might be easier in JavaScript. But I am not familiar with JavaScript. My text fields are being displayed as:

<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">

So I need to add value the text boxes from contacts.txt which has peoples names in line by line. To show as:

<?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">

Here is the includethis.php file that writes all the names to replace the name tag to the 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++;
}
}
4

1 回答 1

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++;
}
于 2013-07-20T23:39:38.743 回答