Solved

PHP While Loop

Posted on 2012-03-27
2
329 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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
Comment Utility
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 108

Accepted Solution

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
posting form data 3 24
Build JSON from table records 17 38
PHP Sum Column in Table 3 26
Wordpress update causing pages to crash 1 18
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now