Viewed   65 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 , 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

2

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.

Tuesday, September 20, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
2

Update (on 2018-03-27 - Restricted to variable products only, avoiding an error)

Here is the correct way to achieve hooking it in woocommerce_after_single_product action hook:

add_action( 'woocommerce_after_single_product', 'custom_table_after_single_product' );
function custom_table_after_single_product(){
    global $product;

   // Only for variable products
   if( ! $product->is_type('variable')) return; 

    $available_variations = $product->get_available_variations();

    if( count($available_variations) > 0 ){

        $output = '<table>
            <thead>
                <tr>
                    <th>'. __( 'Height', 'woocommerce' ) .'</th>
                    <th>'. __( 'Width', 'woocommerce' ) .'</th>
                    <th>'. __( 'Regular price', 'woocommerce' ) .'</th>
                    <th>'. __( 'Sale price', 'woocommerce' ) .'</th>
                </tr>
            </thead>
            <tbody>';

        foreach( $available_variations as $variation ){
            // Get an instance of the WC_Product_Variation object
            $product_variation = wc_get_product($variation['variation_id']);

            $sale_price = $product_variation->get_sale_price();
            if( empty( $sale_price ) ) $sale_price = __( '<em>(empty)</em>', 'woocommerce' );

            $output .= '
            <tr>
                <td>'. $product_variation->get_height() .'</td>
                <td>'. $product_variation->get_width() .'</td>
                <td>'. $product_variation->get_regular_price() .'</td>
                <td>'. $sale_price .'</td>
            </tr>';
        }
        $output .= '
            </tbody>
        </table>';

        echo $output;
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

All code is tested on Woocommerce 3+ and works. You can view additional hooks here…

Friday, October 7, 2022
3

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>
Monday, October 3, 2022
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :