Bill Flippen
Bill Flippen

Reputation: 453

Why is there and extra able column being inserted?

I am trying to add a column to an existing table and getting screwy results.

I currently have:

print "<table border=1>";
print "<tr>
        <td>First Name</td>
        <td>Last Name</td>
        <td>Email?</td>
        <td>League</td>
        <td>Team</td>
        <td>Captain</td>
        <td></td>
        <td>paid</td>
        <td>wks played</td>
</tr>";
while ($row=mysql_fetch_assoc($results)){
if($row['email_address'] != NULL ){
$email='y';
}
else {
$email='n';
}
if($row[captain_id]==$row[player_num]){ $iscapt="Captain"; }
else{$iscapt="";}
$paid=$row[paid];
if($paid){$playpaid="<td bgcolor=#99ff33>paid</td>";}
else {$playpaid="<td bgcolor=#ff6633>not paid</td>";}

printf ("<tr>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
</tr>",
    $row['first_name'],
    $row['last_name'],
    $email,
    $row['league_id'],
    $row['name'],
    $iscapt,
    $playpaid,
    $row['weeks']
);
}
print "</table>";

For what ever reason when I do not have the <td></td> between captain and paid another column is actually generated.

The original code is:

printf("
<tr>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    </td>
    <td>%s</td>
    %s \n
</tr>",
$row['first_name'],
$row['last_name'],
$email,
$row['league_id'],
$row['name'],
$iscapt,
$playpaid);

Try as I might, I am unable to decipher how the standalone </td> and the standalone %s \n are working to get rid of the "extra" column.

Yes I know this should all be done with CSS and such, but I am just modifying code that is in place. I am not up to the level of being able to re-write the whole page just yet. (almost ready though)

So can someone explain why I need to insert an extra column, and how the out of place <\td> and %s \n are working with the old code?

By the way, if I simply just add a <td>%s</td> and add$row['weeks']` to the end of the statement it still inserts an extra column. I haven't tried putting it in closer to the front of the statement.

Upvotes: 0

Views: 124

Answers (1)

lwitzel
lwitzel

Reputation: 591

The printf() command replaces each "%s" with the subsequent inputs. So the first %s is replaced with $row['first_name'], the second with $row['last_name'], and so on.

The issue is that $playpaid needs to have special conditional formatting associated to it, so the author built the <td></td> tags specifically for that column. So you can't just put the resulting value withing <td>%s</td>, because then you end up with double <td><td>, as mentioned in the comments above.

The original code appears to have an extra </td> in it. Try the following:

printf("
<tr>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    %s
    <td>%s</td> \n
</tr>",
$row['first_name'],
$row['last_name'],
$email,
$row['league_id'],
$row['name'],
$iscapt,
$playpaid,
$row['weeks']);

The \n at the end simply adds a carriage return to the resulting HTML.

As for the extra column, that's because you have an extra blank <td></td> in the table header row.

Upvotes: 1

Related Questions