0

I'm trying to display

$row["name"]

through the form of a HTML Table, like this:

echo "<html>
<table class="."table".">
  <tr>
    <td>".$row["name"]."</td>
  </tr>
</table>
</html>";

I'm getting the row variable from here, too:

$query = "SELECT * FROM servers WHERE public = 1";

if ($result = $db->query($query)) {

while ($row = $result->fetch_assoc()) {
    printf ("Server name: %s", $row["name"].'<br>');
    printf ("Hostname: %s", $row["host"].'<br>');
    printf ("Port: %s", $row["port"].'<br>');
    printf ("Player count: %s", $row["players"].'<br>');
    printf ("Server Status: %s", $row["status"].'<br>');
    printf ("Last pinged: %s", $row["last_ping"].'<br>');
    printf ("Current ms: %s", $row["ms"].'<br>');

}

$result->free();
}

It does successfully display the information printed, but doesn't seem to be able to put it into a table.

https://i.sstatic.net/R78cg.png

9
  • The second code isn't being put into a table it looks like. Commented Dec 20, 2013 at 17:19
  • @HarryDenley if you look at the imgur link, it's still creating the table, just not displaying the $row["name"]. I've tried multiple ways of it, it's having none of it. Commented Dec 20, 2013 at 17:21
  • 2
    <table class="."table"."> ? – Try <table class=\"table\"> or <table class='table'> More than likely the issue. Commented Dec 20, 2013 at 17:21
  • 1
    Have you tried var_dump($row) before you output the HTML, to check the contents of the $row variable? Commented Dec 20, 2013 at 17:22
  • 1
    Yeah, the table row there would be <table class=table>. That's not valid html since attributes must be quoted Commented Dec 20, 2013 at 17:25

4 Answers 4

1

You never actually assign any of the row values in the WHILE loop to a variable. If the SQL statement returns more than 1 row, you need to build an array in your WHILE loop to use for later.

Define your array first

$myarray = array();

Then assign the object returned from the SQL statement to the array

while($row = $result->fetch_assoc()){
    $myarray[] = $row;
    //your other printf statements if you want to keep them
}

Now you have a populated $myarray where the first level key is the row number. Since you are probably building a dynamic table (one whose size depends on the amount of information returned), you'll also need to incorporate PHP into the process.

<table class = "table">
    <?php
        for($i = 0; $i < sizeof($myarray); $i++){
             echo "<tr>";
                 echo "<td>";
                      echo $myarray[$i]["name"];
                 echo "</td>";
             echo "</tr>";
        } 
    ?>
</table>

In the above, you run a for loop on every item in $myarray and for each row returned from your original database pull, a new table row and corresponding table column are created.

Sign up to request clarification or add additional context in comments.

2 Comments

Managed to get it working, really need to refresh my PHP, not done it for so long. :L Thanks for the help, greatly appreciated. :)
~ Another Happy Ending ~
1

Have you tried taking the whole table out of the php echo statement?

So having it as html then just echoing the row inside the table

<td><?php echo $row["name"] ?></td>

Comments

0

you have to put the table row <tr></tr> inside the while loop and use separate <td> columns for each data field.

Sample code:

echo '<table>';
while{$row}
{
 echo '<tr>
       <td>'.$row['name'].'</td>
       </tr>';
}
echo '</table>';

Comments

0

this:

echo "<html>
<table class="."table".">
  <tr>
    <td>".$row["name"]."</td>
  </tr>
</table>
</html>";

will echo this:

<html>
<table class=table>
  <tr>
    <td>name</td>
  </tr>
</table>
</html>

thats not valid html

as suggested by Kyle Goslan you should try it like this:

<html>
<table class="table">
  <tr>
    <td><?php echo $row['name']; ?></td>
  </tr>
</table>
</html>

this is how i would do it:

<html>
<table class="table">
   <tr>
      <th>Name</th>
   </tr>
   <?php
$query = "SELECT * FROM servers WHERE public = 1";

if ($result = $db->query($query)) {

    while ($row = $result->fetch_assoc()) { ?>
        <tr>
           <td>
              <?php echo $row['name']; ?>
           </td>
       </tr>
   <?php }
}

$result->free();
?>

2 Comments

I was just about to make a comment about that. I wasn't going to put in an actual answer, because questions like these tend to open up both a Pandora's box and the proverbial "can of worms".
Yeah, i've previously tried that, but it's done nothing. The table is displaying, but blank. It's not getting the variable, I don't understand why.

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.