Home > database >  How to pass value in html table tag
How to pass value in html table tag

Time:03-12

In my side project I have a list of song titles and keys that I'm pulling from my database and I'm just wondering how can I use that with an HTML table.

I've been playing around inside the <table> but still no luck so I would be really appreciate if I can get any help or suggestion.

enter image description here

<table>
  <tr>
    <th>Title</th>
    <th>Chord</th>
  </tr>

// it will display all the title and chord from here
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Key E</td>
  </tr>
</table>


<?php
    foreach ($songTitle as $song) {
        echo "<a href='details.php?id={$song['id']}'>{$song['title']} {$song['chord']}   <br> </a>";
} ?>

CodePudding user response:

First you need to output the table row in the foreach loop, then just use the 2 values in the 2 <td></td> cell definitions

<table>
  <tr>
    <th>Title</th>
    <th>Chord</th>
  </tr>

// it will display all the title and chord from here
?php
foreach ($songTitle as $song) :
?>
  <tr>
    <td><?php echo $song['title'];?></td>
    <td><?php echo $song['chord']; ?></td>
  </tr>
<?php
endforeach;
?>
</table>


CodePudding user response:

I think this is what are you looking for

<table>
      <tr>
        <th>Title</th>
        <th>Chord</th>
      </tr>
     
      <tr>
        <td>Example Title</td>
        <td>Key E</td>
      </tr>
    </table>
    
    // display the song title and chord
    <?php
              foreach ($songTitle as $song) {

      <tr>
        
      
               echo "<td>{$song['title']}</td><td><a href='details.php?id={$song['id']}'>{$song['title']} {$song['chord']}    </a></td>";
</tr>
    } ?>
  • Related