Re: 2 while loops - 2 tables

From: Date: Thu, 15 Mar 2001 04:58:34 +0000
Subject: Re: 2 while loops - 2 tables
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-7500@lists.php.net to get a copy of this message
If the tables have a one to many relationship you can use nested while loops: while(1st loop){ // display header - master table while(2nd loop){ // display features - detail table } } HTH Jayme. -----Mensagem Original----- De: olinux <olnx@yahoo.com> Para: PHP-DB <php-db@lists.php.net> Enviada em: quinta-feira, 15 de março de 2001 01:05 Assunto: [PHP-DB] 2 while loops - 2 tables I am trying to display info from 2 tables in a search result format. My problem is... how do I get the variables to display together? for example: A user selects a city to search apartments in. $sql = "SELECT * FROM $table_name WHERE apt_city = \"$apt_city\" "; $result = mysql_query($sql, $connection); while ($row = mysql_fetch_array($result)) { $aptID = $row['aptID']; $apt_address1 = $row['apt_address1']; $apt_address2 = $row['apt_address2']; $apt_zip = $row['apt_zip']; } while ($row = mysql_fetch_array($result)) { $apt_ID = $row['apt_ID']; $feature1 = $row['feature1']; $feature2 = $row['feature2']; $feature3 = $row['feature3']; } Now I want to display the approriate data from the apartment next to the features. so What I am wondering is how can i "group" the appropriate features with the correct apartment so say a result may look like this [HTML table] ______________________________________________ Apartment 1 | feature1 apt_address1 | feature2 apt_address2 | feature3 _________________|_____________________________ Apartment 2 | feature1 apt_address1 | feature2 apt_address2 | feature3 _________________|_____________________________ Apartment 3 | feature1 apt_address1 | feature2 apt_address2 | feature3 _________________|_____________________________ Should the 2nd while loop be placed inside the first? while ($row = mysql_fetch_array($result)) { $aptID = $row['aptID']; $apt_address1 = $row['apt_address1']; $apt_address2 = $row['apt_address2']; $apt_zip = $row['apt_zip']; while ($row = mysql_fetch_array($result)) { $apt_ID = $row['apt_ID']; $feature1 = $row['feature1']; $feature2 = $row['feature2']; $feature3 = $row['feature3']; } $table_block .= "[ this contains HTML code for table rows to display data ]"; } Thanks Much! olinux

« previous php.db (#7500) next »