Reputation: 21
I'm trying to add a table row generated in PHP to a HTML file. Actually I've done this with a simple HTML form and some PHP code, but I would like the new row to be added at the top of the table, not at the bottom... (It's going to be a to-do list for my homework, but without checkboxes.)
Here's the PHP code:
<?php
$file = fopen("index.php","a+") or exit("Unable to open file!");
$since = $_POST["since"];
$since2 = "<tr><td class=\"since\">$since</td>";
$due = $_POST["due"];
$due2 = "<td class=\"due\">$due</td></tr>\n";
$user = $_POST["user"];
$user2 = "<td class=\"content\">$user</td>";
if ($_POST["since"] <> "");
{
fwrite ($file,"$since2$user2$due2");
}
fclose($file);
?>
Can anyone help me? (Yeah I know the code isn't clean because it's my first attempt to write PHP.)
Here's an example of a tr
made with the code:
<tr><td class="since">Thu 22th Nov</td><td class="content">example</td><td class="due">Tue 27th Nov</td></tr>
My main point is to have a new tr
added on the top!
Really appreciate any help! (I've looked around and hope this question hasn't been asked yet.)
Upvotes: 2
Views: 3152
Reputation: 76395
A bit shorter, but this should do the trick (untested)
<?php
if (!emtpy($_POST['since']))//check using empty, it checks if postvar is set, and if it's not empty
{//only open the file if you're going to use it
$file = fopen('index.php','a');//no need for read access, you're not reading the file ==> just 'a' will do
$row = '<tr><td class="since"'.$_POST['since'].'</td>
<td class="due">'.$_POST['due'].'</td>
<td class="content">'.$_POST['user'].'</td></tr>';//don't forget to close the row
fwrite ($file,$row);
fclose($file);
}
?>
BTW, your if
statement doesn't quite cut it:
if ($_POST["since"] <> "");
{
in PHP !=
and !==
are the operators you're looking for (is not equal too), and an if statement isn't followed by a semicolon.
You're also assigning a lot of post variables to a new variable, just to concatenate them into a string, there's absolutely no need to do that:
$newString = 'Concat '.$_POST['foo'].'like I did above';
$newString = "Same ".$_POST['trick'].' can be used with double quotes';
$newString = "And to concat an array {$_POST['key']}, just do this";//only works with double quotes, though
Update:
to answer your question in the comments below: here's what you need in order to parse the html, and append/prepend the desired elements:
$document = new DOMDocument();
$document->loadHTML(file_get_contents('yourFile.html'));
//edit dom here
file_put_contents('yourFile.html',$document->saveHTML());
Spend some time here to find out how you can add/create/alter any number of elements in the DOM. Methods that might be of interest to you are: createDocumentFragment
, createElement
, and all JavaScript lookalikes: getElementById
, getElementsByTagName
, etcetera...
Upvotes: 2
Reputation: 2780
To add it to the beginning of the file, you can read its contents, add it to the end of your new row, and then write the whole thing back to the file.
// read the original file
$original_list = file_get_contents("index.php");
// use the writing mode to overwrite the file
$file = fopen("index.php","w") or exit("Unable to open file!");
$since = $_POST["since"];
$since2 = "<tr><td class=\"since\">$since</td>";
$user = $_POST["user"];
$user2 = "<td class=\"content\">$user</td>";
$due = $_POST["due"];
$due2 = "<td class=\"due\">$due</td></tr>\n";
if ($_POST["since"] <> "");
{
fwrite($file,"$since2$user2$due2$original_list");
}
fclose($file);
This is not the optimal way to build a to-do list, but we don't know enough about your homework and the requirements to further improve the answer.
Another tip: Avoid using meaningless variable names like $since2
and $due2
, even if they are temporary in nature. By using better names like $since_cell
and $due_cell
, the code becomes much easier to understand, even without comments.
Upvotes: 1