3

I'm new to php and after researching I haven't found an answer I understand. I am using php with html. I am reading in from text files (file_get_contents). I am doing a restaurant menu where the first item is "burgers" (burgers.txt). Second is "chicken", etc. I am separating these text files using with explode function. Here is my code

<?php

$dir = 'C:\xampp\htdocs\posplus\txtfiles';
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;//array of files
}
$dot = array_shift($files);
$dotdot = array_shift($files);

$numfiles = count($files);//counts number of text files
for($f=0;$f<$numfiles;$f++)
{
$filearray = array();
$filearray[$f] = file("txtfiles/".$files[$f]);  //txtfile."burgers.txt"
$linessarray = array();
$linesarray[$f] = count($filearray[$f]);  //number of lines in each file (to be           displayed on different lines)
$namesarray = array();
$namesarray[$f] = pathinfo($files[$f]);  //get name of file without extension(.txt) 
${'array'.$f} = explode("\n", file_get_contents("txtfiles/".$files[$f]));

?>
</head>

<body>
<?php

echo'<table align="center">
    <tr>
    <th colspan = "3">'.$namesarray[$f]['filename'].'</th>
</tr>';
//filename without ".txt"
for ($i=0; $i<$linesarray[$f]; $i++) { //loop through each text file

$part = explode(',', ${'array'.$f}[$i]);//split each line in text file by comma example       (cheeseburger, large, €3.00, made from finest beef)
  echo'<tr>

        <td id="name">'.$part[0].'</td>

        <td id="size">'.$part[1].'</td> 

        <td id="price">'.$part[2].'</td>    

 </tr>
 <tr>
        <td id="desc" colspan="3">'.$part[3].'</td> 
 </tr>';
}}

?>

The above code is working fine printing the tables one after the other. My problem is I need to make it dynamic. The job requires using only one screenful as it is a display menu. It needs to be dynamic in the way that if a font is a certain size I only want for example 10 items on a page. Items 1-10 on first page, 11-20 on second, etc. The sizing calculations are no problem but as can be seen from my code I am looping through the text files. I need to do a page break or similar after a certain number of items. If for example I have 6 items on first txt file and 8 on the second, I would need a page break 4 items into my second file.

The only way I can think of to do it would be a foreach merge_recursive loop such as this

$newArray = array_merge_recursive($array0, $array1, $array2); //merge burgers.txt with chicken.txt, etc

foreach ($newArray as $key => $value) {
      echo "$key - $value <br />"; 
}

This will merge arrays and assign a key to all of them allowing me to do page breaks after say 10 items in my $newArray. Problem is I don't know how many text files I will be reading from so I cannot hard code it as it's done above. Is there a way of merging all my arrays through some kind of loop where one array gets added to the one before?

As I said I'm a beginner and I have next to no help so any help would be appreciated. I am aware that the above code is also very poor so suggestions of better ways of doing it may also be helpful. Apologies for the long winded question. Go easy on me.

4

2 回答 2

1

您可以合并所有数组,即:

$allData = array();
for($f=0;$f<$numfiles;$f++)
{
    $filearray = array();
    $filearray[$f] = file("txtfiles/".$files[$f]);  //txtfile."burgers.txt"
    $linessarray = array();
    $linesarray[$f] = count($filearray[$f]);  //number of lines in each file (to be           displayed on different lines)
    $namesarray = array();
    $namesarray[$f] = pathinfo($files[$f]);  //get name of file without extension(.txt) 
    $allData = array_merge($allData, explode("\n", file_get_contents("txtfiles/".$files[$f])));
}

在下一步中,您可以将 $allData 数组分块

foreach (array_chunk($allData, 10) as $chunk) {
    foreach($chunk as $singlePosition) {
     //echo
    }
    echo 'separator';
} 
于 2013-10-22T16:49:01.003 回答
0

谢谢你的帮助。你的信息很有帮助。但是,在使用这种方法时,如果我在第一个文本文件中说 15 行,它将在第 10 行之后分开,在第 15 行之后再次分开,在 15 之后开始下一个块(16-25)。理想情况下,无论文本文件的大小如何,我都需要它来显示第 0-10、10-20、20-30 行。换句话说,能够在同一个 10 块中显示多个文本文件的内容。也许我遗漏了一些明显的东西,但我无法弄清楚如何修复它。这是我的代码,我使用 div 来分隔块

<?php

$dir = 'C:\xampp\htdocs\posplus\txtfiles';
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
  $files[] = $filename;//array of files
}
$dot = array_shift($files);
$dotdot = array_shift($files);

$numfiles = count($files);//counts number of text files
for($f=0;$f<$numfiles;$f++)
{
$filearray = array();
$filearray[$f] = file("txtfiles/".$files[$f]);
$numlinesarray = array();
$numlinesarray[$f] = count($filearray[$f]);//number of lines in each file (to be displayed on     different lines)

$namesarray = array();
$namesarray[$f] = pathinfo($files[$f]);//get name of file without extension(.txt) 
$allData = array();
$allData = array_merge($allData, explode("\n", file_get_contents("txtfiles/".$files[$f]))); 
?>
</head>

<body>
<div id = "container">

<?php
foreach (array_chunk($allData, 21) as $chunk) {

echo'<div class ="col1"><table align="center">
<tr>
    <th colspan = "3">'.$namesarray[$f]['filename'].'</th>
</tr>';
foreach($chunk as $singlePosition) {


    $part = explode(',', $singlePosition);

    echo'<tr>
             <td id="name">'.$part[0].'</td>
             <td id="size">'.$part[1].'</td>    
             <td id="price">'.$part[2].'</td>   

         </tr>';
}
echo '</table></div>';

   }    
} 
?>
</div>

于 2013-10-25T09:56:14.587 回答