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("
", 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.
You can merge all arrays, ie:
$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("
", file_get_contents("txtfiles/".$files[$f])));
}
in the next step you can chunk $allData array into pieces
foreach (array_chunk($allData, 10) as $chunk) {
foreach($chunk as $singlePosition) {
//echo
}
echo 'separator';
}
Thanks for your help. You're information is very helpful. However, while using this method, if I have say 15 lines in the first text file, it will be separated after line 10 and again after line 15, starting the next chunk after 15 (16-25). Ideally I need it to show lines 0-10, 10-20, 20-30, regardless of size of text file. In other words, be able to display contents of multiple text files in the same chunk of 10. Maybe I'm missing something obvious but I'm unable to figure out how to fix it. Here is my code, I'm using divs to separate chunks
<?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("
", 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>