I have just started with Zend Framework, so might be a bit of a silly question coming up.
I have a Form with 5 Checkboxes. A User can click on as many checkboxes as needed. This gets entered into the Database. (1:n)
This all works fine. But now I am gotten to the part where a User can Edit the Post. The Checkbox gets shown as well as which once are active. But how do I update this now?
The Rest of the Post Update works fine, collecting the Data and send an update:
$this->getDbTable()->update($data, array('post_id = ?' => $id));
Now I want to update the Checkboxes, the 1:n Relationship. But how would I do that if I for example had 4 Checkboxes active but after the update I want only 3 active? Should I delete all entries first and than do a normal insert or is there a trick to do it?
Hope someone can help. Thanks !
Assuming that the row existing in your database is what you use to indicate that it should be checked, you will need to delete it as the update() function won't do that.
If delete / re-insert is going to be too expensive of an operation, perhaps store the checked values in the session and do an array_diff($previous_selected, $currently_selected) to get the list of items to delete.
One thing to note is that the formCheckbox view helper by default creates a hidden input with the same name as the checkbox with a value of 0.
Related
I've just started recently to delve into the depths of html/php & co, so basically I'm a noob.
My question is simple: I'm using a php session to store a variable I'm using for an operation on the successive page. This variable is 'id' which is the primary key of my postgreSQL table. The user has a form on the web page in which it inserts the 'id' so as to select a particular row of the database to visualize and modify. Since the operations of visualization and modification are all in successive pages, I wanted the variable to remain memorized through the session (so that it's not required to post it again every time)
$_SESSION[id] = $_POST[id]
The problem arises when on a successive page I try to use a form to modify data on that particular row. I don't want the user to have to submit the 'id' again, so I use a select that involves $_SESSION[id]. But when the user submits the form, $_POST[id] is not specified in that form and its value goes to null. The result is that $_SESSION[id] is null and I cannot access the database using the query.
I found a way to solve the problem, but I find it extremely inelegant. I basically add a hidden section on the form that sends the id variable like this
<input type="hidden" name="id" value="$_SESSION[id]">
Is there a better way to do this?
I really hope my question makes sense to you, feel free to comment for additional info.
Thanks a lot :)
I'm using chronoforms 5, made a form for creating a record. It works all fine.
But my problem is I need a form for edit the record. I've no idea how to load & show the record data. Anyone has ideas?
You have to be able to identify the record you want to edit. Usually this is by the Record ID, the User ID, or possibly the User email if that is unique. Then you can make a copy of your entry form, rename it, and add a DB Read action at the top of the On Load event in the Setup tab - if you are using the Simple Wizard then you have to switch the form to Advanced mode in the General tab to do this.
Edit the DB Read action to link to the table and set the Conditions box to load the record you want to edit - there is a FAQ about doing that.
You should be using ChronoConnection to list the records. The user selects a record which then displays the form to edit it.
I'm just diving into Laravel this week. I've done a few tutorials and have been able to figure out how to setup my database, loop through my records and display them in a view. With each record I am displaying a checkbox and each is set to checked="checked" based on a boolean field from the "CheckedIn column in the database. I want to be able to uncheck multiple records then submit and update that column in the database then refresh the page showing the changed records as unchecked. Just looking for a jumping off point or links to resources that explain this process better. If I could figure out how to do it via javascript and AJAX that'd be even better.
You need to post up more information for people to help you.
Your process would be something like this:
Create the table with a form, with all the checkboxes named with the ID of the row in the DB table
->
Submit the form
->
Get all the inputs for the checked boxes
->
loop through and see whether it is checked or not
->
Update that row in the database
->
Redirect back
Ok, I'm pretty sure I'm not the first one to run into this, I just don't know the right words to search for a solution.
Consider the following database tables (simplified for this question):
database tables http://www.nth-root.nl/public/images/wolf/cat_attr.png
As you can see in the picture, there is a category table that holds product categories and an attribute table that holds different attributes. For instance: Color (for clothing), Capacity (for mp3 players), etc.
The third table, in the center of the picture, links the attributes to the categories.
Now in the backend I have an 'Edit category' page with a form to edit (or add) a category. On the same page I want to display a list of dropdown lists in order to manage which attributes are linked to the current product category.
backend http://www.nth-root.nl/public/images/wolf-commerce/edit_category.png
It uses jquery to add / delete dropdowns.
The problem is: how do I manipulate the data in the category_attribute table, using the $_POST data of the form.
The lazy solution would be (and yes I've done this in the past):
1. Delete all current rows in category_attribute for the given category
2. Then insert all selected attributes to category_attribute
But this isn't very efficient, it deletes and re-adds all rows even if someone only changes the category's title.
So I'm looking for a way to do the right inserts, updates and deletions:
1. Delete the rows that don't appear in $_POST (the dropdown has been removed)
2. Update the rows that are changed (another option from the dropdown has been selected)
3. Insert rows that are new (a new dropdown has been added)
I'm just not sure how to do it or where to find a good example. It's probably easy to do with some ajax but I am looking for a solution that's purely php, comparing the $_POST values with the values in the database.
If someone knows a good article that explains the best (and most efficient) solution, please let me know, I'd be very grateful.
-edit-
I had included some pictures to clarify my question, but stack overflow refused to include them because my reputation is too low, so I hope my question is clear enough without the images.
-edit2-
Now I have enough reputation, I've added the images.
Thanks in advance,
Nic
Insert and Update:
You can insert a hidden input with the ID records.
Those who are new will reset this ID, so you do the insert.
The ID's that have filled you make an update. If you only want to perform the update really changed the items you can create a hidden input "UPDATED" with value "0". Via javascript you can check if the combo has changed, so that input marked as "1".
Exclusion:
Likewise you can do to exclusion, creating a hidden input scoring records that should be deleted when the user clicks to remove.
Do not know if it's the appropriate way for you, or if you expect a more practical solution.
I'm simply trying to implement an undo button using PHP and MySQL, for example if a user deletes a post or any general item there will be an option to undo that MySQL query and leave the post as it was. Thanks in advance.
You need a flag column to do thing. For example, the del_flag, if the user delete a record, set this flag to 1, when undo, set it to 0.
For clarification, once a row is deleted from a MySQL database, it can't be "undone". There's no going back once an item has been deleted.
So, to mimic this "undo" functionality, you need to (as already suggested in another answer) create a new field in the database table that keeps track of whether a post has been deleted or not. Let's call this new field Deleted and say it can have only two values -- yes (meaning it has been deleted) and no (meaning it hasn't been deleted).
When a post is deleted, you can mark that Deleted field as yes and "hide" the post from view so that it appears to have been deleted (even though it actually is still present in the database).
Likewise, when a user click on the "undo" button that will switch the Deleted field back to no.
In your PHP code, you'll want to check the value of the Deleted field for each post, and then hide or show the post accordingly.
As with any challenge like this, there are multiple solutions. If the above approach isn't what you're looking for, and you really want to actually delete the post from the database, then you could save the deleted post in another database table first (and keep a reference to it, of course, so that you can restore it if the "undo" button is used), and then delete the post from the "main" database.
When you delete the post just mark it as not 'live'. Then to undo just mark it as 'live' again.
Then in your queries just filter results based on if they are 'live' or not.