Jump to content

Create multidimensional array from sql query and display in html table


Recommended Posts

Good day

I am fairly new to php and have a problem.

I have two tables in mysql, that is dividendparams and dividendhistorydetails. dividendparams has fields paramid,paramname while dividendhistorydetails has fields detailsid,paramid,year,dividenditem.

 

I would like to query data from both tables and display as follows;

              Year1                 Year2                 Year3                 Year4

Paramname1 Dividenditem1         Dividenditem2         Dividenditem3         Dividenditem4

Paramname2 Dividenditem5         Dividenditem6         Dividenditem7         Dividenditem8

Paramname3 Dividenditem9         Dividenditem10 Dividenditem11 Dividenditem12

Paramname4 Dividenditem13 Dividenditem14 Dividenditem15 Dividenditem16

Paramname5 Dividenditem17 Dividenditem18 Dividenditem19 Dividenditem20

 

the number of paramname and year can increase, thereby increasing the dividenditem too.

 

Any help on this will be appreciated.

Thank you

I don't think you need a multi dimensional array for that a simple 2d array would be sufficient.

 

So far your table structure seems like so:

 

dividendparams

- paramid

- paramname

 

dividendhistorydetails

- detailsid

- paramid (fk?)

- year

- dividenditem

 

Here the field paramid in table dividendhistorydetails looks like a foreign key. Lookup how to use JOINS in sql.

 

 

NOt tested, so there may be some syntax issues:

<?php

//Query the data
$query = "SELECT p.paramname, h.year, h.dividenditem
          FROM `dividendparams` p
          JOIN `dividendhistorydetails` as h ON p.paramid = h.paramid
          ORDER BY p.paramname, h.year"
$result = mysql_query($query);

//Create arrays for the values
$params = array();
$years = array();
//Proces the results
while($row = mysql_fetch_assoc($result))
{
    $params[$row['paramname']][$row['year']] = $row['dividenditem'];
    if(!in_array($row['year'], $years) { $years[] = $row['year']; }
}
sort($years);

//Create header row
$output  = "<tr>\n";
$output .= "<th>Parameter</th>\n";
foreach($years as $year)
{
    $output .= "<th>{$year}</th>\n";
}
$output .= "</tr>\n";

//Create the data rows
foreach($params as $paramName => $data)
{
    $output .= "<td>{$paramName}</td>\n";
    foreach($years as $year)
    {
        $dividendItem = $data[$year];
        $output .= "<td>{$dividendItem}</td>\n";
    }
}

?>
<html>
<body>

<table border="1">
<?php echo $output; ?>
</table>

</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.