thegweb Posted October 19, 2015 Share Posted October 19, 2015 (edited) I need to perform some filter check on data read from text file into table. For example if published year comes before the birth year, append this entry to a new text file, also if any line with a missing year, append this entry into a different text file. Which author wrote the most books, who was the oldest author, and if title is in "yyyy-mm-dd" format, echo them as special books. here is my code: <?php function get_rows() { $file=fopen("books.txt",'r'); while($line = fgets($file)){//while we can still read the file //$line=trim($line);//remove the line endings and extra white-spaces at start and end list($title,$firstname,$lastname,$published_year,$birth_year) = explode(' ',$line);//you get an array of 5 per line, first item is title, etc... echo "<tr><td style='border:1px solid #ccc'>$title</td><td style='border:1px solid #ccc'>$firstname</td><td style='border:1px solid #ccc'>$lastname</td> <td style='border:1px solid #ccc'>$published_year</td><td style='border:1px solid #ccc'>$birth_year</td></tr>\n";//use the \n to clean up the source a code a bit to make it readable //print_r($birth_year) ; } return true; } ?> <table border="1"> <tr> <th style='border:1px solid #ccc'>Title</th> <th style='border:1px solid #ccc'>Firstname</th> <th style='border:1px solid #ccc'>Lastname</th> <th style='border:1px solid #ccc'>Published Year</th> <th style='border:1px solid #ccc'>Birth Year</th> </tr> <?php get_rows(); ?> </table> Edited October 23, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 19, 2015 Share Posted October 19, 2015 And what problems are you having with your current code? Your explanation is unclear. When you say " . . . if published year comes before the birth year, append this entry to a new text file." I have no idea what you mean. Are you wanting to edit the text file being read to remove that content and put into a new text file? If so, how to determine what the new text file will be named? As far as Which author wrote the most books, who was the oldest author, and if title is in "yyyy-mm-dd" format, echo them as special books. You will need to read all of the data into an array and then parse the values to find those records. Or, you could put this data into a database where it belongs and make everything much easier. Quote Link to comment Share on other sites More sharing options...
thegweb Posted October 19, 2015 Author Share Posted October 19, 2015 Hi, This code runs beautifully without any error. But I need more to add into this. This reads the text file into an array and displays the output into html table. I am only working on text files no database as this is my school project and I am new to php. First I need to know how the data can be sorted by published year. Then if there is any error in my text file like any field missing, say birth year is missing, skip that whole line from my output table and append that line into another text file. Third filter, is based on published year, if published year is grater than birth year then its an error, again that should skip the line from my output table and append this into another text file. If book title is in date format, how to grab those titles and echo them. I am unable to perform these filters on my text file while displaying it into html table. my books.txt file looks like this: title1 John Doe 1960 1935 title2 Jane Doe 1970 1935 title3 Jason Doe 1971 1960 title4 Gee Swan 1960 1984 2015-09-01 Dave Hertner 2000 1984 2001-02-28 Ron Swan 1999 2011-05-20 Mark Ramsay 2001 1980 Hope I was able to give clear description of what I need in code. I will be thankful to you if you can help me in this. Quote Link to comment Share on other sites More sharing options...
thegweb Posted October 23, 2015 Author Share Posted October 23, 2015 (edited) Basic analysis of the data from books.txt. I need to perform these four questions on Which author wrote the most books? Which author was the oldest when they published their book? hat is the longest book title? How many books were published before 1950? My books.txt file looks like this Book-title Firstname Lastname Published-Year Birth-Year title1 John Doe 1940 1935 title2 Jane Doe 1945 1932 title3 Jason Doe 1971 1940 title5 John Doe 1965 1935 2001-02-28 Mark Ramsay 1999 This-is-a-long-title Dave Hertner 2007 1983 I am reading this file and displaying as html Table. But having hard time on applying these checks on array columns. here is my code: <div style="border:1px solid #ccc; padding:10px; width:650px; margin:0 auto;"> <ul> <li>Which author wrote the most books?</li> <li>Which author was the oldest when they published their book?</li> <li>what is the longest book title?</li> <li>How many books were published before 1950?</li> </ul> <?php include 'logincheck.php'; $file_handle = fopen("books.txt", "r"); /* empty books array to store values laters */ $books = array(); //* $header variable to store the titles of table and then use the $header to grab the value to display in output */ $header = array('title', 'firstname', 'lastname', 'published_year', 'birth_year'); /* Creating publish_year array to find the missing publish year and to sort by publish year */ $publish_year = array(); $birthYear = array(); $title_length = array(); $firstname = array(); $lastname = array(); $date_format = array(); /* declaring counter variable to store the key and then later on to use this to match key with books array to apply filters*/ $counter = 0; // declaring counter variable to store the key and then later on to use this to match key with books array to apply filters while (!feof($file_handle)) { $line_of_text = fgets($file_handle); $line_of_text = trim($line_of_text); //echo $line_of_text . "<br>"; //list($title,$firstname,$lastname,$published_year,$birth_year) = explode(' ', $line_of_text); $list = array_combine($header, explode(' ', $line_of_text)); if(!empty($list['published_year']) && $list['published_year'] < "1950") { //echo $list['title'] . " "; $count_books = count($list['published_year']); echo $count_books . " books were published before 1950 " . "(" . $list['title'] . ")<br> "; } $age = array(); if(!empty($list['published_year'])) { $age = $list['published_year'] - $list['birth_year']; echo $age . "<br>"; } // here $counter gives us the key match $books[$counter] = $list; $title_length[$counter] = $list['title']; $birthYear[$counter] = $list['birth_year']; $publish_year[$counter] = $list['published_year']; $date_format[$counter] = $list['title']; $firstname[$counter] = $list['firstname']; $lastname[$counter] = $list['lastname']; $counter++; } $fullname = array(); $duplicates = array(); for ($i =0; $i < count($firstname); $i++){ $fullname_tmp = $firstname[$i]." ".$lastname[$i]; if (in_array($fullname_tmp, $fullname)){ if (in_array($fullname_tmp, $duplicates)){ $duplicates[] = $fullname_tmp; //echo $fullname_tmp . "<br>"; } else{ $duplicates[] = $fullname_tmp; //$duplicates[] = $fullname_tmp; //echo $fullname_tmp . " wrote the most books. (" . count($duplicates) . " books)<br>"; } } else{ $fullname[] = $fullname_tmp; } } //echo "<pre>\n duplicates \n"; print_r($duplicates); //echo $fullname . " wrote the most books." . "<br>"; // echo "\n full names \n"; //print_r($fullname); $lengths = array_map('strlen', $title_length); $book_title_length = max($lengths); //echo "<br>" . $book_title_length . "<br>"; if(!empty($list['title']) == $book_title_length) {echo "<br>\"" . $list['title'] . "\"" ." is the longest title (" . $book_title_length . " characters)<br>" ;} echo '<ul><li><a href="browse.php">Browse books in store</a></li><li><a href="analytic.php">Analytics</a></li><li><a href="logout.php">Logout</a></li></ul>'; fclose($file_handle); ?> Edited October 23, 2015 by Ch0cu3r Added code tags Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 23, 2015 Share Posted October 23, 2015 (edited) Any reason your not using a database instead of a flatfile? Edited October 23, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
thegweb Posted October 23, 2015 Author Share Posted October 23, 2015 Hi, Yes, the reason is I am working on school project and using text file is the requirement. Quote Link to comment Share on other sites More sharing options...
seandisanti Posted October 23, 2015 Share Posted October 23, 2015 assuming this is for a homework assignment or something, what directions does it give you for implementation? Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 23, 2015 Share Posted October 23, 2015 If only these schools would teach real world programming. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2015 Share Posted October 23, 2015 @thegweb Topics merged. Please do not post multiple topics for the same question. Also please note when posting code to wrap it inside tags or press the <> button in the editor Quote Link to comment Share on other sites More sharing options...
thegweb Posted October 23, 2015 Author Share Posted October 23, 2015 Sorry, I am new to this website. I will take care of this from now onwards. Quote Link to comment Share on other sites More sharing options...
printf Posted October 26, 2015 Share Posted October 26, 2015 Okay, based on what you are wanting to do, I will tell you how to do those things... you said.... First I need to know how the data can be sorted by published year... I said.... To do that you will need to go through the whole file and place the valid data into an array, then sort that array on the column that you want to sort on you said... Then if there is any error in my text file like any field missing, say birth year is missing, skip that whole line from my output table and append that line into another text file. I said.... to do that, while you are going through your books database, add a if() condition that test for those missing values. In this case you know your database must contain (5) columns of data, so if(sizeof($line)) , which represents a row of data doesn't equal (5), you would add that line to a 'missing' data file you said... Third filter, is based on published year, if published year is grater than birth year then its an error, again that should skip the line from my output table and append this into another text file. I said.... to do that, while you are going through your books database, add a if() condition that test for those invalid values. So if the publishing year is greater than the birth year. append that line to a 'invalid' data file... Now my simple example... /* books database file 'books.txt' */ title1 John Doe 1960 1935 title2 Jane Doe 1970 1935 title3 John Doe 1935 title4 Jason Doe 1971 1960 title5 Gee Swan 1960 1984 title6 Jane Doe 1935 1970 2015-09-01 Dave Hertner 2000 1984 2001-02-28 Ron Swan 1999 2011-05-20 Mark Ramsay 2001 1980 Now a quick script that you can study... (untested but it should work) <?php // function put in file function putFile ( $file, $data, $new = 'w' ) { $io = fopen ( $file, $new ); fputs ( $io, implode ( ' ', $data ) . "\r\n" ); fclose ( $io ); } // sorting functions // sort newest to oldest function sortA ( $a, $b ) { return ( $a['pyear'] == $b['pyear'] ? 0 : ( $a['pyear'] > $b['pyear'] ? -1 : 1 ) ); } // sort oldest to newest function sortD ( $a, $b ) { return ( $a['pyear'] == $b['pyear'] ? 0 : ( $a['pyear'] < $b['pyear'] ? -1 : 1 ) ); } // books database $db = 'books.txt'; // missing data file $mf = 'missing.txt'; // invalid file ( publising year > birth year ); $if = 'invalid.txt'; // valid row size $vs = 5; // column names $cn = array ( 'title', 'fname', 'lname', 'pyear', 'byear' ); // output container $oc = array (); // db handle $io = fopen ( $db, 'r' ); // read the books db while ( ! feof ( $io ) ) { $line = explode ( ' ', trim ( fgets ( $io, 1048 ) ) ); if ( sizeof ( $line ) != 5 ) { putFile ( $mf, $line, 'a' ); continue; } elseif ( ( int ) $line[3] > ( int ) $line[4] ) { putFile ( $if, $line, 'a' ); continue; } else { $oc[] = array_combine ( $cn, $line ); } } fclose ( $io ); // sort the books array, sort oldest to newest uasort ( $oc, 'sortD' ); // just show the result print_r ( $oc ); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.