PHP Table Data Show Results Vertically not Horizontally

Sarah Malka

I'm really frustrated.. I really need to show this table right. How can I set this code to show table vertically like this:

1 - 4 - 7

2 - 5 - 8

3 - 6 - 9

And not like this:

1 - 2 - 3 - 4

5 - 6 - 7 - 8

9 - ... ect

<?php

$PNG_WEB_DIR = plugin_dir_path( __FILE__ ).'temp/';

wp_mkdir_p($PNG_WEB_DIR);

$cols = $this->settings['cols'];
$rows = $this->settings['rows'];
$label_number = 0;

// die($_GET['order_ids']);
$order_ids = explode(',',$_GET['ids']);
$order_count = count($order_ids);
$labels_per_page = $cols*$rows;
$page_count = ceil(($order_count)/$labels_per_page);

for ($page=0; $page < $page_count; $page++) {
    echo '<table class="address-labels" width="100%" height="100%" border="0" cellpadding="0">';
    $last_height = 0;
    $current_height = $current_width = 0;
    $current_row = 0;

    for ($label=0; $label < $labels_per_page; $label++) { 
        $label_number++;
        $current_col = (($label_number-1) % $cols)+1;

        if ($current_col == 1) {
            $last_height = $current_height;
            $last_width = 0;
            $current_row++;
            echo '<tr class="label-row">';
        }

        if ( $label_number > $this->offset && isset($order_ids[$label_number - $this->offset - 1]) ) {
            $order_id = $order_ids[$label_number - $this->offset - 1];
        } else {
            $order_id = '';         
        }

        $current_width = round( $current_col * (100/$cols) );
        $width = $current_width - $last_width;
        $last_width = $current_width;

        $current_height = round( $current_row * (100/$rows) );
        $height = $current_height - $last_height;

        printf('<td width="%s%%" height="%s%%" class="label"><div class="label-wrapper">', $width, $height);
        // because we are also looping through the empty labels,
        // we need to check if there's an order for this label
        if (!empty($order_id)) {
            // get label data from order
            $order = new WC_Order( $order_id );

            // replace placeholders
            $label_data = isset($this->settings['address_data'])? nl2br( $this->settings['address_data'] ): '[shipping_address]';
            $label_data = $this->make_replacements( $label_data, $order );

            echo '<div class="address-block">';
            echo '<div class="addrress_show">';

            // process label template to display content
            echo $label_data; 
            echo '</div>';
            echo '<div class="clearb"></div>';
            echo '</div>';
        } else {
            echo '&nbsp;';
        }
        echo '</div></td>';

        if ($current_col == $cols) {
            echo '</tr>';
        }

    }
    echo '</table>';
}
        // shpt_after_page hook
                do_action( 'shpt_after_page' );
        ?>
Mi-Creativity

Since I can't know your raw data structure which you use to loop through it and produce the table, I presumed it as one-dimensional array and you want to print the table as 3 columns as you said, to see in action here's a PHP Fiddle - hit run to execute it - this will work for any array length as long as columns is 3, the rows is dynamic

PHP: Updated for naming purpose

<?php
    $table = '<table class="address-labels" width="30%" border="0" cellpadding="0">';
    $dataArr = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17);
    $colNum = 3;
    $rowNum = ceil(count($dataArr)/$colNum);
    for($i=0; $i < $rowNum; $i++){
        $x1 = isset( $dataArr[$i] ) ? $dataArr[$i] : '-';
        $x2 = isset( $dataArr[$i + $rowNum] ) ? $dataArr[$i + $rowNum] : '-';
        $x3 = isset( $dataArr[$i + 2 * $rowNum] ) ? $dataArr[$i + 2 * $rowNum] : '-';
        $table .= '<tr><td>' . $x1 . '</td><td>' . $x2 . '</td><td>' . $x3 . '</td></tr>';
    }
    $table .= '</table>';
    echo $table;

?>

EDIT: Now if you want to change the column number, like set it to 5 for example, you need to do the following:

1st - set $colNum = 5; 2nd - add more variables to hold the values like, *considering it is 5 columns:

 $x4 = isset( $dataArr[$i + 3 * $rowNum] ) ? $dataArr[$i + 3 * $rowNum] : '-';
 $x5 = isset( $dataArr[$i + 4 * $rowNum] ) ? $dataArr[$i + 4 * $rowNum] : '-';

and don't forget to increase the multiplier * $rowNum

then for each extra column you add you need to add <td></td> like:

'<td>' . $x4 . '</td><td>' . '</td><td>' . $x5 . '</td><td>' . '</td>'

Here's another PHP Fiddle showing the above for 5 columns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Listing data horizontally(in a table) instead of vertically

Html Table : td data to be center. vertically and horizontally

Horizontally and Vertically scrollable table view with data from json

Read scraped data vertically out of table instead horizontally Python

Center align a table vertically and horizontally

SQL Server : join table to itself to show data horizontally

Resize issue with table centered horizontally and vertically

How to position a table at the center of div horizontally & vertically

PHP explode data and show in table

How to split data vertically instead of horizontally?

How to consolidate vertically data that is split horizontally in Excel?

Fill a table vertically in php

Trying to show list items vertically but two items horizontally

Display results horizontally without creating summary table

Get data horizontally into html table

Flutter : Horizontally and Vertically Scrolling Time Table with two sided header

How to align horizontally (and vertically) the boxes using display table-cell?

CSS display table vertically and expand horizontally when overflow

Vue JS 2 table title Scroll horizontally with content but not vertically

How to align text inside table thead to center horizontally and vertically

How do I center table text both horizontally and vertically?

iText java - Split table vertically and horizontally and add header row

How to center this select box horizontally and vertically in a table (Bootstrap 4.0)?

PHP: Show data from mysql database in table

Show data with hyphen (-), if row not found in a table - php

cURL get the API data to show in php table

PHP Ajax - Show part of the data in a table?

Show values in a nested loop horizontally in a table

how to change values of 2 dimensional array vertically and horizontally in PHP

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive