Force refresh data before posting to database - php

The problem is :
There are two pages , one for editing name records and one for adding name records.
If I first open an edit page,then I open an add page , and input a name in add page
before I submit the name, I use the edit page to change the name,
(say change from varChar to integer type)
,after that, return to add page and submit the name.
Should this cause an error? How can I prevent this error?

In most web applications that I have used, the first valid request to be processed wins. I think you need to clarify your question, though, as it doesn't sound like the situation you describe would be possible, or it would not be a problem.
It sounds like NAME is a field in a record, not an entire record on its own. It doesn't seem like you could change the NAME field and add it at the same time. Let's imagine you have a record type PERSON that is backed by a table in your database. Each PERSON record has a NAME field that is currently of varChar type. It sounds like you are using a web interface to edit the structure of this table.
If the table already has a field NAME, the add page submission will report an error because a field called NAME already exists. The edit page would succeed, assuming it is possible to convert the NAME field to the integer type: either there are no records, or the value of NAME in every record can be converted to an integer.
Another interpretation of your question is that you are changing the NAME field to be something else entirely in the edit screen, perhaps from NAME varChar to AGE integer. You also happen to use the add page to add a new NAME char(10), for example. This would be allowed as long as the edit page submission is processed before the add page submission.

Related

How to create dynamic field for a database using PHP

I have a multi-client level PHP application that I wrote. This application has many database object just like any other PHP app. But since it is a multi-client level app, I have different client requirements every time we on board a new client. There are couple common object (aka accounts) that host most of the client information. But blmost, every time I want to add new client to the database, this new client is going to require a new column in the accounts databases added because they will have some unique data. There are standard required data (ie. account_name, status......), but some data are unique and labeled different based on the client business type.
Currently, what I have to do to accommodate the requirements is
Alter the accounts table and add this new column manually.
edit the PHP script where I display the account information and display the content of the field like so
if(!empty($row['new_filed']))
echo 'Single Client Required Field: ' . $row['new_filed'];
if the field is editable, then I have to add rules to the edit/add form to do a data validation check before save.
I am trying to build a front end tool that will does that automatically for me. This way I can have an admin "not a programer" go the the front end tool and add a new field and that field will be displayed in the selected section of the selected page.
From the front end tool I want to be able to say add a custom field called "brand_c" and make it of a type varchar(100). Then on the page that is called "account_info.php" in the section called "Other" display this field with the label "Brand" only if the client = 'XYZ' so if the data belong to client 'ABC' do not display the data since this column is custom only for 'XYZ'.
I understand this is a wide question but what I am looking for is any help of how such a thing can be build (were to start from?). This is something that most of the big CRM do "ie. Microsoft CRM Dynamic, Salesforce.com ...) have it. How can I architect something like this?
Thank you
The first step you will need to do is design the tables properly, so basically you need to know how to design table to support dynamic fields and the field will be able to validate/handle such such data.
What I did with my application(dynamic application where the UI fields can be configured in the application's admin) is saving the configuration in the database, each time there is a transaction, the page will fetch the configuration for the selected biller and it will generate the required fields. and when the form is submitted, the application will again fetch the configuration and make validation based on the setting in client_field
Account table *Contain information such as account
account_id //primary key of this table
account_name
account_status
Client table
client_id //primary key of this table
account_id //foreign key:account table
client_name
client_status
you can add different column for the client depending on your requirement, but make sure not to add the required UI fields in this table, instead create the table like below
client_field table //will contain table that can be defined/configured
field_id //primary key of this table
client_id //foreign key:client table
field_name //the field name that will appear in the user interface
field_title //appear when on mouseover on the field
field_datatype //this can be text,integer,date,boolean,etc
field_type //field input type: this can be a textbox,datepicker,dropdown list,etc
field_required //flag to indicate whether the field is required
field_display //flag to indicate whether the field will appear/hidden in the user interface
field_defaultvalue //default value to set when the page renders
field_minvalue //minimum value the field will accept: only acceptable when dataytpe is integer/double
field_maxvalue //same as field_minvalue description instead it will check the maximum integer/double value
field_maxlength //max length for the field
field_order //order of the field in the interface
field status //activate or deactivate the field
You can add additional column but you need to make sure it will be fluid and not redundant.
let us say, you have field_type dropdown list, you need to create another table, lets call it
field_dropdown
id //primary key for this table
field_id //foreign key from client_field
dropdown_value //the value that will be used in the dropdown list when selected
dropdown_description //the description that will appear in the dropdown list
dropdown_order //order of the item
dropdown_status //flag whether this record is active/inactive
You might be wondering how to save the data if this is the case, the data must be saved in another table, lets call it
client_information
id //primary key for this table:
client_id //foreign key from the client table
field_id //foreign key from client_table.
value //the value from the user interface
this way, when you save a new client, each field in the client_field will add a new row in the client_information table along with the value inserted by the user
the challenges in designing this kind of application are:
it is quite hard to develop (lots of condition and mapping, and require alot of time)
you will need to properly configure the fields in order for it to properly work
proper system design,architecture, and flow otherwise it will ruin the dynamicity of the system
you will need proper documentation so that you will understand which is which.
the process will be linear for all the clients except if you can alter your own flow by saving them into the database
the PROS for this design is:
once it is finished, you do not require anymore developer to add additional code except for software enhancement such as feature that is not supported when it was released.
the application will be dynamic and adding any field will be easy and maintainable via the admin module
I am not saying this is the MUST DO, but this is my suggestion on how to develop application that requires dynamic data fields, and so far from my experience in developing one, it did work well just as long it is well design and developed.
Instead of adding additional fields as a new column into your accounts table, you could create a new table, say extra_data, with for example the columns account_id, data_label, and data_content. This way you could save as many extra pieces of data per account into that table, and fetch it in account_info.php.

D7, Views 3 | How do you conditionally rewrite fields?

In Views, when I rewrite a field and then hide some other fields and use their tokens to compile them into that first one I rewrite, how would I go about doing an operation such as this:
Example:
Say I have these fields:
field_first_name
field_last_name
field_old_nickname
field_new_nickname
Everyone has a first & last name, but:
Some people have ONLY an old nickname
Some people have ONLY a new nickname
Some people have both an old & new nickname
What if I want to rewrite these fields into a format such as "John (Nobody) Doe?"
Normally, we would hide all fields but the first name and do this:
[field_first_name] ([field_new_nickname]) [field_last_name]
But what if John has a old nickname too and we want to display the old one instead if that's the case?
Get what I'm saying?
Thanks in advance.
You can use the option "exclude from display if empty" for the
two fields "field_new_nickname" and "field_old_nickname"
then rewrite the output field as : [field_first_name]
([field_new_nickname] ,[field_old_nickname]) [field_last_name]
so the empty field will not show up
A #drupal-support IRC user named "ciss" answered this question for me-- it was much easier than I had thought, which is great. Thank you ciss!:
"Regarding your tokens question: you can use tokens in the empty output of a field. That way you can trigger which tokens should get displayed. Make sure you pick a field for rewriting that can actually be empty (either empty string or "0"). if necessary, add a specific field twice-- Views will not check if the rewritten content is empty."

Symfony Set Drop Down Value Based Upon ID

I am still getting my hands dirty with Symfony, so I am a bit ignorant on how to do something that should be pretty simple.
I have a form that I am creating in the controller and passing to the view:
$form = $this->createForm(new PurchaseOrderType($account), $purchaseOrder);
The form displays exactly how I need it to, no problems at all! I am trying to now make the form more dynamic so that it can auto select a drop down list based upon an "id" variable that I am passing into the form. The id equals 23 by the way.
So, I have a drop down of suppliers and one of the options value is 23. How do I automatically select this option? Sorry for my ignorance :)
Thanks!
Without the code of your form type, I can make only suggestion.
If I get it correctly, inside the purchase order entity, there is an other entity mapped, and that one is represented with an id.
The object, $purchaseOrder has to have the other entity, then in the form type, when you set up the drop down field, you have to specify - with the correct name it shouldn't be a problem - the foreign id.
But you need of course data for the drop down field what can be the result of a SELECT * query.

Store dynamic form fields in database

I have read other answers on this (or at least near to this) subject but I couldn't get a clear view of it so I'm asking for help again.
I have a complex dynamic HTML form that I would like to submit to database using PHP. The form is split into multiple tabs and in each tab I got checkboxes that trigger other parts of the form. Example: at a point in my form I got a checkbox group that has options of: "hotel" and "restaurant". If I check hotels, I get another part of the form displayed, specific for "hotels". Same thing for "restaurant". So it's very dynamic here and I don't know which would be the best approach for storing every form field in database. Because it could contain 15 fields or 20, depending on the selection. Any example would be appreciated as I'm not that advanced with database design.
Thank you!
So it's very dynamic here and I don't
know which would be the best approach
for storing every form field in
database.
I apologise if I have misunderstood you here but I believe that you should design the database according to the data and not the form. It is difficult to comment without knowing the exact details of your situation so here is an example:
If you usually dump all the data from a form into a single table, but because sometimes this will involve submitting 5 values and other times this will involve submitting 10 and so you are unsure how many columns your table should have, then I think the problem is in the database design.
Work out what pieces of data are dependent on other pieces of data. For example, you mention checking "hotel" might open up more fields specific to that choice. Let's assume this involves things like "en-suite", "bed type" etc. Then you should have 3 tables, a registration table (assuming the user is using the form to buy these services), a hotel table and a registration_hotel table. The registration table will record a number of details specific to the registration only such as the customer's name and a unique id number. The hotel table will hold information specific to the hotel only, such as how many rooms have en-suite. The registration_hotel table will hold details specific to that registration at that hotel. You might want a column of type bool to record whether the user requested "en-suite".
When submitting the form, check which pieces the user entered with if(isset($_POST['hotel']) && !empty($_POST['hotel'])). Then only send stuff to the registration_hotel table if that condition is true.
If this design results in making too many separate calls to the database, you might want to look into transactions which will help you to manage the speed and security of these calls.
If you can post in a specific example of something you don't know how to do, that would be useful.
You didn't specify how you can manage this dynamic form. Can you edit it's PHP/HTML source? One great thing would be if you can label your different variables like hotel[], restaurant[], etc.
If your submitted form is clear enough (i mean semantically correctly structured) you can store the whole submitted form serialized.
Note: this method only working when you don't need to search for specific items in your database.
Edit: maybe i'm misunderstood your problem.
You can create a 'metadata' table like this:
form_id | option_name | option_value
---------------------------------------
1 | hotel | true
1 | restaurant | false

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

Categories