Scruffy Nerfherder
Scruffy Nerfherder

Reputation: 189

PHP sorting an array into two columns

I have several text files, each with 4 lines of information for movie reviews.

First line is a quotation - the review. second line is the rating. A fresh icon or rotten icon, mimicking rotten tomatoes. third is the reviewers name. last is the company.

From my research, I assume I need to use an array_chunk function but I am not sure how I would implement it to fit this problem. I am still very new to PHP so I am having trouble implementing what I have found in the documentation into my code.

I am able to break down each file and display the information as needed, minus the two columns with the following code:

    echo "<div class=\"reviews\">";
    $files = glob(getFile($dir,"review*.txt"));
    foreach ($files as $file) {
    $lines = array(file($file));

    foreach ($lines as $line) {
        $rating = ($line[1]=="FRESH\n") ? "fresh.gif" : "rotten.gif";
        echo "<p class=\"quotes\"><img src='$rating'><q>$line[0]</q></p>";
        echo "<p>$line[2]</p>";
        echo "<p>$line[3]</p>";
        }
    }
    echo "</div>";

How would I go about doing this?

Also, if I have 13 reviews the first 6 should be in the left column and the last 7 in the right.

Example of reviews for Nightmare on Elm Street (5 files with four lines each)

The script is consistently witty, the camera work (by cinematographer Jacques Haitkin) crisp and expressive.
FRESH
Paul Attanasio
Washington Post


A highly imaginative horror film that provides the requisite shocks to keep fans of the genre happy.
FRESH
Variety Staff
Variety

Craven vitalizes the nightmare sequences with assorted surrealist novelties.
FRESH
J. R. Jones
Chicago Reader


Great build-up, the suspense and apprehension, getting you invested in a sequence...this movie has that.
FRESH
Chris Stuckmann
ChrisStuckmann.com


What makes the movie work is so simple and economical-you snooze, you die. I've always admired its simplicity.
FRESH
Sean Fennessey
The Ringer

Therefore this particular page would have 2 reviews on the left and three on the right. Each of these lines is one of my lines array

Upvotes: 0

Views: 123

Answers (1)

Aydin4ik
Aydin4ik

Reputation: 1935

Since I can see HTML in your code, I am assuming the two columns will be output into a browser window. So we can use HTML to add the two columns.

Without altering your original code and style too much, here is what I would do. And you would have to play around and adapt the CSS to the current environment where this code lives.

<style>
    .reviews {
      width:100%;
      border: 0;
      margin: 0;
      padding: 0;
    }
    .column {
      width: calc(50% - 2px);
      display: inline-block;
    }
</style>
<?php
    echo "<div class=\"reviews\">";
    $files = glob(getFile($dir,"review*.txt"));
    foreach ($files as $file) {
    $lines = array(file($file));

    foreach ($lines as $line) {
            echo "<div class=\"column\">;
        $rating = ($line[1]=="FRESH\n") ? "fresh.gif" : "rotten.gif";
        echo "<p class=\"quotes\"><img src='$rating'><q>$line[0]</q></p>";
        echo "<p>$line[2]</p>";
        echo "<p>$line[3]</p>";
        echo "</div>"
        }
    }
    echo "</div>";
?>

A note about PHP templating: Instead of the echoes, like above, I would employ this kind of styling for PHP templating:

<?php
    $files = glob(getFile($dir,"review*.txt"));
?>

<style>
    .reviews {
      width:100%;
      border: 0;
      margin: 0;
      padding: 0;
    }
    .column {
      width: calc(50% - 2px);
      display: inline-block;
    }
</style>
<div class="reviews">
    <?php 
    foreach ($files as $file):
        $lines = array(file($file));
          foreach ($lines as $line):
          $rating = ($line[1]=="FRESH\n") ? "fresh.gif" : "rotten.gif";
    ?>
            <div class="column">
                <p class="quotes"><img src="<?=$rating?>"><q><?=$line[0]?></q></p>
                <p><?=$line[2]?></p>
                <p><?=$line[3]?></p>
            </div>
    <?php
            endforeach;
        endforeach;
    ?>
</div>

Upvotes: 1

Related Questions