Re: [chiPHPug] Re: Displaying many-to-many

From: Date: Thu, 14 Dec 2000 19:12:40 +0000
Subject: Re: [chiPHPug] Re: Displaying many-to-many
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-30344@lists.php.net to get a copy of this message
Here is what I would do: <?php # Untested code: $query = "select company_id, company, owner from companies, owners "; $query .= " where owners.company_id = companies.company_id order by company "; $companies = mysql_query($query) or die(mysql_error()); $query = "select company_id, service from companies, company_services, services "; $query .= " where companies.company_id = company_services.company_id "; $query .= " and company_services.service_id = services.service_id "; $query .= " order by company "; $services = mysql_query($query) or die(mysql_error()); $query = " select company_id, specialty from companies, company_specialties, specialties "; $query .= " where companies.company_id = company_specialties.company_id "; $query .= " and company_specialties.service_id = services.service_id "; $specialties = mysql_query($query) or die(mysql_error()); $last_company_id = 'An extremely unlikely company id'; $service_row = 0; $specialty_row = 0; while (list($company_id, $company, $owner) = mysql_fetch_row($companies)){ if ($company_id != $last_company_id){ #Spew out the Services and Specialties of the *previous* company: while (mysql_result($services, $service_row, 'company_id') == $last_company_id){ echo mysql_result($services, $service_row, 'service'), ", "; $service_row++; } while (mysql_result($specialties, $specialty_row, 'company_id') == $last_company_id){ echo mysql_result($specialties, $specialty_row, 'specialty'), ", "; $specialty_row++; } if ($last_company_id != 'An extremely unlikely company id'){ echo "</BLOCKQUOTE>\n"; } $last_company_id = $company_id; echo "<B>$company</B><BR>\n"; echo "<BLOCKQUOTE>Owners: "; } echo "$owner, "; } #Finish off that last company's services and specialties, just like above: #Spew out the Services and Specialties of the *previous* company: while (mysql_result($services, $service_row, 'company_id') == $last_company_id){ echo mysql_result($services, $service_row, 'service'), ", "; $service_row++; } while (mysql_result($specialties, $specialty_row, 'company_id') == $last_company_id){ echo mysql_result($specialties, $specialty_row, 'specialty'), ", "; $specialty_row++; } if ($last_company_id != 'An extremely unlikely company id'){ echo "</BLOCKQUOTE>\n"; } ?> ----- Original Message ----- From: Andra Medea <amedea@rcnchicago.com> To: <chiphpug@egroups.com> Sent: Thursday, December 14, 2000 12:14 PM Subject: [chiPHPug] Re: Displaying many-to-many > Actually, I already junked that part of my code. > Let's see if I have it right: You're saying that PHP would be > just > as happy if the whole thing were patched together with > concatonation. No > need to bother with html tables. But how to keep the joins > straight? > Would that approach use a string of nesting if... while loops to > keep all > the pieces straight? > I keep going back to craving examples because I think I'm > using a > conceptual model that's out of whack here. I think there's some > assumption > that you have that I don't have. If I could see how an ordinary > model plays > out, that would probably make it clear. Unfortunately, all the > models that > I've found so far are only displaying data from a single table. > None of the > joins to contend with, and it's the joins that are tripping me > up. > Meanwhile, I think I'll go back and see if there isn't a way > to > generate the output from a single gnarly SQL statement. Maybe > that can be > used for display. Unlike some database heretics (who shall remain > unnamed) > some of us true d-base believers see the beauty and grace in > many-to-many > tables with their $%*^#& joins. > Although, come to think of it, maybe it's the $%*^#& that > messed up my > code. -- Andra > > > Richard Lynch wrote: > > > Hmmm. I think the calendar code I have up on PX (Sklar) and/or > > WeberDev has > > some many-to-many joins in it. > > Perhaps you could post that section of your code here though. > > > > > -------------------------- eGroups Sponsor -------------------------~-~> > eLerts > It's Easy. It's Fun. Best of All, it's Free! > http://click.egroups.com/1/9699/1/_/_/_/976817177/ > ---------------------------------------------------------------------_-> > > To unsubscribe from this group, send an email to: chiPHPug-unsubscribe@egroups.com > > > >

« previous php.general (#30344) next »