Solved

PHP While Loop

Posted on 2012-03-27
2
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 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