0

I've put together the following code which creates a table upon a selection being made with a drop down menu.

echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

The problem I've got is that for each record that is returned the 'headers' are repeated. The live page can be found here. I just wonder whether someone could perhaps take a look at this please and tell me where I've gone wrong.

Apologies for the really simple question, but I've been looking at this for a while and I just can't find the answer. I think it just needs a fresh pair of eyes to look at it.

0

7 Answers 7

4

Try this, you just need to get headers out of while loop

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";

$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){

echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

Comments

4

The answer is obvious, you are repeating the output of the headers in the loop. Move the

while($rows=mysql_fetch_array($result)){

after the first

echo "</tr>";

Comments

4

You need to put the headers outside of the while loop:

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";

$result = mysql_query($query);

while ($rows = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $rows['findname'] . "</td>";
    echo "<td>" . $rows['finddescription'] . "</td>";
    echo "</tr>";
}

echo "</table>";

Comments

3

This should work:

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>".$rows['findname']."</td>";
    echo "<td>".$rows['finddescription']."</td>";
    echo "</tr>";
    }
echo "</table>";

Comments

3

Your headers are being repeated because you are writing them inside the loop, for each row returned by the query. You simply need to move your headers outside the loop so they're only written once, before the rows returned by the query start printing:

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";

$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
  echo "<tr>";
  echo "<td>".$rows['findname']."</td>";
  echo "<td>".$rows['finddescription']."</td>";
  echo "</tr>";
}
echo "</table>";

Comments

3

headers are repeating becasue they are in while loop, it should work well

echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){

echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

Comments

2

Change to:

echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
while($rows=mysql_fetch_array($result)){
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";

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.