?
Solved

PHP While Loop

Posted on 2012-03-27
2
Medium Priority
?
334 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:Nathan Riley
[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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.
Suggested Courses

764 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