I have php script that read from txt files and display the result after allow the user to enter a string to search.
The system display the user request with the filename and line number. as it show in the uploaded image .
what I need is to make each column contain the file name and the rows will contain line number.
the result based on Mirza's answer
last result
ScreenShot
based on latest update of Mirza answer
سيلان الطاقة الحرارية من البيئة وإليها. وعادة ما يطلق هذا الاسم على ظاهرة ارتفاع درجات حرارة الأرض عن معدلها الطبيعي. وقد ازداد المعدل العالمي لدرجة حرارة الهواء عند سطح الأرض ب0.74 ± 0.18 °C خلال المائة عام المنتهية سنة 2005. وحسب اللجنة الدولية لتغير المناخ(IPCC) فان "أغلب الزيادة الملحوظة في معدل درجة الحرارة العالمية منذ منتصف القرن العشرين تبدو بشكل كبير نتيجة لزيادة غازات الاحتباس الحراري (غازات البيت الزجاجي) التي تبعثها النشاطات التي يقوم بها البشر.
تخترق أغلب أشعة الشمس الغلاف الجوي لتصل إلى سطح الأرض وأغلب تلك الأشعة تقع في الطول الموجي المرئي (ضوء الشمس الذي تراه العين) لذلك يحدث تسخين لسطح الأرض أثناء النهار، ولا تستطيع الأرض امتصاص الطاقة الشمسية بالكامل، ولو حدث ذلك فستصل حرارة الأرض إلى درجة الانصهار، وتكون نهاية الحياة عليها.
تسخن الأرض لامتصاص حرارة الشمس، ثم يعيد سطح الأرض إشعاع الطاقة الممتصة مرة أخرى إلى الغلاف الجوي، وعند غياب أي تأثيرات معقدة يحدث اتزان حراري. وفي هذه الحالة تكون درجة حرارة سطح الأرض نحو – 2.3 درجة مئوية، وتكون الطاقة التي تعيد الأرض إشعاعها على صورة أشعة تحت الحمراء (وهذا المثال مخالف للواقع).
وفي الواقع تعقيدات، حيث يحتجز الغلاف الجوي جزءاً من الأشعة تحت الحمراء؛ لوجود جزئيات ثاني أكسيد الكربون وبخار الماء التي تمتص الجزء الأكبر من الأشعة تحت الحمراء، والجزء الذي لا يتم إمتصاصه يخرج من الغلاف الجوي إلى الفضاء، ويعمل الجزء الممتص من الأشعة تحت الحمراء في الغلاف الجوي على رفع درجة الحرارة.1
<?php
$result = [];
if(isset($_POST["search"]))
{
$search =$_POST['name'];
echo "the word $search exist: <br><br>";
foreach(glob($_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt") as $txts)
{
$line = 1;
$temp = [];
$myFileLink = fopen($txts, 'r');
while(!feof($myFileLink))
{
$myFileContents = fgets($myFileLink);
if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
{
$temp['filename'] = basename ($txts);
foreach($matches[1] as $match)
{
$temp['lines'][] = $line;
}
}
++$line;
}
fclose($myFileLink);
$result[] = $temp;
}
//display the table
echo '<table border=2>';
$filenameHtml = '<tr>';
$lineNumberHtml = '<tr>';
foreach ($result as $item){
$filename = isset($item['filename']) ? $item['filename'] : '';
$lines = isset($item['lines']) ? implode(',',$item['lines']) : '';
$filenameHtml .= "<th>$filename</th>";
$lineNumberHtml .= "<td>$lines</td>";
}
$filenameHtml .= '</tr>';
$lineNumberHtml .= '</tr>';
echo $filenameHtml.$lineNumberHtml;
echo '</table>';
}
?>
<html>
<head>
</head>
<meta http-equiv="Content-Language" content="ar-sa">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
#form {
background: -webkit-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
background: linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
margin: auto;
width: 200px;
height: 200px;
position: absolute;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-style: italic;
line-height: 24px;
font-weight: bold;
color: #09C;
text-decoration: none;
border-radius: 10px;
padding: 10px;
border: 1px solid #999;
border: inset 1px solid #333;
-webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
}
</style
<body>
<div id = "form">
<form action="index.php" method="post">
<h1 align =center > Search Form </h1>
<p>enter your string <input type ="text" id = "idName" name="name" /></p>
<p align =center ><input type ="Submit" name ="search" value= "Search" /></p>
</form>
</div>
</body>
</html>
If i understand correctly, you want to see the result as shown below.
Please replace the php code as below.
<?php
$result = [];
if(isset($_POST["search"]))
{
$search =$_POST['name'];
echo "the word $search exist: <br><br>";
foreach(glob($_SERVER['DOCUMENT_ROOT']."/readfiletest/*.txt") as $txts)
{
$line = 1;
$temp = [];
$myFileLink = fopen($txts, 'r');
while(!feof($myFileLink))
{
$myFileContents = fgets($myFileLink);
if( preg_match_all('/('.preg_quote($search,'/').')/i', $myFileContents, $matches))
{
$temp['filename'] = basename ($txts);
foreach($matches[1] as $match)
{
$temp['lines'][] = $line;
}
}
++$line;
}
fclose($myFileLink);
if(!empty($temp)) {
$result[] = $temp;
}
}
//display the table
echo '<table border=2>';
$filenameHtml = '<tr>';
foreach ($result as $item) {
$filename = isset($item['filename']) ? $item['filename'] : '';
$filenameHtml .= "<th>$filename</th>";
//this line was not having the concatination
$lineNumberHtml .= '<td><table width="100%" border=2>';
foreach($item['lines'] as $line) {
$lineNumberHtml .= '<tr>';
$lineNumberHtml .= "<td>$line</td>";
$lineNumberHtml .= '</tr>';
}
$lineNumberHtml .= '</table></td>';
}
$filenameHtml .= '</tr>';
echo $filenameHtml.$lineNumberHtml;
echo '</table>';
}
?>
Hope this helps !