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" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> 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
>
>



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to