Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am currently working on an edit page on a website that gets the content from an HTML or PHP file and inserts it into a 'fill in the blank' (not sure what to call this) page where I can edit the content and POST it, overwriting the old file with new edited data. The data I am trying to get includes a table (like the one below) as well as an unordered list and some other random text. I have been looking around for ways to do this with file_get_contents as well as fopen(...), finding the data and inserting it into the proper html input editing spaces. This site stores the data only in the files not in a db. Does anyone have a suggestion as to what I could do in this situation?

Table Example

<table>
 <tr><th>Ingredient</th><th>Amount</th></tr>
 <tr><td>Example</td><td>Example</td></tr>
</table>

An example of the file_get_contents and something I found here which just gets a bit of text from the html file. I tried with table data but just displays it as one big string.

<?php
$fileToEditPath = $_POST['editPath']; //editPath is the full path to the file
$fileEdits = file_get_contents($fileToEditPath);

$doc = new DOMDocument();
$doc->loadHTML($fileEdits);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
    if ($div->getAttribute('id') === 'recipe-information') {
         echo $div->nodeValue;
         //Alternatively if this ends up working, echo into something more like this to POST
         echo "<input type='text' name='specificinstructions[]' value='" . $div->nodeValue . "'/>";
    }
}
?>

I do not have any ideas as to what I should try after many hours over several days looking for solutions. I apologies if this is very simple, I am quite new to working with PHP. For a tiny bit more context here is a link to another question outlining more of what I am creating and how the file I am getting data from is created.

Just as I am posting this seeing suggested tags, would this be an easier task to do with javascript/jquery? I am going to investigate that now, but any suggestions would be appreciated.

question from:https://stackoverflow.com/questions/65545494/file-get-contents-find-and-edit-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
66 views
Welcome To Ask or Share your Answers For Others

1 Answer

My suggestion would be to store the data separately from the HTML markup. I think you are making your code much harder than it needs to be by trying to parse the markup each time just to get the data. Storing the data in files would be OK, just watch out for race conditions (i.e. where 2 people make an update to the file at the same time). For that reason using a database would be better but it is not essential. Alternatively you could look for a PHP file only database library that handles concurrent updates (I don't have experience of any so cannot recommend one). There are many ways you could store the data in a file, e.g. CSV or Serialized or JSON. If the data file will only ever be opened by PHP then I think Serialized would be the easiest. NB: The filename would not have a php extension, you can use anything like .txt, .dat, etc.

To read: $data = unserialize(file_get_contents($fileToEditPath))

To store: file_put_contents($fileToEditPath, serialize($data))

This approach keeps your data separate from your markup which is good practice in my opinion. You would then use PHP normally to generate and output the HTML. This would allow you to make changes to the output HTML easily without having to worry if your edit script can still read it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...