Solved

PHP While Loop

Posted on 2012-03-27
2
330 Views
Last Modified: 2012-03-28
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?
0
Comment
Question by:N R
2 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 37774514
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 37776362
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question