Page 1 of 1

PHP Nested Loop Help

Posted: Sat Jan 07, 2006 4:24 am
by goldmoon
twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Trying to implement a nested loop in my code.

The code I have so far displays:


PracticeName...................................PracticeName2
Address...........................................Address
City, State Zip.................................City, State Zip
Phone#...........................................Phone #
Office Link........................................Office Link


The way I want it to look is, it displays all the names that pertain to that particular id; 

PracticeName...................................PracticeName2
Address...........................................Address
City, State Zip.................................City, State Zip
Phone#...........................................Phone #
Name1............................................Name1
Name2
Name3
Office Link........................................Office Link


Here is the code that I have - a multicolumn display:

Code: Select all

<?php

//set number of columns
$columns=2;
mysql_connect("localhost","root","");
mysql_select_db("db");
$query="SELECT distinct l.id, n.id, l.practicename, l.address1, l.city, l.abbrev, l.zip, l.phone, n.names FROM location l, names n WHERE l.id = n.id AND abbrev = 'MI' AND LineOfBusiness = 'General Practice' ORDER BY practicename";
$result=mysql_query($query);
$lineofbusiness='General Practice - Michigan';
$num_rows=mysql_num_rows($result);
echo "<table border=\"0\" align=\"center\" class=\"content10pt\" width=\"500\"><tr class=content10pt><td class=header2 align=center><br>$lineofbusiness</td></tr></table><table border=\"0\" align=\"center\" class=\"content10pt\" width=\"500\"><tbody><br>\n";

for($i=0;$i<$num_rows;$i++) {
$row=mysql_fetch_array($result);
if($i%$columns==0) {
echo "<tr class=\"content10pt\">\n";
}

echo "<td>" . $row['practicecname'] ."<br>" . $row['address1'] . "<br>" . $row['city'] . ", " . $row['abbrev'] . " " . $row['zip'] . "<br>" . "Phone: ". $row['phone'] . "<br><a href=http://./office_details2.php?id=".$row['id'].">Office Details</A><br><br><br>\n";

if(($i%$columns)==($columns-1) || ($i+1)==$num_rows) {
echo "</tr>\n";
}
}
echo "</tbody></table>\n";

?>

Not sure where to put the nested loop to display those Names. Please help. Thanks.


twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Jan 07, 2006 7:33 am
by pilau
Put the loop where you want the names to be printed out. That's my answer to what I understood from your question. But maybe you can be more clear?

Re: PHP Nested Loop Help

Posted: Sat Jan 07, 2006 12:17 pm
by goldmoon
What I'm trying to display is (there are multiple listings displayed):

PracticeName...................................PracticeName2
Address...........................................Address
City, State Zip.................................City, State Zip
Phone#...........................................Phone #

-->(here would be an inner loop that would display all the Names pertaining to that practice name)
---<(separate query - "select names from name where id=$row['id'] )
Name1............................................Name1
Name2
Name3
Office Link........................................Office Link


What I currently have is a 2 column display, and not sure how to display that inner loop within a multicolumn display.


Hope that clarified it a bit. If you could help that would be great. Thanks.

goldmoon wrote:twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Trying to implement a nested loop in my code.

The code I have so far displays:


PracticeName...................................PracticeName2
Address...........................................Address
City, State Zip.................................City, State Zip
Phone#...........................................Phone #
Office Link........................................Office Link


The way I want it to look is, it displays all the names that pertain to that particular id; 

PracticeName...................................PracticeName2
Address...........................................Address
City, State Zip.................................City, State Zip
Phone#...........................................Phone #
Name1............................................Name1
Name2
Name3
Office Link........................................Office Link


Here is the code that I have - a multicolumn display:

Code: Select all

<?php

//set number of columns
$columns=2;
mysql_connect("localhost","root","");
mysql_select_db("db");
$query="SELECT distinct l.id, n.id, l.practicename, l.address1, l.city, l.abbrev, l.zip, l.phone, n.names FROM location l, names n WHERE l.id = n.id AND abbrev = 'MI' AND LineOfBusiness = 'General Practice' ORDER BY practicename";
$result=mysql_query($query);
$lineofbusiness='General Practice - Michigan';
$num_rows=mysql_num_rows($result);
echo "<table border="0" align="center" class="content10pt" width="500"><tr class=content10pt><td class=header2 align=center><br>$lineofbusiness</td></tr></table><table border="0" align="center" class="content10pt" width="500"><tbody><br>\n";

for($i=0;$i<$num_rows;$i++) {
$row=mysql_fetch_array($result);
if($i%$columns==0) {
echo "<tr class="content10pt">\n";
}

echo "<td>" . $row['practicecname'] ."<br>" . $row['address1'] . "<br>" . $row['city'] . ", " . $row['abbrev'] . " " . $row['zip'] . "<br>" . "Phone: ". $row['phone'] . "<br><a href=http://./office_details2.php?id=".$row['id'].">Office Details</A><br><br><br>\n";

if(($i%$columns)==($columns-1) || ($i+1)==$num_rows) {
echo "</tr>\n";
}
}
echo "</tbody></table>\n";

?>

Not sure where to put the nested loop to display those Names. Please help. Thanks.


twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/quote]