php dynamic checkboxes - php

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.

Related

Add or Remove Dynamic Dependent Select Box data from two mysql tables using jQuery with PHP Ajax

I need complete sample code to Add or Remove Dynamic Dependent Select Box data from two mysql tables using jQuery with PHP Ajax
Dependent select data should be come these below tables. And should be add and remove button to add row.
Category
Sub Category
Below is sample data, but in this, dependent select data coming from only one mysql table.
https://www.webslesson.info/2019/09/add-or-remove-dynamic-dependent-select-box-using-jquery-with-php-ajax.html
Can any provide me sample demo with source code ?
Please this sample image
Please see this image, There are three tables, (1) Items, (2) Category, (3) Sub Category, I need same like this, but data should come from Category table and sub category table in dependent select boxes. And this is multi dynamic row. Can you give me all demo code, becuase I do not know about Jave,Ajax and jQuery.
Below is sample demo and source code weblink, But in this sample, He is getting data from only one table for both dependent boxes category and sub category. Data should be come from two tables both dependent boxes.
enter link description here
I hope you understand what I required.

How to manage other option in any listing?

I have one Category List user will select a category from the list if user didn't find a category in the list, then he will select "other" option and it will display a text box there user will enter a new category name. This newly added category will go for approval to Site Admin till then it should be mentioned as "Uncategorised".
So my question is how to achieve this using a mysql table should I create a new table as uncategorized category or should I add one extra column to category table as "isApproved". As the solution should be for both add and edit of new category.
As you described
Admin must approve newly added category.
To acheive that you definitely will have a field status or something like that to check if this category is approved or not. You can simply use that field. If it is not active, it is "Uncategorized.
A more flexible way than adding one isApproved (or rather a more generic name like status) column to your table is to create a whole new temporary table. There are number of reasons why this is the better approach:
You can save diagnostical information like who created this category, when did he create it, and so on.
You separate your logic: An unapproved entry is, simply put, a temporary one. Approving it is nothing more than moving it over to your categories table and thus making it permanent.
Your categories table doesn't get clustered with unnecessary entries.

How to match specific ID to list of ID's

Not sure my title is definitive enough, but here's my dilemma.
Firstly, I have two database tables.
The first one lists all specialitities like this:
ID, Description
The second table stores a practitioner ID with a Specialititiy ID from the above table.
So I have say table 1 with 100 specialitities listed in it.
And in table 2 I have say Practitioner ID 1 with say 5 of those specialitities.
Example:
Practitoner ID, Specialitiy ID
123,31
123,45
123,67
123,68
123,81
Now, I want to edit those through a form.
I can list all the specialities through a loop through an array of all the specialitities, including a checkbox adjacent to each speciality.
But how do I check the box matching those specialitities listed for the practitioner?
I have a PHP function that gets all the specialitities from the specialitities table and I then loop through that array to list them all, with a checkbox for all of them.
I also have a PHP function that gets all the specialitity ID's for a specific practitioner.
Is there a method of matching the ID of the speciality from the practitioners table with the speciality listed on the form, and checking the box?
I'm not sure I've made myself as clear as I should here, but it might make sense!

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?

how to add/edit/manage related data in multiple tables

I have several tables that i need to create an admin interface for
Table 1
Table 2
Table 3
Table 4
Table 5
each table's contents are reflected of which parent it belongs to in a field...so
table 2, has a field for which row in table 1 it relates to, table 3 has a field for relating to table 2 and so on.
Whats the best way to present this to the user so they dont have to memorize id numbers. Say for instance, they want to add a new entry in table 3, they must select which row in table 2 to link to.
The relationship only extends one level above after table 1. So creating a new set of options in table 5, would involve creating an entry in table 1, new entry in table 2, linking it to table 1, new entry in table 3, linking it to table 2, new entry in table 4, linking it to 3, and finally the new option in table 5.
So my question is user-interface related as to the best way to present this to the user. Alternatively, what is this kind of system called, so i can search for other examples.
Thanks.
From what you're saying, it looks like it's not possible for one entry in a table to exist without the corresponding entries in the other tables, right?
In that case, you could present a wizard like interface which prompts the user to enter the data for each table on each new wizard page, starting from the data for table 1. Then once all the data has been collected, you can fire off a series of update statements where the id of the record in table 1 is reused for the inserts in tables 2-5. (Depending on your table design, you might get this id using LAST_INSERT_ID()).
Or if you don't want to use multiple insert statements, you might use an updateable view (if you're using MySQL 5).
Alternatively, you could have a table structure where a record in table X must have a linked record in table Y (where Y < X). In other words, a record in table 1 may not have a linked record in table 2, but a record in table 2 must have a linked record in table 1.
In this case, you can still use the wizard, but you can start with the table that you actually want to create information for, then have the wizard prompt for data for the previous table, and so on until it prompts for data for table 1. So if you really want data to be created for table 4, have the wizard prompt for that first, then prompt for data for table 3, and so on up to table 1. Then do the data entry as before.
Sounds like a problem ready-made for my old favorite, the master-detail layout. Each table is represented in a separate pane (Panes 1 through 5) displayed in the same window. The panes are arranged top-to-bottom and/or left-to-right for Tables 1 through 5 sequentially. In each pane, there is always exactly one “active” record, which is marked for the user in some way. The contents of each pane are determined by the active records in the panes above/left of it. Thus, Pane N shows the Table N records for the active record of Table N-1 shown in Pane N-1. The active record in a pane is whichever record in the table last had focus (in any field). Thus, for example, the user can display the Table N records for a particular record in Table N-1 by clicking on any field for that record in Pane N-1. Queries for Pane N are launched asynchronously once the active record changes in Pane N-1 (no “Refresh” button, please). All non-read-only fields should be edit-in-place. A Save button or menu item inserts/updates all records of all tables as a batch (alternatively, you can fire an Update whenever a field is edited and focus leaves it, if your bandwidth can handle it).
Thus, to add a new record to Table N, the user places focus on the correct records for Table 1 through Table N-1, then places focus in Pane N (e.g., click anywhere in it) and selects the Add Record menu item. This inserts a new blank record in Pane N for the user to complete. The user can continue to select Add Record to fill Pane N with the desire records for the active Table N-1 record. At any time, the user can use the same process to add Table N+1 records for a newly created Table N record by clicking on Pane N+1 and selecting Add Record. Also, at any time, the user can edit the fields of the records in any pane. (Alternatively, you can have separate Add Record menu items for each table, which saves the user having to shift focus to a pane to add records, but that many menu items can get more cluttering than its worth; another approach is to always have a blank record in each pane ready for the user to fill out, eliminating the need for the Add Record menu item).
This design provides the users with the most flexibility, allowing them to add, delete, and update records in any order convenient for any table at any time. Because the window “remembers” the active records between edits, it’s also very efficient, eliminating the tedious re-selecting of Table 1 through N-1 records to edit a series of Table N records for a particular Table N-1 record (unlike, say, a wizard). It displays all the records for all tables in a single window in an intuitive hierarchical layout that facilitates viewing and exploring the data and minimizes navigation among windows or pages (again, unlike a wizard).
Five panes is a lot for a single window, but not too much. However, it would help if you provided easy ways for users to resize and hide/show each pane. Thus, if the user needs to work on a bunch of Table N records of a record in Table N-1, she or he can hide all the other panes, expanding Pane N to full window size to minimize scrolling. Also, if the user does not ever need to study or edit the records of some of your tables, do not put them in panes of their own, but rather make each appear as a field for the adjacent table in the structure. For example, if the users aren’t allowed to edit Table N, then instead of Pane N you can have a field in Pane N+1 that displays the functional name of the Table N record that each Table N+1 record belongs to. By making it a drop-down list, user will be able to assign/re-assign the Table N record for any Table N+1 record.
Details at http://www.zuschlogin.com/?p=31.
More stuff at Stack Overflow than may be relevant:
Hierarchy Visual Design
UI design pattern for multi level grid
What’s best when inserting into a table view, and add button or a blank line?

Categories