3

I am trying to create a webpage that takes user input, posts it to a creation page and then creates another webpage displaying the data in a html table. I am using file_put_contents to create the webpage and I am getting an error whenever I try to include a loop to get the content from 3 td's to be output. Here is what I have so far. I will mention I posted earlier, however I deleted that as that post was confusing, lacked detail and examples.

This is the input field for user (deleteBox does not get posted) The user will input: An ingredient, the amount and a note (txt is an empty variable)

<tr><td><input type='checkbox' name='deleteBox'></td><td>" + "<input type='text' name='ingredient[]' value='" + txt + "'/>" + "</td><td>" + "<input type='text' name='amount[]' value='" + txt + "'/>" + "</td> + <td>" + "<input type='text' name='note[]' value='" + txt + "'/>" + "</td></tr>

I've tried the below with both the for loop in a variable and directly entering it into the $strOut. I don't really know if this is how you're supposed to do it (clearly not, as it doesn't work -> gives error unexpected 'for' on line 42 [$tbl line]) and cannot find a work around. How would I make the array data display in the output for file_put_content so that it doesn't create errors?

$ingredient = $_POST['ingredient'];
$amount = $_POST['amount'];
$note = $_POST['note'];

$tbl = for ($i = 0; $i < count($ingredient); $i++) {
    echo '<tr>';
    echo '<td>' . $ingredient[$i] . '</td>';
    echo '<td>' . $amount[$i] . '</td>';
    echo '<td>' . $note[$i] . '</td>';
    echo '</tr>';
}

$strOut =   '<!DOCTYPE html>'
        .   '<table class="recipe-ingredients">'
        .   '<tr>'
        .   '<th class="table-th-large">Ingredient</th>'
        .   '<th class="table-th-medium">Amount</th>'
        .   '<th class="table-th-xl">Note</th>'
    // User input tr goes here
        .   $tbl
        .   '</table>';
    // Closing html tags after

file_put_contents("example.php", $strOut);

Any suggestions or work arounds would be much appreciated.

1 Answer 1

2
$tbl = '';
for ($i = 0; $i < count($ingredient); $i++) {
        $tbl.= '<tr>';
        $tbl.= '<td>' . $ingredient[$i] . '</td>';
        $tbl.= '<td>' . $amount[$i] . '</td>';
        $tbl.= '<td>' . $note[$i] . '</td>';
        $tbl.= '</tr>';
    }

Use this. By the way, You forgot to put a closing </tr> in the heading row.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.