5

I was trying to display an array in php to an HTML table but there's an issue. I found several examples here in stack overflow, but they don't work for my case.

Controller:

<?php include('inc/db_connect.php');?>

<?php
try
{
  $sql = "SELECT id GroupName, VideoTITLE, ByArtist FROM videoclip";
  $result = $pdo->query($sql);
}
catch(PDOException $e)
{
  $error = 'unable to fetch data: '.$e->getMessage();
  include'error.html.php';
  exit();
}
$URLS = array();
while ($row = $result->fetch())
{
  $URLS[] = array('id' => $row['id'], 'GroupName' => $row['GroupName'], 'VideoTITLE' => $row['VideoTITLE'], 'ByArtist'=> $row['ByArtist'] );
}

html:

<div id="table_admin" class="span7">
        <h3>Videoclip List</h3>

        <table class="table table-striped table-condensed">

                <thead>
                <tr>
                <th>Song name</th>
                <th>Group name </th>
                <th>Artist </th>
                </tr>
                </thead>
            <?php foreach ($URLS as $URL){
                echo'<tbody>';
                echo'<tr>'; 
                echo'<td>'. $row['VideoTITLE']."</td>";
                echo'<td>'. $row['GroupName'].'</td>';
                echo'<td>'. $row['ByArtist'].'</td>';
                echo'<tr>';
                echo'</tbody>';
              }
            ?>

        </table>

    </div>

4 Answers 4

10

You're close:

</thead>
<tbody>
<?php 
    foreach ($URLS as $URL){
        echo'<tr>'; 
        echo'<td>'. $URL['VideoTITLE']."</td>";
        echo'<td>'. $URL['GroupName'].'</td>';
        echo'<td>'. $URL['ByArtist'].'</td>';
        echo'<tr>';
    }
?>
</tbody>

Since you're taking the values of the $URLS array and calling each one $URL you need to refer to $URL for each row's value. Not the $row variable you originally used to populate the array from the database results.

FYI, you may want to look into htmlentities() to escape your data to help prevent XSS attacks.

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

1 Comment

thanks, this is a personal projects , i was planning to sanitize it after i finish building , just to make sure that i learn it right. Thanks , silly mistake , i never see that , just correct it
2

Assuming your data from the fetchRow is good to go, I see only one major bug in your html: change $row to $URL in your foreach loop. Also, you can mix PHP with HTML to make it a little more pretty:

<?php foreach ($URLS as $URL) { ?>
    <tr>
        <td><?php echo $URL['VideoTITLE']; ?></td>
        <td><?php echo $URL['GroupName']; ?></td>
        <td><?php echo $URL['ByArtist']; ?></td>
    <tr>
<?php } ?>

Comments

1

yea as john said, you have $URLs as $URL, but then your referring to an array calling $row in the for each.

the other thing is that you may want to take the tbody tags outside of the for each loop

Comments

1

After getting the result from database please follow

<?php
  echo '<table>';
  echo '<thead>';
  echo '<tr>';
  echo '<th>Song name</th>';
  echo '<th>Group name </th>';
  echo '<th>Artist </th>';
  echo '</tr>';
  echo '</thead>'
  echo '<tbody>'; 
  foreach($URLS as $URL) {
    echo'<tr>'; 
    echo'<td>'. $URL['VideoTITLE']."</td>";
    echo'<td>'. $URL['GroupName'].'</td>';
    echo'<td>'. $URL['ByArtist'].'</td>';
    echo'<tr>';  
  }
  echo '</tbody>'; 
  echo '</table>'; 
?>

I hope this help you

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.