Re: can you help to beginner?

From: Date: Thu, 23 Aug 2001 17:57:40 +0000
Subject: Re: can you help to beginner?
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-11740@lists.php.net to get a copy of this message
Actually, it is much better to use three tables in this case. Have the following tables: Company table: companyid and companyname Services table: serviceid and servicename Company2Services table: companyid and serviceid Let's say you have three companies: 1 Kleptos Anonymous 2 Solar Sneezers 3 Microshaft And you have 4 services: 1 Dry-cleaning 2 Software programming 3 Web programming 4 Gardening Now let's say Solar Sneezers and Microshaft both offer Web programming as a service. You would add the following records to the Company2Services table: companyid = 2, serviceid = 3 companyid = 3, serviceid = 3 Now lets say Kleptos and Solar Sneezers both offer Dry-cleaning. Add the following to the Company2Services table: companyid = 1, serviceid = 1 companyid = 2, serviceid = 1 And only solar sneezers offers Gardening: companyid = 2, serviceid = 4 So now you have the following in the Company2Service table: companyid = 2, serviceid = 3 companyid = 3, serviceid = 3 companyid = 1, serviceid = 1 companyid = 2, serviceid = 1 companyid = 2, serviceid = 4 NOW, you have something that is flexible. Run the following query: SELECT c.companyname,s.service FROM Company2Service cs LEFT JOIN company c ON cs.companyid=c.companyid LEFT JOIN services s ON cs.serviceid = s.serviceid ORDER BY c.companyname; This will return 5 records, which are the 5 different combinations of companies and services, with the company names in alphabetical order. Here would be the code: $Link = mysql_connect(....); $Query = "SELECT c.companyname,s.service FROM Company2Service cs LEFT JOIN company c ON cs.companyid=c.companyid LEFT JOIN services s ON cs.serviceid = s.serviceid ORDER BY c.companyname;"; $Result = mysql_db_query("MyDatabaseName",$Query,$Link); while($Row = mysql_fetch_array($Result)) { $company_name = $Row["companyname"]; $service_name = $Row["servicename"]; $Companies[$company_name][] = $service_name; } foreach($Companies as $Company => $ArrayOfServices) { print "Company: $Company<BR>\n"; foreach($ArrayofServices as $Service) { print "$Service<BR>\n"; } } -- Jonathan "Andrius Jakutis" <andrius@internetmedia.lt> wrote in message news:20010823174553.85097.qmail@pb1.pair.com... > Hello All, > > Maybe you can help me, becouse I was searching for various documentations, > and it seems too difucult for me, becouse everything is so crowded. > > I need to built small html file, where I could get listed companies. > > I have two tables in my MYSQL database, companies and services. > Company table: > companyid and companyname > > services table: > id companyid services. > > So I need simply to get listings like this: > > Company name > services > > Company2 name > Services > > etc... > > Can you help me? I know that for this example its better to use one table, > not two, but I am just learning.. > > Thanks > >

« previous php.db (#11740) next »