jasper1106 Posted July 31, 2010 Share Posted July 31, 2010 Hi guys, New to PHP but learning every day. This is probably a really newbie error but I've tried multiple different ways and can't get it to work. $tableData = ''; if(mysql_num_rows($result)==0) { $tableData = "<tr><td colspan=\"4\">No results returned.</td></tr>\n"; } else { while($row = mysql_fetch_assoc($result)) { $tableData .= " <tr bgcolor='#FFFFFF' align='center'>\n"; $tableData .= " <td>{$rank}</td>\n"; $tableData .= " </tr>\n"; $rank = ($row['rank']); if ($rank == 0) { print "Cadet"; } if ($rank == 1) { print "Sergeant"; } if ($rank == 2) { print "2nd Lieutenant"; } if ($rank == 3) { print "1st Lieutenant"; } if ($rank == 4) { print "Captain"; } if ($rank == 5) { print "Major"; } if ($rank == 6) { print "Lt. Colonel"; } if ($rank == 7) { print "Colonel"; } } } ?> <table class="tables" cellspacing="0" cellpadding="0"> <tr> <th>Rank</th> </tr> <?php echo $tableData; ?> </table> When this script is run the $rank variable shows the numbers 1,2,3,4,5,6,7 instead of the textual translations for example if 0 is displayed then "cadet" should be shown instead. The current script shows the numbers in the table but prints the accurate translations at the top outside the table tag..... Can anyone show me what I've done wrong here? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/209442-if-exchange-placements-what-have-i-done-wrong/ Share on other sites More sharing options...
jcbones Posted July 31, 2010 Share Posted July 31, 2010 $tableData .= " <tr bgcolor='#FFFFFF' align='center'>\n"; $tableData .= " <td>{$rank}</td>\n"; //<--Printing out rank, before changing it. Try it this way: //top of script. $ranks = array('Cadet','Sergeant','2nd Lieutenant','1st Lieutenant','Captain','Major','Lt. Colonel','Colonel'); //inside the while loop, replace these lines. $tableData .= " <tr bgcolor='#FFFFFF' align='center'>\n"; $tableData .= '<td>' . $ranks[$rank] . '</td>' . "\n"; Quote Link to comment https://forums.phpfreaks.com/topic/209442-if-exchange-placements-what-have-i-done-wrong/#findComment-1093638 Share on other sites More sharing options...
jasper1106 Posted July 31, 2010 Author Share Posted July 31, 2010 Ah I never thought about converting it so the array counts it, thank you jc! Quote Link to comment https://forums.phpfreaks.com/topic/209442-if-exchange-placements-what-have-i-done-wrong/#findComment-1093651 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.