use of rowspan in dynamic data

i want to use rowspan in array data used to show by foreach loop.how can i use rowspan.
here it is my foreach code
<?php
      foreach($DataArray as $row) {
     echo '<tr>' ;
     foreach($row as $cell) {
     echo '<td>' . $cell . '</td>';
    }
    echo('</tr>');
   }
?>

Open in new window

and my array is
$DataArray=array(
array('state'=>'Uttrakhand','state'=>'Uttrakhand','district_name' => 'ALMORA','district_code'=>8,'block_name'=>'BHIKIYASAIN','block_id'=>47,'school-sample' =>5,'school-acheived'=>1,'AWS-reached'=>5,'AWS-acheived'=>2,'school-sample' =>5,'school-acheived'=>1,'AWS-reached'=>5,'AWS-acheived'=>2),
array('state'=>'Uttrakhand','district_name'=>'ALMORA','district_code'=>8,'block_name'=>'CHAUKHUTIYA','block_id'=>48,'school-sample' =>5,'school-acheived'=>1,'AWS-reached'=>5,'AWS-acheived'=>2),
array('state'=>'Uttrakhand','district_name'=>'ALMORA','district_code'=>8,'block_name'=>'DHAULADEVI','block_id'=>49,'school-sample' =>5,'school-acheived'=>1,'AWS-reached'=>5,'AWS-acheived'=>2),
array('state'=>'Uttrakhand','district_name'=>'ALMORA','district_code'=>8,'block_name'=>'DWARAHAT','block_id'=>50,'school-sample' =>5,'school-acheived'=>1,'AWS-reached'=>5,'AWS-acheived'=>2),
array('state'=>'Uttrakhand','district_name'=>'ALMORA','district_code'=>8,'block_name'=>'HAWALBAG','block_id'=>51,'school-sample' =>5,'school-acheived'=>1,'AWS-reached'=>5,'AWS-acheived'=>2),
array('state'=>'Uttrakhand','district_name'=>'ALMORA','district_code'=>8,'block_name'=>'LAMGARAH','block_id'=>52,'school-sample' =>5,'school-acheived'=>1,'AWS-reached'=>5,'AWS-acheived'=>2),
);

Open in new window

abhishek sharmaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Olaf DoschkeSoftware DeveloperCommented:
The rowspan attribute has to be applied to the td tag first having the value you want to span across multiple rows.
So what do you want to span? All repeating values?

There is no general rule about when and why you rowspan, you could even span a column across multiple rows, if all the rows contribute differing values, but you want to list them out in a single cell.

So tell us more about your idea of what to span and how to detect it.

Bye, Olaf.
0
abhishek sharmaAuthor Commented:
sir i want   only single district_name value with rowspan
0
Olaf DoschkeSoftware DeveloperCommented:
OK, you have an array of rows, so the number for rowspan should be count($DataArray). The district_name is in the second column, but let's not rely on that, your array elements are associative (each item has its column name), we can make use of that as key/value pair in the following way:

<?php
   $firstrow = true;
   $span = count($DataArray);
   foreach($DataArray as $row) 
   {
     echo '<tr>';
     foreach($row as $header=>$cell) 
     {
       if ($firstrow)
       {
         if ($header==='district_name' or $header==='district_code')  
         {
           echo '<td rowspan=' . $span . '>' . $cell . '</td>';
         } else { 
           echo '<td>' . $cell . '</td>';
         }
       }
       else
       {
         if ($header!=='district_name' and $header!=='district_code') 
         {
           echo '<td>' . $cell . '</td>';
         }
       }
     }
     echo('</tr>');
     $firstrow = false;       
   }
?>

Open in new window

The state also is the same in all rows, but you will be able to add that. The problematic part about how rowspan works in HTML is, you need to specify the count of rows you want to span at the first row of the spanning. In general, you will not know in advance how many rows a value repeats. So this might need to be turned into a two-pass code to determine $span or even an array of spans in the first pass of scanning rows and then apply that spans in the second pass through all rows.

Bye, Olaf.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abhishek sharmaAuthor Commented:
Thankyou very much.
it`s working for me
0
Olaf DoschkeSoftware DeveloperCommented:
The question author in principle already accepted the answer himself by commenting it works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.