Yii CGridView bulk edit/update? - php

I want to edit/update, multiple rows together in Yii CGridView.
Say, each row has a checkbox and a single edit/update button, I select multiple rows and by clicking on edit/update button all selected rows are opened for editing and update.
Is this possible with Yii CGridView..??

Use the selectableRows property of the CGridView.
According to Yii Documentation for CGridView
public integer $selectableRows;
the number of table body rows that can be selected. If 0, it means rows cannot be selected. If 1, only one row can be selected. If 2 or any other number, it means multiple rows can be selected. A selected row will have a CSS class named 'selected'.
You can use the the $.fn.yiiGridView.getSelection(containerID) to retrieve the key values of the selected rows.
Add some more buttons either at the top or bottom of the gridview with actions for edit, delete or whatever action you need to take and pass the key values retrieved above.

If you want to edit rows in-line you can use extension phaActiveColumn which I messed up to support multiple rows (the whole table if desired).
EDIT: well, the extension basically creates an input field replacing the grid cell when user clicks on the cell. The field will have the cell's value for starters and it then sends an ajax call to controller, as soon as the user presses enter, asking to save the value of the field, which will be the user's input.
My addition was to create one such field for every cell in the row and store user input in javascript objects which then get send all at once with one ajax call to the controller. Multiple row updates are supported (creating adequate number of javascript objetcs).
If anyone has any interest in this there it is.
phaEditColumn2
phaAbsActiveColumn2

Related

How to get the values of the table columns while adding the rows

I have a table which can add up rows (insert row() method in js) when clicked on a button.
First column has a select tag which is populated with the product names from data base. Second column has a input text box for quantity. Next is a button. When I click on the button it should multiply the price of the selected item from first column with the quantity and set the answer to the next column i.e price.
This is working for only one row. When I add new row and click the button for setting price, it will take the value of first row itself.
This all should be done on application level on client side with proper server side checks on submission. You shouldn't need database CRUD at this point.

Adding and removing rows on a CGridView Yii

I have a search form that the user uses to find persons from the database, the results are displayed on a table, then the user can choose persons from the results table and add them or remove them from another table (the CGridView) and this last table will be saved to the database when the user clicks save button. My problem is that I'm adding and removing the persons (the rows) with jQuery, so when I use the sort function of the CGridView, clicking any header, the rows that were added with jQuery are lost. Also, when I remove rows from the last table I want to refresh the table so the zebra style rearranges. How can I do this without losing the data added with jQuery?

fill textbox with values based on selected table cell value (ajax call & mysql)

I have a table which is populated by values obtained from mysql database. I want to use an ajax call function so that when I click a cell - based on the cell value the textboxes get filled. Now the data in the textboxes will be from another table in the mysql database.
So an example of how it would work is:
I have one table for credentials, which shows user name and their user id.
On the website this is shown as a table. Now when i click the a cell which has a user name. The textbox would be populated by the users say total exams taken which is obtained from exams table in the mysql.
I am totally stumped on how to go about doing this. Please can someone help!
You can do this by using ajax .
use this
$("#cell").click(function(){
$.get("file.php?id="+$(this).val(),function(data){
$("#textboxid").val(data);
})
})
the file.php is the file which will perform the back-hand calling to database and make a output.

PHP edit unique row in table

I currently have a PHP form that uses AJAX to connect to MySQL and display records matching a user's selection (AJAX: Display MySQL data with value from multiple select boxes)
As well as displaying the data, I also place an 'Edit' button next to each result which displays a form where the data can be edited. My problem is editing unique records since currently I only use the selected values for 'name' and 'age' to find the record. If two (or more) records share the same name and age, I am only able to edit the first result.
Let's assume your file for editing is edit.php. Then, in the file where you generate the edit links, try changing your edit button link as follows:
'edit'
Then you will be able to access ID variable as
echo $_REQUEST['ID'];
Note that the ID is case sensitive. Let me know how it goes.
when displaying records from ajax, also send the primary field(id in most cases) along with name and age
and when u are displaying these data along with edit incorporate that primary field with edit

php dynamic checkboxes

Currently I have a form that submits an image with textfields such as
title, description and another field that autoincrements for imageID, another
area for the actual file , called vfile, and *** another part that has
3 checkboxes and a text field.
Everything works fine, and this is what it does. Submits the data to a database so that it can pull the information to a page on the website.
The only part I am trying to update is:
The 3 checkboxes and the textfield.
Lets say the first checkbox reads: Apples
The second : Oranges
The Third: Grapes
And in the other category is a blank textfield that if you add something, it would add it to a category called "Other".
So the database design has 4 fields: 1 - apples, 2 - oranges, 3 - grapes, 4 - other.
When I click a checkbox, it would add checked to the database under the correct one, either apples, oranges, or grapes.
If I add a field to the textbox such as: Bannanas, then it would add "Bannanas" to the database field vother and show that in the database.
This is all fine, but what if the next picture has all 4 items, plus another one? Such as if the next picture had Apples, Oranges, Grapes, Bannanas, and Plums?
How could I have the "Bannanas" other category, change into a checkbox category that could be chosen for the next pics when I go to the add images page next time.
So that when I go to the second picture to submit, it would give me the option of not just 3 checkboxes, but 4 checkboxes now, that I could check the first 4, "Apples, Oranges, Grapes, Bannanas" and then put Plums in the other category.
Basically upon submit it takes what is in the other feild and addes a new category to the database, which is then displayed in the array of checkbox choices and it is removed from the Other Category now, for it is a checkbox. (thus it would not want the value left in the old field, for it would keep creating the same category over and rewriting the old data possibly.
Anyway, any suggestions?
Thanks in advance.
(It sounds like this is more of a database design question and not a php question, but I may be misunderstanding what it is you are looking for advice on)
It sounds like you are saying that these attributes (Apples, Orange, etc) are stored as columns in your main table; but the situation you are describing sounds more like Tagging. Typically you would maintain a list of things that get tagged (your images), and a separate list of all possible tags (Which would be a table containing the rows : Apple, Orange, Grape). Your UI has the option to select from pre-existing tags (rows in the tag table) or add a new tag using the "Other" box. New tags would be added as a new row to the tag table. Since tags and tagged items have a many-to-many relationship you would create a third table (called a join table) that stores keys of tagged items and keys of tags; that way you can select either side of the relationship easily : get all the tags for a given item; get all the items with a given tag.
Does that help?
(EDIT : for comments)
So, Activities sounds like the list of Tags. If I want to show a form with checkboxes for all the Activities I can query the activities table for them. Each of those checkboxes can have a name attribute or something that captures the ID of the row that its bound to.
Also I would select from the join table the ids of the tags that my currently viewed image has selected. As I am populating the checkbox list I can check this result set to see if the id of the checkbox I'm putting on the page is in the list of tags for the image.
To store this back to the db on submit, the easiest thing is probably to (in a transaction) delete all the entries for the image from the join table and replace them with new entries based on the state of the check boxes in the form.
Drop the apples, oranges and grapes columns.
Create a second table with two fields: imageID and itemtype.
Don't make any of the two a key. Now you can list as many different types of items for each image as you need. It will be comparatively expensive to get the list of item types in use from the database but unless you have millions of items this shouldn't be a problem. (Mikeb is suggesting to keep the list of item types in use in a separate table to speed this up.)
The alternative is to dynamically add a column to the first table for each item type you encounter. This will create a very sparse table. I wouldn't do this.
You need to change your database schema to support an infinite number of attributes.
You should move your list of attributes from a set of fields in the record to a list of related records in a new table. This new table requires 2 columns. The first is the primary key from the existing table so you can establish the relationship between the tables. The second is the attribute field ('Bananas' or 'Apples' or 'Plums', etc.) You can have as many records in the attributes table as you like for each record in your main table. You could also have no attribute records if none are checked.
This kind of relationship between two tables is called a one-to-many relationship.

Categories