Jump to content

thegweb

New Members
  • Posts

    8
  • Joined

  • Last visited

thegweb's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Sorry, I am new to this website. I will take care of this from now onwards.
  2. Hi, Yes, the reason is I am working on school project and using text file is the requirement.
  3. 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); ?>
  4. Thanks for your help. Being a student and working on a project, we cant say no to our instructor, it will then affect grades. For the second question: This project is just a class assignment and we are not told to really worry about the secure sessions. Though this is stupid to work in an appropriate way but assignment is assignment. I appreciate your help. I saved cookie in different file to check if it is set then good otherwise redirect to main page for login without any error message. Thanks again,
  5. Hi, I know database will make it alot easier and thiis not my decision. I am working on a school project and this is my very first project and all these restrictions are project requirements. I hope to get some help here as it has been driving me crazy to figure out all this based on text files and not using sessions. I googled alot but no help. Thats why I was here in a hope to get some help.
  6. Is there any way out to check if user is logged in before user is allowed to see page content but only using cookies, no session no mysql. I am using text file to read username and password and if it matches then set cookie name "loggedin" and welcome the user. I am using login form on the same page and posting to itself, ex form on index page and posting on index page as well. How can I confirm on every other page if user is logged in using same cookie? Thanks
  7. 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.
  8. 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>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.