makenoiz Posted February 10, 2009 Share Posted February 10, 2009 Hi there - I have two tables and Im comparing one field from each db in order to create a bunch of drop down that have "selected" values. here are the tables: Table A +--------------+--------------------+ + id description +--------------+--------------------+ + 1 Hot +--------------+--------------------+ + 2 Cold +--------------+--------------------+ + 3 Wet +--------------+--------------------+ + 4 Dry +--------------+--------------------+ Table B +--------------+----------------+-------------------+----------------+-------------------+ + Date svcIssueCatID1 svcIssueCatNotes1 svcIssueCatID2 svcIssueCatNotes2 +--------------+----------------+-------------------+----------------+-------------------+ + 2009-02-01 1 It was too hot 4 It was too dry +--------------+----------------+-------------------+----------------+-------------------+ I know there is a more elegant way to code this using loops but Im not sure where to start. Everytime I start I end up with a mess. I need one loop to get all the categoried from table A then another loop to create each of the 3 sets of drp down/text fields Can anyone point me in the right direction? Thanks <?PHP $query = "SELECT `svcIssueCatID1`, `svcIssueCatNotes1`, `svcIssueCatID2`, `svcIssueCatNotes2`,`svcIssueCatID3`, `svcIssueCatNotes3`,FROM `tableB` WHERE `date`=\"".$selDate."\" "; $result = mysql_query($query); $row = mysql_fetch_array($result); $tableBID_1 = $row['svcIssueCatID1']; $tableBID_2 = $row['svcIssueCatID2']; $tableBID_3 = $row['svcIssueCatID3']; $tableBID_4 = $row['svcIssueCatID4']; $tableBNotes_1 =$row[svcIssueCatNotes1]; $tableBNotes_2 =$row[svcIssueCatNotes2]; $tableBNotes_3 =$row[svcIssueCatNotes3]; $tableBNotes_4=$row[svcIssueCatNotes4]; $options=""; $query_tableA = "SELECT * from tableA"; $result_tableA = mysql_query($query_tableA); while ($row_tableA = mysql_fetch_array($result_tableA)) { $tableAID = $row_tableA['id']; $tableADesc = $row_tableA['description']; if ($tableAID == $tableBID_1 ){ $isSelected = "selected"; } $options.="<OPTION VALUE=\"".$tableAID."\" ".$isSelected.">".$tableADesc."</option>"; $isSelected = ""; } ?> <SELECT name="CatID_1"><?=$options?> </SELECT> <textarea name="Notes_1" cols="50" rows="6"><?=$tableBNotes_1; ?></textarea> <? $options=""; $query_tableA = "SELECT * from tableA"; $result_tableA = mysql_query($query_tableA); while ($row_tableA = mysql_fetch_array($result_tableA)) { $tableAID = $row_tableA['id']; $tableADesc = $row_tableA['description']; if ($tableAID == $tableBID_2 ){ $isSelected = "selected"; } $options.="<OPTION VALUE=\"".$tableAID."\" ".$isSelected.">".$tableADesc."</option>"; $isSelected = ""; } ?> <SELECT name="CatID_2"><?=$options?> </SELECT> <textarea name="Notes_2" cols="50" rows="6"><?=$tableBNotes_2; ?></textarea> <? $options=""; $query_tableA = "SELECT * from tableA"; $result_tableA = mysql_query($query_tableA); while ($row_tableA = mysql_fetch_array($result_tableA)) { $tableAID = $row_tableA['id']; $tableADesc = $row_tableA['description']; if ($tableAID == $tableBID_3 ){ $isSelected = "selected"; } $options.="<OPTION VALUE=\"".$tableAID."\" ".$isSelected.">".$tableADesc."</option>"; $isSelected = ""; } ?> <SELECT name="CatID_3"><?=$options?> </SELECT> <textarea name="Notes_3" cols="50" rows="6"><?=$tableBNotes_3; ?></textarea> ......... then later I update table B Quote Link to comment https://forums.phpfreaks.com/topic/144565-loop-help-please/ Share on other sites More sharing options...
sasa Posted February 10, 2009 Share Posted February 10, 2009 try <?php // 1st create unselected options $options=""; $query_tableA = "SELECT * from tableA"; $result_tableA = mysql_query($query_tableA); while ($row_tableA = mysql_fetch_array($result_tableA)) { $tableAID = $row_tableA['id']; $tableADesc = $row_tableA['description']; $options.="\t<OPTION VALUE=\"$tableAID\">$tableADesc</option>\n"; } $query = "SELECT `svcIssueCatID1`, `svcIssueCatNotes1`, `svcIssueCatID2`, `svcIssueCatNotes2`,`svcIssueCatID3`, `svcIssueCatNotes3`,FROM `tableB` WHERE `date`=\"".$selDate."\" "; $result = mysql_query($query); while ($row = mysql_fetch_array($result)){ $tableBID[1] = $row['svcIssueCatID1']; $tableBID[2] = $row['svcIssueCatID2']; $tableBID[3] = $row['svcIssueCatID3']; $tableBID[4] = $row['svcIssueCatID4']; $tableBNotes[1] =$row[svcIssueCatNotes1]; $tableBNotes[2] =$row[svcIssueCatNotes2]; $tableBNotes[3] =$row[svcIssueCatNotes3]; $tableBNotes[4] =$row[svcIssueCatNotes4]; for ($i = 1; $i <= 4; $i++); $search = "<OPTION VALUE=\"".$tableBID[$i]."\">"; $replace = "<OPTION VALUE=\"".$tableBID[$i]."\" selected=\"selected\">"; echo "<SELECT name=\"CatID_$i\">\n"; echo str_replace($search, $replace, $options); echo "</SELECT>\n"; echo "<textarea name=\"Notes_$i\" cols=\"50\" rows=\"6\">".$tableBNotes[$i]."</textarea>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144565-loop-help-please/#findComment-758774 Share on other sites More sharing options...
makenoiz Posted February 10, 2009 Author Share Posted February 10, 2009 Thank you.. I will try this today and let you know right away. I really appreciate your suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/144565-loop-help-please/#findComment-758983 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.