Database Schema For Populating Select Lists with PHP/MySQL - php

I have a two part question with no code to show, but i hope you can help:
Essentially, i have a website which lets businesses create a profile for themselves (original table schema here).
Now within the site I want a form with three select lists in it: the first select list is for business catagory/tag - I have managed to create a query which will return entries from my main business registration table by using a Toxi solution.
However i also want a second select list with countries or regions, and a third select list with states/counties which will load automatically BASED on the selection from the Country select list.
Then naturally, on submitting the form, i want to return the results to the visitor.
So, apart from wondering how to achieve this much in PHP/MySQL, i have a more fundamental question about whether to set up the countries/states 'tagging' or 'catagories' field as another Toxi solution, or if there's a better way to do it. For instance, should i/can i just create seperate tables to populate the select lists with states/counties based on country selection, and have the actual country and state fields that a business selects on registration, stored within my main business table, and just match the strings when the user hits submit, in order to reduce queries to MySql and improve speed?
As always, your advice and help is greatly appreciated.
Thanks
Dan

Think about the "full name" of a state (or whatever political division is one level narrower than "country"). In the Americas, here are some "full names" of states.
California, US
Connecticut, US
Ontario, CA
Sonora, MX
Lara, VE
Base your lists on a two-column table of the full names of states.
ISO country codes
The critical point is that your selection lists shouldn't allow values like "Alabama, MX" or "Sonora, US".

Related

Best schema to find items according to multiple criterias

I'm creating a website using Symfony2 and I want my users to see a list of items according to their profile. I am looking for the best way to do that.
The users have basic fields attached to their profile (first name, email, etc.) but I want to add some specific information: gender, income, interests (so different types : bool, integer, string etc). Some fields and values may be added, modified or deleted later.
The items will have criteria so that they will be shown to the corresponding users according to the users' profile.
Actually it is a bit like a shopping website with information attached to products and then the client can reduce the number of items shown thanks to a list of criteria.
I thought of putting an array with the different criteria and values in the User entity but I find this solution really bad.
Do you have an idea of what might be the best Entity/DB schema to do that?
This is filtering, your query which you use to get your product is the one you should play with not the user.
As i understood you want to show different product to each user depending on their gender etc..
You will need some kind of link to know which product to show to which user? Which information you want to check in the product side? you can have an entity linked to product with different information like gender male for a product that you will use in your query to get all select * from product where product.criteria.gender = user.gender this is just a simplified query but it is a bit more tricky with a join if you want to do it in SQL or DQL

PHP echoing a specific field from a table in MySQL

Firstly, I'm quite new to PHP having only dived in some three weeks ago but loving it as a new thing to learn! I have a specific problem that I cannot seem to find a solution for via Google. I'm running a test page that will form the basis of a final product for a local recreational club that runs competitions and wants to display the results online on their website.
I've created a MySQL database and called it 'results' and imported as a CSV a sample of competition results. My code to connect to the database works as the page displays the "Database Connection Established" message.
The database contains a table called 'z_any_year_results' and the table structure looks like this:-
Record_Number Field Value
1 Field_1 Value_1
2 Field_2 Value_2
3 Field_3 Value_3
4 Field_4 Value_4
5 Field_5 Value_5
I understand how to select the specific table using
mysql_select_db("results") or die(mysql_error());
$data = mysql_query("SELECT z_any_year_results FROM results")
but I need to echo a specific field from the table in a specific section of the web page. So for example, in one section of the page I need to output the field containing the value Field_1 and nearby on the page the field containing the value Value_1. But in another section of the page I need to output the field with the value Field_4 and nearby on the page, the field containing the value Value_4. So I guess my problem is how to extract a specific piece of data from a table to the exclusion of all other records in the table and outout it as an echo on the web page. I cannot find anything on the web that is written in a simple step-by-stepway to help novices like myself understand.
Can anyone point me in the right direction on how to achieve this?
Many thanks in advance.
You are using a type of data design known as key/value design. In other words, each row has the name of a data item and its value. That's not an ideal sort of design for a beginner to use, because it makes for fairly intricate queries.
To answer your question, if you want a certain named field's value you use this query.
SELECT Value FROM z_any_year_results WHERE Name = 'Field4'
But, maybe you want a design that resembles your application's entities a little more closely.
You might have an entity, a table, called, contestant, another called contest, and another called prize.
contestant is a table with columns like contestant_id, surname, givenname, email etc
e.g. 1 , Ellison, Larry, larry#oracle.com
Then you can use queries like SELECT * FROM contest WHERE YEAR(datestart) = 2016 which will make your queries more closely reflect the logic of your application.

Retrieve a table's fields from a lookup table dynamically

I am making a mysql/php project and in one form, the specs require dynamic fields in a way that you have an initial selectbox with a value from a lookup table that describe job unions. Depending on the selected value, it will spawn (probably via reflection) different fields.
For example, I have 2 associations with ids 1 and 2. If the user selects union #1, then the fields would be first name, last name, phone, address. If the user selects union #2 then the fields would be mobile, email, im name, "enroll now?"(checkbox).
Now, this might occur often, because the total of unions are more than 10 and specs require it to be flexible.
What I thought is this:
form loads up the fields of the first lookup (jobunions)
user selects the job union, and the value of the selectbox is the name of another table, for example LK_TABLE_2
The reflection examines the LK_TABLE_2 fields and retrieves/renders the fields below the selectbox of step 1.
I need your opinion on whether this business logic is acceptable (is there a pattern I could use?), if not anything to suggest and if it's doable how to store the filled data into the user preferences table.
Any insight would do.
Update: here is a schema (pdf) of what I am trying to do
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B_vptVa0K8J2YjBjMGJmZDgtYzUxZi00ZTE5LTgxZjgtOTNlMjQ5OGM3ZTY1&hl=en_US
I am closing this question, because of the project that was terminated, and further more I don't work in the specific company anymore (over half a year ago).
We jumped to a more nosql solution before this happened and it was working fine... until termination.

MySQL relations for country region and town tables

I am trying to figure out the best method to relate country, region and town tables.
On my website I want the user to be able to just enter a town. Then optionally country and region, both of which will be required to be entered or not at all.
Currently my tables are as such
tbl>User has townID (FK)
tbl>town has id(PK) townName regionID(FK DEFAULT NULL)
tbl>region has id(PK) regionName countryID(FK NOT NULL)
tbl>country has id(PK) countryName
I thought to possibly further spit the user to town relation to:
tbl>User has locationID (FK)
tbl>location has id (PK) townID(FK) regionID(FK) countryID(FK)
But I think that is unnecessary and just further complicates the issue?
The country database is already populated. I intend to build up my own references of town > region > country relations as entered by users. So if a user enters a town with no region and country then it is entered into tbl>town without a regionID if there isn't already a town with the same name without a region ID. This is same for a town where a region and country ID has been entered by the user. Only I check that there isn't already a town > region > country relation that already exists before entering. Later on in the development of the site I will be providing Ajax suggestions for country/region based upon the town entered by a user.
So to the questions:
I can envisage pitfalls with this such as duplicate data or data possibly being overwritten. Is there a better way to construct the tables to fit in with my desired methods?
This might get answered by the prior question: but is there anything I can do to reduce the PHP processing of the tables. Obviously I'd prefer to just insert with one PHP statement but I think there are too many caveats to do it at once.
Also as the users town entry may be null and may or may not contain a foreign key reference to a region how is it best to create a View that takes that into consideration?
As it will be hosted I would rather not be using MySQL functions.
Please let me know if you need any clarification. I really want to get this right the first time before continuing, so your help will be invaluable.
I don't think you reduce the code because it's much too explicit. You can change it, but it won't be better.
Accepting a town name without a region and country is like letting someone enter their first name without their middle or last. It's data, but it's not an identifier.
Fullerton's full name is "Fullerton, California, USA". By not requiring Fullerton's full name, you abandon foreign keys for data integrity. ("Fullerton, California, USA" is a city; "Fullerton, Alabama, USA" is not.) Good luck with that.
If you're going down this path, the best advice I can offer you is get rid of the id numbers. ISO publishes standard codes for countries and subdivisions of countries. You can look them up in Wikipedia. Storing natural keys will reduce the number of joins from 3 to zero. Zero joins is almost always going to out perform 3 joins.
You'll probably need to use outer joins to create your views.

Developing a Web application using Dreamweaver

am developing a web application and I need some help.
I have in my database(MySQL) tables for village linked to the sub county table. Sub county linked to the county table and the county table linked to the District table.
I need to design a web form in dreamweaver using PHP for capturing people's details i.e Name,Date of Birth, Village, Sub county,County and District. But since I'll already have the District,County,Sub county and village in my DB, I need to create a drop down for village which after selection should populate automatically the Sub county,county and district text fields.
You can advise on the best way I could do this.
Cheers.
A simple query is in order? Just use an ajax request to send the selected country to a script, the script should return either XML data to fill the select box with or send the dom select box.
i'd go for the
<select onchange="ajaxGetWhatYouNeedByValue(this.value, 'populate some #id');"><option.....
as i understood your question, you need something like => when you select a village it should return all related data associated with the village. Therefore i suggest you using ajax for background loading needed data.

Categories