PHP While Loop

In my while loop it puts the header to the tables after each username instead of just the first row.

while ($line= mysql_fetch_array($result))
	 {
	echo '<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="400" cellpadding="3" cellspacing="3">
	<tr>
		<td>Username</td>
		<td>Thread Count</td>
		<td>Post Count</td>
		<td>Points Total</td>
	</tr>
	</table>';
	echo '<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="400" cellpadding="3" cellspacing="3"> 
	<tr>
		<td>' . $line['author_name'] . '</td>
		<td>' . $line['thread_total'] . '</td>
		<td>' . $line['post_total'] . '</td>
		<td>' . (($line['post_total'] * $posts) + ($line['thread_total'] * $topics)) . '</td>
	</tr>
  	</table>';
	}

Open in new window


Also how can I make the columns line up like maybe a set width or something?
LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Like this...

echo '<table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="400" cellpadding="3" cellspacing="3">
	<tr>
		<td>Username</td>
		<td>Thread Count</td>
		<td>Post Count</td>
		<td>Points Total</td>
	</tr>';

while ($line= mysql_fetch_array($result))
	 {
	echo '<tr>
		<td>' . $line['author_name'] . '</td>
		<td>' . $line['thread_total'] . '</td>
		<td>' . $line['post_total'] . '</td>
		<td>' . (($line['post_total'] * $posts) + ($line['thread_total'] * $topics)) . '</td>
	</tr>';
	}
echo   '</table>';

Open in new window

0
Ray PaseurCommented:
DaveBaldwin has shown you the right way to get things not to repeat -- do not put those parts of the programming inside a loop structure like while() or foreach()!

But you also asked about lining up columns.  The "deep background" of this question might be that you are trying to learn PHP, HTML, and CSS all at once by reading other people's code or experimenting with your own code.  That is a terrible way to learn programming, especially when there are so many complex interactions between the different technologies.  It's almost like trying to learn how to make an apple pie by looking at apple pies.  Sure, there are apples, but now what?  What else do we need, what processes do we follow, how long will it take, etc?

Instead of trying to learn all this stuff yourself, find a local community college or university extension and take some classes.  I would take them in the following order to build knowledge that will enable you to move from each step to the next step.  

1. Introduction to HTML: Principles of Semantic Markup
2. Introduction to CSS: Selectors and Properties
3. Introduction to Computer Science
4. Introduction to Computer Science II
5. Introduction to Object-Oriented Programming
6. Introduction to PHP
7. Intermediate PHP
8. Introduction to JavaScript
9. Using jQuery to animate the Browser
10. HTML5
11. CSS3
12. Developing for mobile devices

If you cannot find structured learning resources like college classes, then you will be forced to manage your education by yourself and that will make it take a lot longer.  There are many online resources for learning these things (W3Schools, Tizag) and those may be helpful.  Search Amazon.com for well-reviewed books on the topics, but be careful to pay attention to the dates of publication - a book about PHP4 is only useful for mulch.

My professional library is about 11 shelf-feet wide.  In total, it cost an average of $40 per inch.  Many of my books are from SitePoint.  I like their examples and writing style. Not all of them are "superb" but all are worth reading.

I know PHP fairly well but I almost always do the other stuff with a book or two at my elbow.  You can't know everything.  Don't despair at the challenge.  If you commit yourself to learning you will be professionally employable within a year or two, and in due time you could become an expert.

In the case of "how do I control width" the answer is you use CSS selectors to apply a class to the <td> tags, something like this
<td class="myColumns">

Open in new window

and in your style sheet you write something like this.
.myColumns { width:32em; }

Open in new window

If you intend to develop for mobile devices you will want to stay away from explicit widths and rely mostly on percentages.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.