Html table updated by a text file with PHP

Hi

I know this is a question is already mentioned but i wanted to know with the code below, can i upload the row on that day. Ie 9/12/17 it displays the info from that row (not the date).

PHP Code
<? 
$fulltext = file_get_contents("data/test1.txt"); 
$linearray = explode("\n", $fulltext); 
foreach($linearray as $line => $text){ 
    $fullarray[] = explode("\t", $text); 
} 
echo "<table>"; 
foreach($fullarray as $key => $value){ 
    echo "<tr>"; 
    foreach($value as $which => $text){ 
        echo "<td>{$text}</td>"; 
    } 
    echo "</tr>"; 
} 
echo "</table>"; 
?>

Open in new window


Text File
			Morning		Afternoon		Evening
8/12/17			10am		12pm		6am
9/12/17			9am		1pm		7am
10/12/17			8am		2pm		6am

Open in new window


A test output is here: click here. However the real output i would like is attached, in a html table format.
test1.xlsx
Imran ShabirAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NerdsOfTechTechnology ScientistCommented:
So to clarify, you want the dates and five (5) time columns per date?

'Date'            Time1      Time2      Time3      Time4      Time5

12/8/2017                              
12/9/2017                              
12/10/2017                              
12/11/2017                              

Since the date data is ambiguous, exploding it from d/m/y and reforming as m/d/y is advised:

<?php
$fulltext = file_get_contents("data/test1.txt"); 

// sample input
/*
$fulltext = 'Morning		Afternoon		Evening
8/12/17			10am		12pm		6am
9/12/17			9am		1pm		7am
10/12/17			8am		2pm		6am';
*/

$linearray = explode("\n", $fulltext); 
foreach($linearray as $line => $text){ 
    $fullarray[] = explode("\t", $text); 
} 
echo "<table>\n"; 
echo "<tr>\n"; 
echo "<td>&nbsp;</td>\n";
for ($i = 1; $i <= 5; $i++) {
	echo '<td>Time' . $i . "</td>\n";
}  
echo "</tr>\n"; 
$x=0;
foreach($fullarray as $key => $value){ 
    if($x){ //skip first line when $x=0, otherwise run
	    echo "<tr>\n"; 
	    $df = explode('/', $value[0]);							// input format day[0]/month[1]/year[2]
	    if(count($df)>=3){										// test if only three elements: day, month, year exist	
    		if(checkdate(intval($df[1]), intval($df[0]), intval($df[2]))){				// expected input: month[1], day[0], year[2]
    		    $timestamp = intval($df[1]) . '/' . intval($df[0]) . '/' . intval($df[2]);      // output m[1]/d[0]/y[2]
		    }else{
    			$timestamp = ''; //invalid date
		    }
	    }else{
    		$timestamp = ''; //invalid element count
    	}
    	echo '<td>' . $timestamp . "</td>\n";
    	for ($i = 1; $i <= 5; $i++) {
		    echo "<td>&nbsp;</td>\n";
	    }  
        echo "</tr>\n"; 
    } 
    $x++;
}
echo "</table>\n"; 
?>

Open in new window

0
NerdsOfTechTechnology ScientistCommented:
(Edit) working (tested) solution code above.
0
Imran ShabirAuthor Commented:
Hi NerdsofTech

I uploaded your code and text file, this is the result click here. textfile ishere .
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Imran ShabirAuthor Commented:
As per the excel spreadsheet..

1) I want the text file to display in rows on a html table: text file layout tab
2) i want it to display in a 2 table without date: output tab
0
NerdsOfTechTechnology ScientistCommented:
I don't understand. Please clarify:
1) I want the text file to display in rows on a html table: text file layout tab
?
2) i want it to display in a 2 table without date: output tab
?

The output was exactly as specified before.
0
Imran ShabirAuthor Commented:
As per the excel spreadsheet..

1) I want the text file to display in rows on a html table:
2) i want it to display in a 2 table without date: output tab

Also, your config, you can see doesnt bring up the text file data.
0
NerdsOfTechTechnology ScientistCommented:
You repeated your last comment. Please clarify the meaning.

From the hypothetical text file:
			Morning		Afternoon		Evening
8/12/17			10am		12pm		6am
9/12/17			9am		1pm		7am
10/12/17			8am		2pm		6am

Open in new window


Can you please describe where the data should go?

Where did these times come from on the output tab? Are they hard-coded?:

From your excel spreadsheet
output tab:
	Morning	Afternoon
Time1	9:00	13:00
Time2	10:00	14:00
Time3	11:00	15:00
Time4	12:00	16:00
Time5	8:00	17:00

Open in new window


This tab shows exactly how the PHP code outputs (no times, blank)? Is this not correct?
text file layout tab:
	Time1	Time2	Time3	Time4	Time5
12/8/2017					
12/9/2017					
12/10/2017					
12/11/2017	

Open in new window

                       

Please expand on the expected results and how the input data comes into play. Thank you.
0
Imran ShabirAuthor Commented:
Hi

Apologies i am not making it easier for you.

As mentioned above, i want to upload the data as per the hypothetical text file (morning, afternoon, evening) to be inputted to php but displayed on the page in a table. Now the catch here is, where i have the dates added for that date the entries (example for 8/12/17, only the morning afternoon and evening times should display not the rest).

Your questions
  1. Can you please describe where the data should go?
- This will be uploaded via a text file (or if you excel etc. would work better)
  1. Where did these times come from on the output tab? Are they hard-coded?:
- The times are an example of something that has been requested for a website setup.
  1. This tab shows exactly how the PHP code outputs (no times, blank)? Is this not correct?
- These were examples i was adding but i think they are more of an extraction.

Hope this explains it.
0
Shalom CarmelCTOCommented:
This expert suggested creating a Gigs project.
Hey Imran,

This question is on the border of asking the community to give you pro bono work. Did you consider making a gig for actual consulting?
0
Imran ShabirAuthor Commented:
Hi Shalomc

Not sure what you mean by gigs project?

Imran
0
Shalom CarmelCTOCommented:
I mean these gigs. https://www.experts-exchange.com/gigs/

I, for example, walk away from questions that turn out to be requests for free work rather than "how to" questions.
Imho, this is why gigs exist: so you can work with an expert to write code according to your exact specifications.

Cheers
0
Imran ShabirAuthor Commented:
I understand i asked the wrong question but can you suggest how i turn the below into a table format please?

Php code
<html>
	<body>

	<?php
	$f = fopen("upload/upload.txt", "r");
	while(!feof($f)) { 
	    echo fgets($f) . "<br />";
	}

	fclose($f);

	?>

	</body>
	</html>

Open in new window


Txt file
Day | Time1 |  Time2 | Time3 | Time4 | Time5 | Time6
Day 1 | 07:00 |  13:00 | 16:00 | 18:00 | 19:00 | 22:00
Day 2 | 08:00 |  14:00 | 17:00 | 19:00 | 19:00 | 12:00
Day 3 | 09:00 |  15:00 | 16:00 | 18:00 | 09:00 | 22:00

Open in new window

0
NerdsOfTechTechnology ScientistCommented:
Your input, data delimiter, and question have changed...

The following script takes your new input, new delimiter " | ", and spits out the expected table output:

<?php
// $fulltext = file_get_contents("data/test1.txt"); //uncomment this to read from file and remove the sample input assignement below

// sample input
$fulltext = 'Day | Time1 | Time2 | Time3 | Time4 | Time5 | Time6
Day 1 | 07:00 | 13:00 | 16:00 | 18:00 | 19:00 | 22:00
Day 2 | 08:00 | 14:00 | 17:00 | 19:00 | 19:00 | 12:00
Day 3 | 09:00 | 15:00 | 16:00 | 18:00 | 09:00 | 22:00';

$linearray = explode("\n", $fulltext); 
foreach($linearray as $line => $text){ 
    $fullarray[] = explode(" | ", $text); 
} 

/* //debug
echo '<pre>';
print_r($fullarray);
echo '</pre>';
*/
echo "<table>\n"; 
foreach($fullarray as $k1 => $v1){
	echo "<tr>\n";
	foreach ($v1 as $k2 => $v2){
		echo "<td>$v2</td>";
	}
	echo "</tr>\n"; 
}
echo "</table>\n";
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Imran ShabirAuthor Commented:
Thanks for the response.

I copied your code and added it only shows the data from your sample.

<?php
$fulltext = file_get_contents("data/test1.txt"); 

//uncomment this to read from file and remove the sample input assignement below

// sample input
$fulltext = 'Day | Time1 | Time2 | Time3 | Time4 | Time5 | Time6
Day 1 | 07:00 | 13:00 | 16:00 | 18:00 | 19:00 | 22:00
Day 2 | 08:00 | 14:00 | 17:00 | 19:00 | 19:00 | 12:00
Day 3 | 09:00 | 15:00 | 16:00 | 18:00 | 09:00 | 122:00';

$linearray = explode("\n", $fulltext); 
foreach($linearray as $line => $text){ 
    $fullarray[] = explode(" | ", $text); 
} 

/* //debug
echo '<pre>';
print_r($fullarray);
echo '</pre>';
*/
echo "<table border=“1” cellspacing=“10”>\n"; 
foreach($fullarray as $k1 => $v1){
	echo "<tr>\n";
	foreach ($v1 as $k2 => $v2){
		echo "<td>$v2</td>";
	}
	echo "</tr>\n"; 
}
echo "</table>\n";
?>

Open in new window

0
Imran ShabirAuthor Commented:
Ok i removed the sample bit and it brought it up. Thank you.
0
NerdsOfTechTechnology ScientistCommented:
You're welcome. Was the output satisfactory?
0
Imran ShabirAuthor Commented:
Hi NerdsofTech

Apologies I didnt realise you replied, the results where satisfactory thank you. I am just testing out customising the table but thats something i can work on.

Thank you again.
1
NerdsOfTechTechnology ScientistCommented:
Thank you for the update. Please mark my solution #a42454715 above as 'best solution' to close out the question.
0
Imran ShabirAuthor Commented:
Very helpful
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.