Jump to content

IF exchange placements - what have I done wrong?


jasper1106

Recommended Posts

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.....  :wtf:

Can anyone show me what I've done wrong here? Thank you in advance.

  Quote

$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";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.