Noman uddin Noman uddin - 1 year ago 77
MySQL Question

In PHP how to parse database data into various tables

I am new to php. I have following type of database table data which I want to display in PHP. Table data is available in a variable "Outputdata"

enter image description here

Now I want to display this data in php in the following way.

enter image description here

Product ID should not be repeated and "Child Columns" should be nested on the basis of "category".

Following is my code:
Model.php Code:

public function getproddata()
$Outputdata=$this->query("CALL Product();");
return $Outputdata;

Controller COde:

public function Procdata() {

PHP Code:

<div class='data1'>
<table class='data11 defaultTable'>
<th colspan='2'>Data <?php echo $i ?></th>
<td><?php echo "Product ID" ?></td>
<td><?php echo $current['Product ID'] ?></td>
<td><?php echo "Percentage" ?></td>
<td><?php echo $current['Percentage'] ?></td>
<div class='diagnose_toggle' onClick='toggleDiagnose($i)' id='detailsButton$i'></div>
<table id='details$i' class='defaultTable'>

<th><?php echo "Category ID" ?></th>
<th><?php echo "Child Category ID"?></th>
<th><?php echo "Child Category Name"?></th>

<td><?php echo $current['Category ID']?></td>
<td><?php echo $current['Child Category ID']?></td>
<td><?php echo $current['Child Category Name']?></td>


<?php $i++; ?>

Database Query:

Select Product_ID, Percentage, CategoryID, Child_Categ_ID, Child_categ_name
from products order by Product_ID, CategoryID

Problem is all product ids are displayed in separate rows and no nesting is also there.

Any idea to add the nesting and grouping logic?
Thank you

Answer Source

``assuming for your 1st table your query is a select all query you would get all values in an array then user foreach loop var_dump will help you view your query that you captured

foreach  ($variable as $key as $value){
<td><?php echo $value->column_name1 ?><td>
<td><?php echo $value->column_name2 ?><td>
<td><?php echo $value->column_name3 ?><td>
<td><?php echo $value->column_name4 ?><td>

if fetching from 2 tables if (primary key == foreign ) use && inside on how precise you want to filter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download