Asked  7 Months ago    Answers:  5   Viewed   30 times

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>

 Answers

27

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.

Wednesday, March 31, 2021
 
Wickethewok
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
100

Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP. However you can simply tell your web server to parse your HTML files as PHP too and then you can run your PHP code in an HTML file wherever you want. Now assuming your Web server is Apache.

Step 1:

Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website

AddType application/x-httpd-php .html

Step 2:

Go to Your HTML file and add an include statement where you want your PHP file to be included

<form method="get" action="latest.php">
<?php include ("yourPHPfile.php"); ?> 
</form>

Step 3:

All done, now go check output in browser.

Saturday, May 29, 2021
 
SpiderLinked
answered 5 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
45

Judging from your array, this might be something like what you're looking for:

<table border="1">
    <thead>
        <tr>
            <th>Name</th>
            <th>Subject</th>
            <th>Test1 Marks</th>
            <th>Test2 Marks</th>
            <th>Total Marks</th>
            <th>Status</th>
            <th>Percentage</th>
            <th>Pass Count</th>      
            <th>Total Percentage</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($arr as $name => $subjects): ?>
            <?php $i = 0; ?>
            <?php foreach($subjects as $subjectName => $subject): ?>
                <?php if (is_array($subject)): ?>
                    <tr>
                        <?php if ($i === 0): ?>
                            <td rowspan="8"><?php echo $name; ?></td>
                        <?php endif; ?>
                        <td><?php echo $subjectName; ?></td>
                        <td><?php echo $subject['test1']; ?></td>
                        <td><?php echo $subject['test2']; ?></td>
                        <td><?php echo $subject['total']; ?></td>
                        <td><?php echo $subject['status']; ?></td>
                        <td><?php echo $subject['percentage']; ?></td>     
                        <?php if ($i === 0): ?>
                            <td rowspan="8"><?php echo $subjects['pass_count']; ?></td>
                            <td rowspan="8"><?php echo $subjects['gross_percentage']; ?></td>
                        <?php endif; ?>
                    </tr>
                <?php endif; ?>
                <?php $i++; ?>
            <?php endforeach; ?>
        <?php endforeach; ?>
    </tbody>
</table>
Saturday, May 29, 2021
 
cegfault
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :