简体   繁体   中英

Creating New Table Row After Third Iteration in ForEach Loop PHP

I have made a guestbook form which submits data to a .txt file using fwrite(). Then in a separate .php file I open the .txt file, explode the data, and then use a foreach loop to display the items in a table.

That's where my problem lies, I have managed to make all of my data items appear in new rows all held in the first column of my (3 X Infinite due to it being built by users signing the guestbook) table.

I know there is the for() and if() method using counters and the modulus - but I can't get their placement right. Either I accidentally cause nothing to display in my table or all of it in a single row, all of it in a single cell. I've found lots of examples of how to do this with a declared array but I can't figure out how to apply that in a dynamic sense.

My code:

    <?php echo "<h3><center><u>Review What You've Posted</u></center></h3>";
    ?>

    <?php
        $firstName = $_POST['firstName'];

        echo "<u>Your Name:</u> " .$firstName. "<br><br>";

        $yourEmail = $_POST['yourEmail'];

        echo "<u>Your Email:</u> " .$yourEmail. "<br><br>";

        $yourComments = $_POST['yourComments'];

        echo "<u>What You Thought:</u> " .$yourComments. "<br>";

        "<br>";


        $guestBook = fopen('guestBookData.txt', 'a+');

        $outputstring = $firstName . ':' . $yourEmail . ':' . $yourComments 
    . ':' . "\r";


        fwrite($guestBook,$outputstring);
        fclose($guestBook)

    ?>
    </main>

This is called by:

    <main class="Main Body BG" id="wrapper">

    <table style="width:100%" border="3" cellpadding="3">
    <caption>Previous Visitors</caption>
    <br>
    <tr>
    <td bgcolor="#FBB2EA" align="center"> Name </td>
    <td bgcolor="#FBB2EA" align="center"> Email </td>
    <td bgcolor="#FBB2EA" align="center"> Comments </td>
    </tr>

    <?php 

    $guestBook = fopen('guestBookData.txt', 'r'); 

    if($guestBook){
        while(($dataString=fgets($guestBook)) !==false){

            $dataField = explode(':', $dataString);

            foreach($dataField as $df){

                echo "<tr><td>".$df."</td></tr>";

                }   

    }

    fclose($guestBook); 

    }

    ?>

    </table>

    </main>

A couple of things I'd suggest. Don't use plain fwrite for that. You're implicitly assuming that : can't be part of your input which is not a reasonable assumption to make. Use the following:

 $guestBook = fopen('guestBookData.txt', 'a+');
 $outputarray = [ $firstName, $yourEmail, $yourComments ];
 fputcsv($guestBook,$outputarray,$delimiter); 

Second thing is you're making a row for each field, you should do this:

 <main class="Main Body BG" id="wrapper">

<table style="width:100%" border="3" cellpadding="3">
<caption>Previous Visitors</caption>
<thead>
<tr>
<th bgcolor="#FBB2EA" align="center"> Name </td>
<th bgcolor="#FBB2EA" align="center"> Email </td>
<th bgcolor="#FBB2EA" align="center"> Comments </td>
</tr>
</thead>
<tbody>
<?php 

$guestBook = fopen('guestBookData.txt', 'r'); 

if($guestBook){
    while(($dataField=fgetcsv($guestBook,0,":")) !==false){ //CSV style reading here too
      echo "<tr>"; //Open row
      foreach($dataField as $df){
          echo "<td>".$df."</td>";
      }   
     echo "</tr>"; //Close row
}

fclose($guestBook); 

?>
</tbody>
</table>

</main>

You can read more on fputcsv and fgetcsv

**Added some table semantics as well like thead and tbody as well. Those are not required but are nice to have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM