0

I am newer to PHP and I am able to get the desired output but I am doing it one index position at a time. I am returning data from a .txt file and I need to insert this data into an HTML table I am creating using PHP. BUT FIRST I need to be able to get the same output without typing out every index position. I tried to use a forloop but it kept outputting only one line. I manually outputted the lines from the file and it works. What loop in PHP would be best to achieve the same results and output these elements? IMPORTANT, as is I am able to sort and rsort (I want to be able to do this so if it can be implemented in the loop that would be awesome) any help is more than I have right now.

PHP

$books = array();

 if ($fp)
{
    while(true)
    {
        $lines_in_file = count(file($filename));
        $line = fgets($fp); 

        if (feof($fp))
        {
            break;
        }
  $line_ctr++;

list($title, $author, $pubdate, $isbn) = explode('*', $line);

    $new_line = explode('*', $line);

    for($ii= 1; $ii <= $lines_in_file; $ii++){
        $lines = fgets($fp); //Read each line
        $member = trim($lines);
        array_push($books, $member);   
    }

    //This foreach only brings back the first like in the txt file, why?
 $cntr = 0;

    foreach($books as $element){
        $cntr++;
        $table .= "<tr>";
        $table .= "<td>".$title."</td>";
        $table .= "<td>".$author."</td>";
        $table .= "<td>".$pubdate."</td>";
        $table .= "<td>".$pubdate."</td>";
        $table .= "<td>".$isbn."</td>";
        $table .= "</tr>\n";  //added newline
        echo $element;
    }

  //sort($books);
   // rsort($books);
    echo $books[0];
    echo "<br>";
    echo $books[1];
    echo "<br>";
    echo $books[2];
    echo "<br>";
    echo $books[3];
    echo "<br>";
    echo $books[4];
    echo "<br>";
    echo $books[5];
    echo "<br>";
    echo $books[6];
    echo "<br>";
    echo $books[7];
    echo "<br>";
    echo $books[8];
    echo "<br>";
    echo $books[9];
    echo "<br>";
    echo $books[10];
    echo "<br>";
    echo $books[11];
    echo "<br>";
    echo $books[12];
    echo "<br>";
    echo $books[13];
    echo "<br>";
    echo $books[14];
    echo "<br>";
    echo $books[15];
    echo "<br>";
    echo $books[16];
    echo "<br>";
    echo $books[17];

}//END WHILE LOOP
fclose($fp ); //Close file
}
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49
  • Possible duplicate of [How to read a large file line by line](https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line) – Ryan Kozak Sep 21 '18 at 00:58
  • @ IdontDownVote, 1. Thank you for your genuine answer. 2. Would you be able to demonstrate the forloop and integrate an HTML table as well? – CodeConnoisseur Sep 21 '18 at 01:10
  • If I understand your question, you want to use a `foreach` loop. – miken32 Sep 21 '18 at 01:17
  • When I run my code it is only bringing back the first line of the text file. Any ideas why? I used a foreach loop. – CodeConnoisseur Sep 21 '18 at 01:17
  • @mike32, Yes sir, I edited my code and added a foreach loop but for some reason it is only bringing back the first like of my .txt file over and over again instead of populating my table with each line in the .txt file. Any ideas why? – CodeConnoisseur Sep 21 '18 at 01:21

1 Answers1

1

Having to make a few guesses here but i believe the file is going to look like:

title*author*pubdate*isbn
title*author*pubdate*isbn
title*author*pubdate*isb

if this is wrong, let me know

as long as the fie is not to large read it in to an array:

$book_array=file('book_file.txt');
//print_r($book_array); //is it an array of the books, one book per array key

now to separate each line:

foreach($book_array as $line){

    $line_sep=explode('*',$line);

//    with no sorting option you could just echo inside the loop
  //if you want to keep sorting options we have to keep the separated lines

    $new_book_array[]=$line_sep;
}

unset($book_array);//free up memory if needed
//print_r($new_book_array);//is it a multi-d array, book then the 4 elements

to sort our new multidimensoanl array:

usort($new_book_array, function($a, $b) {
    return strcmp($a['0'], $b['0']);;
}); //this sorts on key 0 in your case title:

//print_r($new_book_array); // has it sorted properly

for display: loop again

echo '<table><tr><th>Title</th><th>Author</th><th>Pub Date</th><th>ISBN</th></tr>';

foreach($new_book_array as $book){
//print_r($book);  //is it each indervidual book array with 4 elements
      echo '<tr><td>'.$book[0].'</td><td>'.$book[1].'</td><td>'.$book[2].'</td><td>'.$book[3].'</td></tr>';
}
echo '</table>';