2

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.

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but it's far from perfect, there are no safeguards in case there's only one child in the new array etc
@Kostis It's okay.For now this is what I want.Thanks again ! =)
1

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..

3 Comments

thanks @MoeTsao it is almost for what I want ,but how to make column for name have one row for each name
It's doable, but code would get ugly fast. Are you sure you need to have it in separate rows? Use css to present a divider between lines?
yes ,because I want to add some delete action inside each row name.I don't care how the code would look like,I will customize it later =)
0

PHP logic to create a new array with all group members in a single group as a single element of the array

<?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);
?>

HTML table structure (You need 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>

JSfiddle DEMO O/P

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.