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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.