table header <hd>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nadeem14375
Forum Newbie
Posts: 23
Joined: Sat Oct 30, 2010 2:11 am

table header <hd>

Post by nadeem14375 »

Dear all,

i have the following code to show a table header:

while ($get_row = mysql_fetch_assoc($get))
{
echo '<table border="1" cellpadding="2" cellspacing="2" width="200">
<tr>
<th>'.'Name'.'</th>
<th>'.'Phone No'.'</th>
</tr>
<tr>
<td width="50%">'. $get_row['name'].'</td>
<td width="50%" align="right"> '.$get_row['num'].'</td>
</tr>
</table>';
}

it repeats the heading on every row of the column:

Name Phone No
ahmed 34343433
Name Phone No
gul 3434343432
Name Phone No
shah 99228282
Name Phone No
nawaz 838372777
Name Phone No
waheed 4774474
Name Phone No
Wali 87222727

the heading should be only on first row.

what's the wrong?

Regards:
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: table header <hd>

Post by oscardog »

Here you go...

Code: Select all

$count = 0;
echo '<table border="1" cellpadding="2" cellspacing="2" width="200">';
while ($get_row = mysql_fetch_assoc($get))
{
if($count == 0) {
echo '<tr>
<th>'.'Name'.'</th>
<th>'.'Phone No'.'</th>
</tr>';
}
echo '<tr>
<td width="50%">'. $get_row['name'].'</td>
<td width="50%" align="right"> '.$get_row['num'].'</td>
</tr>';
$count++;
}
echo '</table>';
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: table header <hd>

Post by social_experiment »

Or the code to create the table header can be placed outside the while loop.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
nadeem14375
Forum Newbie
Posts: 23
Joined: Sat Oct 30, 2010 2:11 am

Re: table header <hd>

Post by nadeem14375 »

thanks

it should be out side the while loop.
social_experiment wrote:Or the code to create the table header can be placed outside the while loop.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: table header <hd>

Post by oscardog »

social_experiment wrote:Or the code to create the table header can be placed outside the while loop.
...I am having a slow day. Literally cannot believe I moved the table tags outside and not the headers :banghead:
Post Reply