I have a guest entry form that takes a persons name and email. this writes the contents to a txt file. I then want to take the text file and format it so that it is an array. I use an explode function, but my code is coming up empty. My code is below....

</head>
<body>
<h1>Visitors</h1><hr />
<?php

readfile("visitors.txt");
$VisitorsArray = file_get_contents("Visitors.txt");



foreach ($VisitorArray as $Visitor) {
        //$NewVisitor = explode("~", $Visitor);
        echo "<tr>";
        echo "<td><strong>" . $Count++ . "</strong>.</td>";
        echo "<td><strong>Name</strong>: " . stripslashes($NewVisitor[0]) . "<br />";
        echo "<strong>Email</strong>: " . stripslashes($NewVisitor[1]) . "<br />";

?>
</form><hr />
<p><a href="SortVisitorsAscending.php">Sort Visitors A-Z</a>
<p><a href="SortVisitorsDescending.php">Sort Visitors Z-A</a>
<p><a href="DeleteVisitor.php">Delete Visitor</a>
</body>
</html>

It appears you are missing a closing "}" for your foreach statement.

Also there are tags missing like <form>, closing </td> and </tr> and <table> </table> pair. But main error is as pixelsoul pointed the missing curly bracket.

Also,

foreach ($VisitorArray as $Visitor) {

should be

foreach ($VisitorsArray as $Visitor) {
commented: Thanks! didnt even catch that small error +0

Something else that won't work is feeding $VisitorsArray into the Foreach statement. It actually isn't an array, it is just the contents of the file. The foreach is expecting an array to itterate through.

(sorry, I am at work and I just keep glancing over spotting this stuff.)

Curious, how is the text file that you are attempting to read formatted?

like this?:
firstname lastname~email@address.com
firstname lastname~email@address.com
firstname lastname~email@address.com

I now have the closing added in and nothing is broken, but I am still left without any output. Click Here My code is once again below, would it have to do with those closings? I figured that I would still have output regardless.

foreach ($VisitorsArray as $Visitor) {
        $NewVisitor = explode("~", $Visitor);
        echo "<tr>";
        echo "<td><strong>" . $Count++ . "</strong>.</td>";
        echo "<td><strong>Name</strong>: " . stripslashes($NewVisitor[0]) . "<br />";
        echo "<strong>Email</strong>: " . stripslashes($NewVisitor[1]) . "<br />";
        }

like this pictures shows. Hopefully that illustrates it well enough lol

I am at work too actually. Glad we both have time to do this at work haha

I think the problem is in the text file. Can you post a few lines of it?

Here is the whole file actually. It is simply seperated by break statements and thats it.

The problem is that the format of the file and the code to convert it into an array do not match. What would you prefer: either change the format of the file to match the code or cgange the code to match the file structure?

Ouch, do you have control over how that text file is written/formatted?

So, a couple of reasons why this isn't working for you.

1) file_get_contents function only returns the contents of the file. It does not push the contents into an array, which is want to give to the foreach statement. We need to explode and push the contents of the file into an array before we hand it to the foreach statement.

2) You have an explode function in your loop that is exploding on "~" but the file is only delimited by "<br />" tags from your example. I would highly recommend if you are able, change the format of this file. CSV would really be the ideal format for this.

My file would probably look something like:
firstname lastname,email@address.com
firstname lastname,email@address.com

Or if I really had control over it:
firstname,lastname,email@address.com
firstname,lastname,email@address.com

Then I would explode first on "\n" or "\n\r" and then I could explode on the commas.

Meh, I am missing the conversation between typing that response and dealing with work stuff.. lol

probably change the code. I would feel more comfortable if it is just a line or two of code. I am also willing to change the text file though. Lets try code first to match.

It is not the most elegant solution but here you go. I am not sure if the table is formatted correctly.

// explode by <br /> and remove <br /> at the beginning and end
$VisitorsArray = explode('<br />', trim($string, '<br />'));
$VisitorsArrayLength = count($VisitorsArray);
$Count = 1;

for($i = 0; $i < $VisitorsArrayLength; $i += 2) {

    echo "<tr>";
    echo "<td><strong>" . $Count++ . "</strong></td>";
    echo "<td><strong>Name</strong>: " . stripslashes($VisitorsArray[$i]) . "<br />";
    echo "<strong>Email</strong>: " . stripslashes($VisitorsArray[$i+1]) . "</td>";
    echo '</tr>';
}

Well, I finally got left alone at work to make a quick few changes to show you how it might work if it was in a csv format. This is tested and works.

<?php
    // You don't need this btw
    //readfile("visitors.txt");

    // get the file contents
    $VisitorsArray = file_get_contents("Visitors.txt");
    // explode for each new line. 
    $VisitorsArray = explode("\n", $VisitorsArray);

    foreach ($VisitorsArray as $Visitor) {
            // now explode the string in the array on comma
            $Visitor = explode(",", $Visitor);
            echo "<tr>";
            echo "<td><strong>Name</strong>: " . stripslashes($Visitor[0]) . "<br /></td>";
            echo "<strong>Email</strong>: " . stripslashes($Visitor[1]) . "<br /></td>";
            echo "</tr>";
    }
?>

The file would look like this possibly:

John Doe,john@something.com
Tom Jones,tom.jones@something.com
Sam Parker,Sammy@something.com
The Dude,dude@something.com
Homer Simpson,h.simpson@something.com
Senor Gato,the.gato@something.com
commented: perfect, thank you for all your help and time. +0

Heh.. so now you have both solutions :)

If you could change the format to CSV it would be much more manageable. You could then use the fgetcsv function which reads a line directly into an array without you doing any coding.

Also writing to CSV is simple using the fputcsv function.

Thank you. that worked perfectly. I appreciate you taking the taking to walk me through it instead of just calling me an idiot and expecting me to understand lol. Thanks again!

I actually did get access to the file that creates the txt file. I will post it here and if you can walk me through change it so csv format that would be great, if not I can stick with the other way.

Sure, so for the code that lists the file contents:

<?php

    $VisitorsArray = file_get_contents("Visitors.txt");
    $VisitorsArray = explode("\n", $VisitorsArray);
    //$VisitorsArray = array_filter($VisitorsArray, 'strlen'); //removes empty array elements

    //echo "<pre>";
    //print_r($VisitorsArray);
    //echo "</pre>";

    $i = 0;
    foreach ($VisitorsArray as $Visitor) {
        $Visitor = explode(",", $Visitor);
        echo "<ul>";
        echo "<li><span>Name: </span>" . stripslashes($Visitor[0]) . "</li>";
        echo "<li><span>Email: </span>" . stripslashes($Visitor[1]) . "</li>";
        echo "</ul>";
        $i++;
    }
?>

Notice I commented out that line for the array_filter. You could uncomment if it was actually needed.

For the code to write to the file:

<?php
$FirstName = $_GET["FirstName"];
$Email = $_GET["Email"];

echo "<p><strong>Name: </strong>". $FirstName;
echo "<p><strong>Email: </strong>". $Email ."<br />";

$NewGuest = "$FirstName,$Email";

$VisitorsFile = "Visitors.txt";

if(file_put_contents($VisitorsFile, PHP_EOL . $NewGuest, FILE_APPEND) > 0){

// echo "Success";
}else{
// echo "failed";
}
?>

If I want to sort these, can I simply put a sort($Visitors); and a rsort($Visitors); or would I need VisitorsArray in that part? and it would go before the for statement wouldnt it?

Yeah, you would sort $VisitorsArray right after the explode. It would be pointless to sort $Visitor because it only contains to the two elements (name, email).

<?php
    $VisitorsArray = file_get_contents("Visitors.txt");
    $VisitorsArray = explode("\n", $VisitorsArray);
    //$VisitorsArray = array_filter($VisitorsArray, 'strlen'); //removes empty array elements
    sort($VisitorsArray);

    $i = 0;
    foreach ($VisitorsArray as $Visitor) {
        $Visitor = explode(",", $Visitor);
        echo "<ul>";
        echo "<li><span>Name: </span>" . stripslashes($Visitor[0]) . "</li>";
        echo "<li><span>Email: </span>" . stripslashes($Visitor[1]) . "</li>";
        echo "</ul>";
        $i++;
    }
?>

If you do not have any additional questions for this, make sure to mark this thread solved too :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.