Forum Moderators: coopster

Message Too Old, No Replies

PHP & MySQL Input Method

PHP & MySQL Integration of Multiple Values in one field.

         

Sherif

5:13 pm on Jun 4, 2010 (gmt 0)

10+ Year Member



Hey Guys,

I just wanted to thank you all for making this community a very fruitful one. I myself began learning php & MySQL through reading through the forums and through many tutorials and e-book.

I am currently working on a small website, and everything seems to be working great, i only have a small problem in a part of it.

I am working using PHP, and MySQL to input data about companies so that people in my community can find more information about these companies in terms of address, phone numbers, website, etc..

No, I'm beginning to get more support from these companies, and i got the idea of adding more details about their production rate, and prices, but i was wondering how can i implement this on the existing database.

the type of data that i'll be getting for each company is as follows

Prices:
year=2008 Price=1000$
year=2009 Price=950$


and the same goes for the production

year=2008 capacity=10,000tons
year= 2009 capacity= 15,000tons



i was wondering how can i add these information in one field for each, example adding a field for prices, and a field for capacity in the database.


How can i use php, to add to the existing data the prices and capacities for each year, and how can i later on edit them or even remove a part of it?

I thought of using arrays, but i don't seem to understand it, and how i can implement it in this case.


Your help in this would be life saving for me.


Thank you,
Sherif.

rocknbil

6:14 pm on Jun 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i was wondering how can i add these information in one field for each, example adding a field for prices, and a field for capacity in the database.


My first thought is to use a valid mysql date instead of just a year value. What if the company's "fiscal year" does not start and end at the same time as other companies? Or they start their first year in June, so the statistics only have 6 months of data? This would give you a much more accurate view of the company's statistics for a given year.

It's easy to select just the year out of a valid mysql date:

(valid mysql date format: 2010-06-06)

select * from table where year(date_field) = '2010';

year() [dev.mysql.com]

Using a standard mysql date format will give you all sorts of other options inherent in mySQL without resorting to programming workarounds, saving you tons of time (sorting, selecting records, extracting date formats.)

Second, it's very likely you will want to store multiple records for each company so you can graph data over time. What good is it otherwise? :-) Knowing this, it's also very likely you already have data for companies that is single-record data, that is, they only have one company name, one address, one phone (well, these may be multiple, but my point is, you probably already have a main table with row data in place.)

So your solution lies in creating a second table in which to store the many-to-one records, a relational table, and you store it so it joins on the main table's unique id. This would allow you to add these records without disturbing your current table structure as well. If you have something like the below, where "id" is an unique id for each record,

id|company_name|address|contact_fname|contact_lname|address|city|state|postcode|country etc.

you create a second table:

id|company_id|year_start|year_end|price|capacity etc., any other data relating to this year

And the company id field joins on the id field in the original record. To get all the data for a company, sorted by year from the lowest to highest, with the mySQL date (YYYY-MM-DD) date formatted as MM/DD/YYYY,

select company.company_name, date_format(year_data.year_start,"%c/%d/%Y"),date_format(year_data.year_end,"%c/%d/%Y"), year_data.price,year_data.capacity from company,year_data where company.id=year_data.company_id sort by year_data.start_year asc;

date_format() [dev.mysql.com]

To get all the data for a specific year,

select company.company_name, date_format(year_data.year_start,"%c/%d/%Y"), date_format(year_data.year_end,"%c/%d/%Y"), year_data.price,year_data.capacity from company, year_data where company.id=year_data.company_id and year(year_data.year_start)='2010';

How can i use php, to add to the existing data the prices and capacities for each year, and how can i later on edit them or even remove a part of it?


I won't expand a lot on this because "there are lots of ways." If you've been working with the basics of connecting to a database and displaying records via the select statements, adding or updating records is very similar except that you'd use update, insert, and delete statements.

You would have to create an administrative interface, accessed via a login you would write, that would incorporate the management features you need. The point of my post, really, is to let the database functions to as much work for you as you can to minimize your programming tasks.

Sherif

7:06 pm on Jun 7, 2010 (gmt 0)

10+ Year Member



Hey rocknbil,

Thank you so much for this detailed explanation... I really appreciate you taking all of that time to reply to this thread.

I just want to ask a final question, when you say we create a many-to-one records table (relational table), do you mean that we go into phpMyAdmin, then add the relational table to the database, then go to the designer mode, the create the relation, or do you mean just connecting to the 2 tables when performing the query?

If i'm going to create the relation in phpMyAdmin, then
- what is the "select referenced key", and the "Select foreign key"?
- are there any special commands or concepts that i must understand while performing this relation?

This is the first time that i am working on something of this nature, so please mind my very small knowledge, so could you please clarify on the sql statements, since i am not familiar with the "." command in the statements

Again, thank you for your support and again i really appreciate it.

P.S. can you please guide me to any readings that could be helpful for me at this stage?

Sincerely,
Sherif Malek

rocknbil

10:37 pm on Jun 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what is the "select referenced key", and the "Select foreign key"?


Ahh . . . I think that may be beyond the scope of what you're doing, almost to the point of not being necessary, or even, creating more overhead (at the very least, a steeper learning curve.) The foreign key would be "company_id" and it's referenced primary key is the id field of the main table. the reason for setting foreign keys is to keep the data between the two synchronized and establishes a faster lookup between the two when doing so. It also creates some conditions you need to address when updating and deleting records, and is entirely internal to mySQL (not PHP), and only supported by INNODB engines. So you can if you want, but for what you're doing is probably not necessary.

do you mean that we go into phpMyAdmin, then add the relational table to the database .... just connecting to the 2 tables when performing the query?


Yes . . . just create the second table, and make sure the data type of "company_id" in my example is the same data type as "id".

i am not familiar with the "." command in the statements


Look how both tables have "id" as primary fields in my example. When you join on multiple tables, how does mySQL distinguish between the two? It can't . . . unless you disambiguate which one you mean by specifying which table you're referring to. So it's always good practice to do that when you join multiple tables:

select table1.fieldname, table2.fieldname from table1, table2.....

can you please guide me to any readings that could be helpful for me at this stage?


mySQL Documentation [dev.mysql.com]

Choose your version, and there are some tutorials there.

Sherif

5:06 pm on Jun 15, 2010 (gmt 0)

10+ Year Member



Thank you so much for your patience, and support... i started working on the link between both tables...

Just for confirmation, if i wanted to SELECT all columns from both table, would the syntax be

SELECT * FROM Table1, Table2 WHERE.....

Is it recommended that i use the " ` " (back ticks) in the table names?
Should I avoid the spaces in the mySQL command? (Ex. the ones between the table names?)


Thank you so much for your support.


Sincerely,
Sherif

Readie

6:33 pm on Jun 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it recommended that i use the " ` " (back ticks) in the table names?
Should I avoid the spaces in the mySQL command? (Ex. the ones between the table names?)

Using back ticks is a good habit to get into, will save you a headache when you unintentionally use a protected name.

Whitespace has no bearing on the query whatsoever - it does however make it that much more readable, and imo is to be encouraged.

---

As for selecting from multiple tables... Either a left or inner join would be the way to go, you can find descriptions and examples pretty quickly from a Google of "mysql join". If you enounted problems that you can't overcome implementing a join statement come back and post again.

Just a pointer btw, all references to column headers (and, AFAIK, * too) need to be referenced with their table name too:

SELECT `table1`.*, `table2`.* FROM...

Sherif

11:35 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



Thanks...

I'm going to work on it and see how everything.


Again, Thanks for the quick support ;)


Sincerely,
Sherif