Display Query in HTML mysql

arkam chou
arkam chou used Ask the Experts™
on
Dear Expert,

can I query and display like attachment?
query.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
Yes.  It's easily done with nested iterators as you retrieve the rows from the data set.  This shows the general design pattern.
http://iconoun.com/demo/temp_arkam.php
<?php // demo/temp_arkam.php
/**
 * SEE: http://www.experts-exchange.com/questions/28689202/Display-Query-in-HTML-mysql.html
 */
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT EE
$rows[] = ['country' => 'US',    'state' => 'LA', 'people' => 'Car'];
$rows[] = ['country' => 'US',    'state' => 'LA', 'people' => 'Motor'];
$rows[] = ['country' => 'US',    'state' => 'LA', 'people' => 'Fan'];
$rows[] = ['country' => 'US',    'state' => 'NY', 'people' => 'Apple'];
$rows[] = ['country' => 'US',    'state' => 'NY', 'people' => 'Speaker'];
$rows[] = ['country' => 'US',    'state' => 'NY', 'people' => 'Shoe'];
$rows[] = ['country' => 'US',    'state' => 'NY', 'people' => 'Airplane'];
$rows[] = ['country' => 'Japan', 'state' => 'AB', 'people' => 'house'];
$rows[] = ['country' => 'Japan', 'state' => 'AB', 'people' => 'hotel'];
$rows[] = ['country' => 'Japan', 'state' => 'AB', 'people' => 'Car'];
$rows[] = ['country' => 'Japan', 'state' => 'AC', 'people' => 'Earphone'];
$rows[] = ['country' => 'Japan', 'state' => 'AC', 'people' => 'Pen'];
$rows[] = ['country' => 'Japan', 'state' => 'AC', 'people' => 'Book'];
$rows[] = ['country' => 'Japan', 'state' => 'AC', 'people' => 'School'];

// DOES THE TEST DATA LOOK OK?  YES.
// var_dump($rows);


// CHANGE INDICATORS
$old_country = FALSE;
$old_state   = FALSE;

// PROCESS ROW BY ROW
foreach ($rows as $row)
{
    // CHANGE OF COUNTRY
    if ($old_country != $row['country'])
    {
        echo "<br>Country: " . $row['country'];
        $old_country = $row['country'];
    }

    // CHANGE OF STATE
    if ($old_state != $row['state'])
    {
        echo "<br>\tState: " . $row['state'];
        $old_state = $row['state'];
    }

    // CHANGE OF PEOPLE ASSUMED ON EVERY ROW
    echo "<br>\t\tPeople: " . $row['people'];
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial