• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 81
  • Last Modified:

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

0
abhishek sharma
Asked:
abhishek sharma
  • 3
  • 2
  • 2
1 Solution
 
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
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
 
Anjum ShahzadCommented:
This is something I was looking for from a long time. I was searching for this sort of data and delighted in perusing this one.
A debt of gratitude is in order for sharing,Thank you for the ideas.
Sibelius Crack
0
 
Anjum ShahzadCommented:
Thank You so Much Admin, Extraordinary article and unique article.
Love yours website.
movavi video editor crack
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now