1

I'm trying to neatly organize data from my MySQL database into tables. I'm almost there, but experiencing some troubles with my use of a while loop.

Posting my simple code:

<link type="text/css" rel="stylesheet" href="style.css"/>

<?PHP 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
    WHILE($rows = mysql_fetch_array($query)):

        $username = $rows['username'];
        $password = $rows['password'];
        $email = $rows['email'];

    echo "
    <table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead>
    <tr>
    <td>$username</td>
    <td>$password</td>
    <td>$email</td>
    ";
    endwhile;
?>

This gives me an output of something like this:

Username    Password    Email Adress

test           123  [email protected]

Username    Password    Email Adress

testies 123 [email protected]

Username    Password    Email Adress

tested  12345   [email protected]

The obvious problem here is that my headers are printed inside the while loop, and thus I get new headers for every entry. This looks really stupid though, and I would prefer to have the headers only appear once. Any ideas on how to do this?

I have tried to start the table with headers before the while loop, but then the later <td>s wont correspond with the headers width, since the program doesn't recognize them as the same table, and thus not needing to match.

4
  • 5
    take out header code outside the loop. Commented Apr 30, 2014 at 9:30
  • 1
    How would your headers outside the loop not correspond with the result? they are hardcoded and not dependend on your resultset. Commented Apr 30, 2014 at 9:32
  • This is very Basic, so i'd advise you to do some more reading on begginer tutorials for programing in general. Commented Apr 30, 2014 at 9:35
  • Yeah, i realise this is very basic, sorry if it doesnt fit the website. Thanks for chiming in anyways tho, ill definately go read some more :) Commented Apr 30, 2014 at 10:31

4 Answers 4

2

Use this

<link type="text/css" rel="stylesheet" href="style.css"/>

<?PHP 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users"); ?>
<table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead><?php 

$query = mysql_query("SELECT * FROM users");
    WHILE($rows = mysql_fetch_array($query)) { ?>                    
    <tr>
    <td><?php echo $rows['username']; ?></td>
    <td><?php echo $rows['password']; ?></td>
    <td><?php echo $rows['email']; ?></td>
    </tr>
<?php } ?>    
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much for the help :) I'm impressed how many responses i got and how quick people were about it! As fas as i can understand, the trick is in the opening and closing of the php brackes, so that the table is always in the HTML portion of the code, is this correct?
Yes the html portion which need not be repeated should not be in a loop such that it is printed only once. The things which are to be itereated should be in the loop. Like in your case you needed only one THEAD so it is above the loop and you needed to repeat the TR so it is inside the loop. So as many TR(table rows) are generated as there are loop iterations. Glad you solved it
0

change your code as follows.

<?PHP 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
  echo "
    <table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead>";
WHILE($rows = mysql_fetch_array($query)):
        $username = $rows['username'];
        $password = $rows['password'];
        $email = $rows['email'];


echo"<tr>
    <td>$username</td>
    <td>$password</td>
    <td>$email</td>
    ";
    endwhile;
?>

Comments

0

print the headers before the loop.

<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</tr>
</thead>
<?php 
$connect = mysql_connect("localhost","root","");

mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
    WHILE($rows = mysql_fetch_array($query)):

        $username = $rows['username'];
        $password = $rows['password'];
        $email = $rows['email'];

        echo "
        <tr>
        <td>$username</td>
        <td>$password</td>
        <td>$email</td>
        </tr>
        ";
    endwhile;
?>
</table>

Comments

0

Try this:

<link type="text/css" rel="stylesheet" href="style.css"/>

<?php 
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
?>
    <table>
    <thead>
    <th>Username</th>
    <th>Password</th>
    <th>Email Adress</th>
    </thead>";
<?php
WHILE($rows = mysql_fetch_array($query)):

    $username = $rows['username'];
    $password = $rows['password'];
    $email = $rows['email'];
echo "<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.