Fearpig Posted August 24, 2006 Share Posted August 24, 2006 Hi Guys,Could anyone help me with this one.... I'm trying to display some data in the format of:Department1Name1Name2Name3Deparment2Name4Name5Department3Name6Name7Name8...I've got as far as this code:[code]<?php$db = mysql_connect("localhost", "root", "password");mysql_select_db("Telephonelist",$db);$result = mysql_query("SELECT * FROM tbl_department",$db);$subresult = mysql_query("SELECT * FROM tbl_telephonenumbers",$db);$db = mysql_connect("localhost", "root", "password");mysql_select_db("Telephonelist",$db);$result = mysql_query("SELECT * FROM tbl_department",$db);$subresult = mysql_query("SELECT * FROM tbl_telephonenumbers",$db);while ($row = mysql_fetch_array($result)) { printf("<b>%s </b><br>\n", $row["Department"]); while ($row2 = mysql_fetch_array($subresult)) { if ($row['ID'] == $row2['Department']) { printf("%s %s<br>\n", $row2["First_Name"], $row2["Last_Name"]); } } echo '<br><br>';}?>[/code]But unfortunately I only get:Department1Name1Name2Name3Department2Department3...It look like the subresults are only being processed on the first pass :(. Can anyone point me in the right direction? CheersTom Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 24, 2006 Share Posted August 24, 2006 Your second fetch statement:[code] while ($row2 = mysql_fetch_array($subresult)) { if ($row['ID'] == $row2['Department']) { printf("%s %s<br>\n", $row2["First_Name"], $row2["Last_Name"]); }[/code]reads the entire selection within the first fetch for "Department". It prints out the matching $row['ID'] s and ingnores al the other ones. I would advise to use a JOIN so you can handle all via one select statement.[code]SELECT t1.ID, t2.Department, t2.First_name, t2.Lastname FROM tbl_department as t1, tbl_telephonenumbers at t2WHERE t1.ID=t2.Department ORDER BY t1.Department[/code]Ronald :cool: Quote Link to comment Share on other sites More sharing options...
Fearpig Posted August 24, 2006 Author Share Posted August 24, 2006 Thanks Ronald but I didn't want to display the data that way.... and I managed to solve it myself ;D (I am very chuffed!!)All I needed to do was move the $subresult inside of the loop so that it was processed on each pass![code]<?php$db = mysql_connect("localhost", "root", "password");mysql_select_db("Telephonelist",$db);$result = mysql_query("SELECT * FROM tbl_department",$db);$subresult = mysql_query("SELECT * FROM tbl_telephonenumbers",$db);$db = mysql_connect("localhost", "root", "password");mysql_select_db("Telephonelist",$db);$result = mysql_query("SELECT * FROM tbl_department",$db);//move $subresult from herewhile ($row = mysql_fetch_array($result)) { printf("<b>%s </b><br>\n", $row["Department"]); $subresult = mysql_query("SELECT * FROM tbl_telephonenumbers",$db); while ($row2 = mysql_fetch_array($subresult)) { if ($row['ID'] == $row2['Department']) { printf("%s %s<br>\n", $row2["First_Name"], $row2["Last_Name"]); } } echo '<br><br>';}?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.