0

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>
3
Contributors
24
Replies
90
Views
4 Years
Discussion Span
Last Post by pixelsoul
Featured Replies
  • Also, foreach ($VisitorArray as $Visitor) { should be foreach ($VisitorsArray as $Visitor) { Read More

  • 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"); // … Read More

  • 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]) … Read More

0

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.

2

Also,

foreach ($VisitorArray as $Visitor) {

should be

foreach ($VisitorsArray as $Visitor) {
Votes + Comments
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

0

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 />";
        }
Attachments Screen_Shot_2013-03-05_at_1.49_.06_PM_.png 25.42 KB
0

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

Attachments
<br />james<br /> Dean@eiu.edu<br />james<br /> Dean@eiu.edu<br />Your name goes here<br /> Your email address goes here<br />abby<br /> a@eiu.edi<br />Ashley Brown<br /> acbrown3@hotmail.com<br />Your name goes here<br /> Your email address goes here<br />Isaac Barker<br /> iabarker@eiu.edu<br />Bozo T. Clown<br /> btclown@eiu.edu<br />Your name goes here<br /> Your email address goes here<br />Your name goes here<br /> Your email address goes here<br />Isaac Barker<br /> iabarker@eiu.edu
0

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?

0

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.

0

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.

0

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>';
}

Edited by broj1

2

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
Votes + Comments
perfect, thank you for all your help and time.
0

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.

0

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!

0

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.

Edited by Atlanta15Braves: misspelling

Attachments Screen_Shot_2013-03-05_at_6.03_.12_PM_.png 133.05 KB
2

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";
}
?>
0

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?

0

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++;
    }
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.