0

I have this code to get all the data from my products table. It's working great like this:

if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo $row['id'];
            echo $row['country'];
            echo $row['price'];
            echo $row['games'];
            echo $row['plus'];
            echo $row['buylink'];
            echo "<br/>";
        }

But I want these results to be shown in a table inside the body. I tried something but it's not working.

This is how the table looks :

<table class="table table-hover table-striped table-condensed">
<thead>
  <tr>
    <th>ID</th>
    <th>Country</th>
    <th>Price</th>
    <th>Games</th>
    <th>Plus</th>
    <th>Buy Link</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td><?php echo $row['id']; ?></td>
    <td><?php echo $row['country']; ?></td>
    <td><?php echo $row['price']; ?></td>
    <td><?php echo $row['games']; ?></td>
    <td><?php echo $row['plus']; ?></td>
    <td><?php echo $row['buylink']; ?></td>

  </tr>
</tbody>

6
  • What did you try? What happened? If the <td><?php echo $row['id']; ?></td> is your attempts you need to have that in the while. Commented Feb 20, 2017 at 22:36
  • The code inside the <tbody> it's not working, not showing anything Commented Feb 20, 2017 at 22:37
  • Where is the while? How does the first code chunk relate to the second? Commented Feb 20, 2017 at 22:37
  • The first code contains the while code.. Commented Feb 20, 2017 at 22:39
  • So the first code does nothing with the second code? $row is undefined in that case. Commented Feb 20, 2017 at 22:41

3 Answers 3

3
<?php
if ($result->num_rows > 0) {
?>
<table class="table table-hover table-striped table-condensed">
  <thead>
    <tr>
      <th>ID</th>
      <th>Country</th>
      <th>Price</th>
      <th>Games</th>
      <th>Plus</th>
      <th>Buy Link</th>
    </tr>
  </thead>
  <tbody>
  <?php while($row = $result->fetch_assoc()){ ?>
    <tr>
      <td><?php echo $row['id']; ?></td>
      <td><?php echo $row['country']; ?></td>
      <td><?php echo $row['price']; ?></td>
      <td><?php echo $row['games']; ?></td>
      <td><?php echo $row['plus']; ?></td>
      <td><?php echo $row['buylink']; ?></td>
    </tr>
  <?php }?>
  </tbody>
</table>
<?php
}else {
 // if there is no result... Code something here.
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I dont get any errors now, but the table is still empty.. I dont know what may be the problem
find <?php }?> </tbody> and change <?php } ?> </tbody> and try again pls. Maybe an error bcs of space. This code must work great. There must not be any problem. If you have a problem again please upload your .php file to yandisk and lets look. I think you wrote an unnecessary comma to somewhere in your codes.
0

Youre getting close. You need to have the loop spit out the rows into the html.

<table class="table table-hover table-striped table-condensed">
<thead>
  <tr>
    <th>ID</th>
    <th>Country</th>
    <th>Price</th>
    <th>Games</th>
    <th>Plus</th>
    <th>Buy Link</th>
  </tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_row()) {
    echo "<tr>";
    foreach ($row as $item) {
        echo "<td>$item</td>";
    }
    echo"</tr>";
}

?>
</tbody>

also instead of using a associative array and indexing into it (ie. $row['country']), You can use a normal array and a foreach loop to cut down on the code.

8 Comments

Tryed that but i get this error : Warning: mysqli_result::fetch_row(): Couldn't fetch mysqli_result..
Above this code in <?php ?> tags do you have something like this? $mysql = new mysqli(); $mysql->connect('localhost', 'xxx', 'xxxx', 'xxx'); $sql = "SQL GOES HERE"; $result = $mysql->query($sql);
Yes i do i have the connection in other file db.php and i do require 'db.php' on the top.. As i sayd the first code i posted works! it displays the data but outside the table on top of my site.. i want it to be displayed in the table $sql = "SELECT id, country, price, games, plus, buylink FROM products"; if ($result=mysqli_query($mysqli,$sql)) { // Return the number of rows in result set $rowcount=mysqli_num_rows($result); // Free result set } this code works fine
Did you used the variable name $result twice or reassign it?
I used it twice
|
0

LE: Each html table row is created based on each database's table row. So, you just need to loop through the result set (that while), and for each iteration of the loop, you need to create a new html table row:

<table class="table table-hover table-striped table-condensed">
    <thead>
        <tr>
            <th>ID</th>
            <th>Country</th>
            <th>Price</th>
            <th>Games</th>
            <th>Plus</th>
            <th>Buy Link</th>
        </tr>
    </thead>
    <tbody>
        <?php while($row = $result->fetch_assoc()){ ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['country']; ?></td>
                <td><?php echo $row['price']; ?></td>
                <td><?php echo $row['games']; ?></td>
                <td><?php echo $row['plus']; ?></td>
                <td><?php echo $row['buylink']; ?></td>
            </tr>
        <?php } ?>
    </tbody>
</table>

1 Comment

While this code might provide answer to the question, it is always better to add some explanation to your code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.