0

Say I had the following:

$stmt = $pdo->prepare("SELECT jobName, jobDesc FROM Jobs");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $jName = $row['jobName'];
    $jDesc = $row['jobDesc'];
    print '<span class="job">Headline:</span> ' . $jName . '<br />
           <span class="job">Description:</span>
           <p class="job">' . $jDesc . '</p>';
} 

The HTML output would be (for testing purposes)

<span class="job">Headline:</span> test 1
<span class="job>Description:</span>
<p class="job">test 1 Description</p>

<span class="job">Headline:</span> test 2
<span class="job>Description:</span>
<p class="job">test 2 Description</p>

<span class="job">Headline:</span> test 3
<span class="job>Description:</span>
<p class="job">test 3 Description</p>

If I wanted to remove one of these entries from the page, but keep it in the DB, how would I accomplish this with PHP? I'd probably have to put a link or button on each entry to remove it, and I know I can't actually take it off the page permanently with jQuery unless I use something to save the DOM state.

Thank you

3
  • 2
    I'd suggest adding a flag to the row in the DB along the lines of SHOULD_DISPLAY, and then set the flag for rows you don't want displayed and filter them out in that query (or at display time if for some reason that makes more sense). Commented Jun 8, 2013 at 21:36
  • 1
    @MattWhipple consider adding that as an answer, you deserve the rep. Commented Jun 8, 2013 at 21:39
  • Thanks...done. I wasn't originally sure if I was answering the right question. Commented Jun 8, 2013 at 21:44

2 Answers 2

3

I'd suggest adding a flag to the row in the DB along the lines of SHOULD_DISPLAY, and then set the flag for rows you don't want displayed and filter them out in that query (or at display time if for some reason that makes more sense).

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

4 Comments

One more question: my DB is quite small. Would your answer be better suited for a small DB, or is REST/CRUD still more efficient?
I'm not sure what you mean. REST/CRUD aren't ideas that affect this solution. If you want a persistent means to indicate that particular objects should exist in the DB but not be displayed on a page then a flag is the option. If it's something that can be expressed as a logical rule then it could also be done as a filter using that logic...
using JS code would make sense to make the initial selection more responsive, or if you're looking to do some kind of per-user filtering in which case you could use local storage and keep most of the logic in the front end. Although then you'd likely also need back-end persistence if you expect the user's display to be consistent among different devices/clients. In THAT case, you'd probably want to store a list of ids to exclude for that user. The right solution comes down to exactly what you need. The size of the DB shouldn't matter, do the simplest thing that works for you right now.
And more point..."my DB is small" means that the most efficient solution is the one that takes you the least time. "Premature optimization is the root of all evil." (Knuth)
1

There are different ways to accomplish this.

You could create a RESTful controller in PHP to implement basic CRUD operations, and then use jquery to delete/update each html element from your DOM and your DB.

Or you could just add a flag field to your database table, for example called "deleted", that can be true or false and let your php script render the row if it was not previously marked as deleted.

1 Comment

I have no experience with REST or CRUD, but I think I might try to use it because I need to learn it anyway.

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.