Reputation: 313
I have the following array that I get from mysql database, after I get all data I should create one table to show all values.
Array
(
[0] => Array
(
[groupNo] => 1001
[name] => james
)
[1] => Array
(
[groupNo] => 1002
[name] => chen
)
[2] => Array
(
[groupNo] => 1002
[name] => ash
)
[3] => Array
(
[groupNo] => 1001
[name] => mark
)
)
My current table is like this one :
Group Number | Name | Action |
-------------+------+---------------------
1001 | James | |
------------+----------------+------------
1002 | chen | |
-------------+---------------+------------
1002 | ash | |
-------------+---------------+------------
1001 | mark | |
-------------+---------------+------------
But what I want is my table look exactly like below :
Group Number | Name | Action |
-------------+------+---------------------
1001 | James | |
+---------------+------------
| Mark | |
-------------+----------------------------
1002 | chen | |
+----------------------------
| ash | |
-------------+----------------------------
Below is my code :
<?php
if (count($sharingGroup) > 0) {
for ($i = 0; $i < count($sharingGroup); $i++) {
$sharingGroupRecord = $sharingGroup[$i];
?>
<tr>
<td>' . $sharingGroupRecord ['groupNo'] .'</td>
<td>' . $sharingGroupRecord ['name'] .'</td>
<td><a name="action" href="#">Action<span>
</span></a>
</tr>
Anybody please help me to solve this.
Upvotes: 2
Views: 4219
Reputation: 6344
Suppose the following is your input array
$array = array(
array(
"groupNo" => 1001,
"name" => "james"
),
array(
"groupNo" => 1002,
"name" => "chen"
),
array(
"groupNo" => 1002,
"name" => "ash",
),
array
(
"groupNo" => 1001,
"name" => "mark"
),
array
(
"groupNo" => 1003,
"name" => "marco"
),
);
Then you could do the following:
$newArray =array();
foreach($array as $arr){
$newArray[$arr['groupNo']][] = $arr["name"];
}
unset($array); // unset the unwanted array.
and for printing the table
<table border="1">
<?php if(!empty($newArray)){
foreach($newArray as $key=>$val){
echo "<tr>";
echo "<td rowspan='".sizeof($val)."'>".$key."</td>";
echo "<td>".$val[0]."</td>";
echo "</tr>";
for($i=1;$i<sizeof($val);$i++){
echo "<tr><td>".$val[$i]."</td></tr>";
}
}
} ?>
</table>
Upvotes: 0
Reputation: 7019
<?php
$group=Array(0=>array('groupNo' => 1001, 'name' => "james"), 1=>array('groupNo' => 1002, 'name' => "chen"), 2=>array('groupNo' => 1002, 'name' => "ash"), 3=>array('groupNo' => 1001, 'name' => "mark"));
$new_group=array();
for($i=0; $i < count($group); $i++)
{
$new_group[$i]=array($group[$i]['groupNo']);
for($j=0; $j < count($group); $j++)
{
if($new_group[$i][0]==$group[$j]['groupNo'])
{
$new_group[$i][]=$group[$j]['name'];
array_splice($group, $j, 1);
$j--;
}
}
}
print_r($new_group);
?>
rowspan
property)<table id=t1 border="2">
<?php
if(count($new_group)>0)
echo "<tr>
<th>Group Number
<th>Name
<th>Action
</tr>";
foreach($new_group as $grouping)
{
$count=count($grouping)-1;
echo "<tr>
<td rowspan=".$count.">".$grouping[0];
for($j=1; $j <= $count; $j++)
{
echo "<td>".$grouping[$j];
echo "</tr>";
if($j<$count)
echo "<tr>";
}
}
?>
</table>
Upvotes: 0
Reputation: 1105
First you need to recreate the array
$newArray = array();
foreach($sharingGroup as $item) {
$newArray[$item['groupNo']][] = $item['name'];
}
it'll produce this:
Array
(
[1001] => Array
(
[0] => james
[1] => mark
)
[1002] => Array
(
[0] => chen
[1] => ash
)
)
then you loop through the newArray
if (count($newArray) > 0) {
$html = '';
foreach($newArray as $key => $val) {
$html .= "<tr>\r\n";
$html .= "<td rowspan='".count($val)."'>{$key}</td>\r\n";
foreach($val as $key => $td) {
if($key>0) {
$html.= "<tr>";
}
$html .= "<td>{$td}</td>\r\n";
$html .= "<td>Action</td>\r\n";
$html .= "</tr>\r\n";
}
}
}
and you'll get this as a result
Here's the code all in one place
Upvotes: 1
Reputation: 1054
Try this:
<?php
$transpose=array();
if (count($sharingGroup) > 0) {
for ($i = 0; $i < count($sharingGroup); $i++) {
$sharingGroupRecord = $sharingGroup[$i];
$transpose[$sharingGroupRecord['groupNo']][]=$sharingGroupRecord['name'];
}
print_r($transpose);
$output="<table>";
foreach ($transpose as $groupNo => $group){
$output.="<tr><td>$groupNo</td><td>";
foreach ($group as $name){
$output.="$name<br />";
}
$output.="</td></tr>";
}
$output.="</table>";
echo $output;
With more rows..
<?php
$transpose=array();
if (count($sharingGroup) > 0) {
for ($i = 0; $i < count($sharingGroup); $i++) {
$sharingGroupRecord = $sharingGroup[$i];
$transpose[$sharingGroupRecord['groupNo']][]=$sharingGroupRecord['name'];
}
print_r($transpose);
$transpose_again=array();
foreach ($tranpose as $groupNo=>$group){
foreach($group => $name){
$transpose_again[][$groupNo][]=$name;
}
}
// Now you have a row numbers that is sorted by the groupNo.. I will stop here and let you figure out how to present it and do the rowspan if you really want to..
Upvotes: 1