4

Basically I've retrieved data from mysql on 1 line and made it look like a table with borders and stuff but I'd want it to repeat for every row on the mysql table. Here's the following code I've used so far:

while($info = mysql_fetch_array( $data )) 
 { 
 echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; 

 echo "<tr>


    <td><center><b>".$info['id']."</td></center></b>"."<td><center><b>".$info['username']."</td></center></b>"."<td><center><b>".$info['kills']."</td></center></b>"."<td><center><b>".$info['deaths']."</td></center></b>"."<td><center><b>".$info['ratio']."</td></center></b></tr>"; 

 echo "</table>"; 
 } 

UPDATE:

 echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; 
 while($info = mysql_fetch_array( $data )) 
 { 
 echo "<tr> 
    <td><center><b>".$info['id']."</td></center></b>"."<td><center><b>".$info['username']."</td></center></b>"."<td><center><b>".$info['kills']."</td></center></b>"."<td><center><b>".$info['deaths']."</td></center></b>"."<td><center><b>".$info['ratio']."</td></center></b></tr>"; 

 } 
 echo "</table>"; 
5
  • Move the opening and closing <table> outside the while loop. Commented Sep 8, 2012 at 0:45
  • That should create a new table for each row in your mysql result set. Normally you would create a tr for every result, but either what you should see every row returned from your query with the code you posted. Are you sure theres more than one row being returned? Commented Sep 8, 2012 at 0:46
  • There are 2 rows on my query but it only returns the 2nd one. Commented Sep 8, 2012 at 0:57
  • sorry it was true it works my bad thank you :D Commented Sep 8, 2012 at 0:59
  • goodness me! what is css stuffs, doing after every td. Commented Sep 8, 2012 at 12:05

2 Answers 2

5

You want the table tage outside the loop that fetches the rows of data:

echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; 
while($info = mysql_fetch_array( $data )) 
{ 
    echo "<tr>
    <td><center><b>".$info['id']."</td></center></b>"."<td><center><b>".$info['username']."</td></center></b>"."<td><center><b>".$info['kills']."</td></center></b>"."<td><center><b>".$info['deaths']."</td></center></b>"."<td><center><b>".$info['ratio']."</td></center></b></tr>"; 
} 
echo "</table>"; 

As you are new to PHP I should probably expand this out a little more:

Your code will now echo out the table opening tag:

<table width='1000' cellpadding='10' cellspacing='5' border='1'>

Then go into the while loop and echo out all the rows that it finds:

<tr><td><center><b>Something1></b></td></tr> // shortened for example.
<tr><td><center><b>Something2></b></td></tr>
<tr><td><center><b>Something3></b></td></tr>
<tr><td><center><b>Something4></b></td></tr>

Then when it exists the loop, it will output the closing tag for the table:

</table>

What you were previously doing was outputting a table for each and every row of data. For future reference, it is a good idea to look into your source code to get an understanding of what is happening.

Edit: When I was going through your code comment, I formatted it out neater to see what was going on, you might want to make your code easier to read by having the following format:

echo "<table width='1000' cellpadding='10' cellspacing='5' border='1'>"; 
while($info = mysql_fetch_array( $data )) 
{ 
    echo "<tr> 
        <td><center><b>".$info['id']."</b></center></td>
        <td><center><b>".$info['user‌​name']."</b></center></td>
        <td><center><b>".$info['kills']."</b></center></td>
        <td><center><b>".$info['deaths']."</b></center></td>
        <td><center><b>".$info['‌​ratio']."</b></center></td>
        </tr>"; 
} 
echo "</table>";
Sign up to request clarification or add additional context in comments.

4 Comments

I'm doing it all through the source code. But when I did what you mentioned, it still doesn't work. There are 2 rows in mysql db to be more exact, thought it only fetches the 2nd row...
This is what I've done: Please check question again, i've put updated code
@MirwaisMaarij Good-O. I made an edit that you might want to take a peep at - will make your code easier to read and understand. Also, you should be closing tags in the same order that you open them - ie don't close a <td> tag before you close the </b> tag :)
</center> what it does here? <tr align='center'> or <td align='center'> would do
1

Just move your table tags out of the loop.

echo '<table>';

foreach( $row )
{
  echo '<tr>....</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.