Calculated Field / Column in DB List
-
Hello,
with WP Data Access is it possible to generate calculated fields/columns on the Table list with a combination of characters and column/field content?
For example:
I have rows with gps data and I want to build a link for access a map-site like google etc.
Is it possible to generate custom fields with a combination of text and variables of other fields?Thanks a lot in advance.
Best regards,
Bernd
-
Yes, it’s possible Bernd.
You have to create a view and do all the calculations and concatenations of the various bits and pieces in the select statement to create the fields you need. You then display this view on your page.
Kind regards
MelodyHi Bernd,
Is the solution mentioned by Melody an alternative for you?
@melody Smart solution! π Although it has some limitations…
Best regards,
PeterHi Melody, hi Peter,
thank you for your rapid feedback.
What do you mean with “create a view” and “in the select statement to create the fields you need” there?
Where can I do that in the plugin area?
I only started working with “wp data access” yesterday and and I am not an expert yet π
I have an idea in a very simple table and it would be great i can get a solution…Thank you and kind regards,
BerndBernd, you have to know how to write some sql code and you probably will not be able to do it if you are a beginner. Do you happen to have a friend who is a coder who can take a look at what you’re trying to do and can write the sql script for you?
Hi Melody,
oh, to write a little SQL Code should not be the problem.
I was a programmer in my first job.
Now I’am coding in Windows powershell, some other scripting and SQL Scripting is not strange to me.
I am new in “wp data access” plugin and I don’t know where I have to create the view (Data Explorer, Designer, Projects, Publisher) and where to place the sql select statement or query in this view.
Please give me a tipp…
Thank you.
Kind regards,
Berndoh great .. well you need to write the sql script to create the view and then you have to execute the script in either Data Explorer or Data Designer .. there is a button called “Import data/Execute script(s)”
Here is an example of a very simple script I wrote:
CREATE VIEW tcsa_v_committee AS SELECT C.committee_portfolio , CONCAT(L.leader_firstname,' ',L.leader_lastname) AS v_fullname , L.leader_phone_main , L.leader_email FROM tcsa_committee AS C INNER JOIN tcsa_leader AS L USING (leader_id) ORDER BY committee_display_order ASC;I have two tables called tcsa_committee (which has the committee positions) and I have tcsa_leader which has people in it and some of them are on the committee. I am creating the view tcsa_v_committee which has the committee positions with the leader’s names and other details and in the order in which I want the committee displayed in the table.
Make sure your CREATE VIEW sql script ends with a ; and that you have a blank new line at the end or it will not work in WPDA.
Hope that helps.
I will try this. This could be the solution.
Where can I look for the syntax of the sql statements?
It seems there is a difference between wp data access and mysql syntax, or?
– https://www.mysqltutorial.org/mysql-views-tutorial.aspx
Thank you and best regards.I have only a simple table with 7 columns:
Column Name
weg_id
weg_bezeichnung
start_gps_breite
start_gps_laenge
end_gps_breite
end_gps_laenge
nlp_bereichMy create view statement comes with error message:
ERROR: Importwege_view.sqlfailed [check import file]
Here is my script:CREATE VIEW bk_weg_view AS
SELECT W.weg_bezeichnung
,W.nlp_bereich
,CONCAT(‘http://www.openstreetmap.org/?mlat=’,W.start_gps_breite,’&mlon=’,W.start_gps_laenge,’&zoom=15&layers=M’) AS osm_link
,W.start_gps_breite
,W.start_gps_laenge
,W.end_gps_breite
,W.end_gps_laenge
FROM bk_weg_db AS W;I have already tried several variations without success..
Thank you.
The concat row in the thread before is not correct:
The http-string ends with an =,CONCAT('http://www.openstreetmap.org/?mlat=',W.start_gps_breite,'&mlon=',W.start_gps_laenge,'&zoom=15&layers=M') AS osm_linkthe name in your FROM .. bk_weg_db .. is that a table name or a database name? It must be a table name. I ask because it has _db on the end, so it looks like a database name.
FROM bk_weg_db AS W;Oh yes, this is a table with a misleading name. Until now it is all a test environment…
Hi Bernd,
What is the status? Did you succeed? Do you need a hyperlink in a publication? Or in a project?
The plugin supports hyperlink columns. Usually these columns are formatted by the plugin. Since you are using a view, you need to take care of the formatting yourself.
(1) You first need to add a column to your view in which you dynamically build the url as Melody suggested. To display your column as a hyperlink in your publication or project, your column output needs be a valid JSON string in the following format:
{ "label" : "...", "url" : "http://www.openstreetmap.org/?mlat=...", "target" : "_blank" }(2) After that you need to tell the plugin to handle your hyperlink column as a hyperlink in publications and projects:
– Go to the Data Explorer
– Click on the Manage link of your view
– Click Settings
– Click Column Settings
– Select Column Type hyperlink for your hyperlink column
– SaveThe challenge is in the first step. Here is an example:
create view authors_bernd as select first_name, concat('{"label":"',first_name,'","url":"http://wordpress.local/wp-admin/admin.php?page=wpda&table_name=authors&action=listtable&wpda_s=',first_name,'","target":""}') url from authors;The example is simpler than yours, but it contains everything you need in your view as well. Column url is defined as a hyperlink within the plugin. In the create view it is a concatenation of strings and the value of column first_name. The output of column url must be valid JSON in the format I mentioned above.
I hope this helps you to create a dynamic hyperlink. My advice: try to get it working with just one column first and add your other columns later.
Let me know if I can help!
@melody Thank you for contribution! π It is appreciated!
Best regards,
PeterI created your table on my database and ran your script without any changes in WPDA and it worked. Remember I told you that you must have a blank new line at the end.
CREATE VIEW bk_weg_view AS SELECT W.weg_bezeichnung ,W.nlp_bereich ,CONCAT('http://www.openstreetmap.org/?mlat=',W.start_gps_breite,'&mlon=',W.start_gps_laenge,'&zoom=15&layers=M') AS osm_link ,W.start_gps_breite ,W.start_gps_laenge ,W.end_gps_breite ,W.end_gps_laenge FROM bk_weg_db AS W;@peterschulznl thanks for the info on how to CONCAT an URL .. I do have an URL in one of my views .. it is in its own column and I defined that column as a hyperlink and that works, but I’m going to try what you’ve explained above.
PS I’m happy to help where I can π It’s the least I can do to show my appreciation for all your hard work. I’m in South Africa and my currency is worth nothing compared to yours, so making a worthwhile donation is not really feasible. I think one of my Rands is 5c in Euros .. I’ll need a bank loan just to buy you a cup of coffee .. hahahaha
Hi Peter,
I have tried to get this.
My create view script:
create view bk_weg_view as select weg_bezeichnung, concat('{"label:osm_link","url":"http://www.openstreetmap.org/?mlat=',start_gps_breite,'&mlon=',start_gps_laenge,'&zoom=15&layers=M","target":"_blank"}') AS url from bk_weg;I had to add the key name
ASbefore url in the concat line and then I get the messageINFO: Importbk_weg_view1.sqlcompleted succesfully
But where can I find the view for the other settings in your thread?
In the data explorer I only find my table (with type “base table”) but not this view.Is there a documentation of the “wp data access” syntax and statements?
The mysql syntax is a little different.Thank you very much.
Best regards,
Bernd
The topic ‘Calculated Field / Column in DB List’ is closed to new replies.