1

I have two array, one is meant for table heading and second is meant for table data.

Table Heading array

  Array
  (
   [0] => techrepublic.com
   [1] => office.microsoft.com
   [2] => en.pstrecovery.net
   [3] => easeus.com
   [4] => download.cnet.com
   [5] => datanumen.com
   [6] => 2324
 )

Table Data array

  Array
(
   [software services] => Array
    (
        [datanumen.com] => 2
        [office.microsoft.com] => 3
        [easeus.com] => 8
        [download.cnet.com] => 9
        [en.pstrecovery.net] => 10
    )

[software services in Delhi] => Array
    (
        [en.pstrecovery.net] => 1
        [datanumen.com] => 3
        [office.microsoft.com] => 4
        [easeus.com] => 9
        [download.cnet.com] => 10
    )

[orignal software services] => Array
    (
        [en.pstrecovery.net] => 1
        [easeus.com] => 6
        [download.cnet.com] => 7
        [datanumen.com] => 8
        [office.microsoft.com] => 9
    )

)

I want output table as shown in attachment enter image description here

What i have done till now

    // code for table heading
    foreach ( $_POST['competitor']) as $value) { // $_POST['competitor']) 
                                                // containts table heading 
                                                // array
          echo "<th>". $value ."</th>"  ;
    }

  //code for table row 

     foreach ($details as $Keywords => $comp) {  // $details contains row
                                    // data array
       echo '<tr><td>' .$Keywords .  '</td>';
       foreach ($_POST['competitor'] as $competitor) {
        if (isset($comp[$competitor])) {
             echo '<td>' . $comp[$competitor] . '</td>' ;
        } else {
        echo '<td>Not Found</td>' ;
       }
      }
       echo '</tr>' ;
      }

I am unable to arrange td and tr. How can I achieve the output as shown in attachment?

Currently i am getting following output which is wrong :enter image description here

6
  • What do you mean by "I am unable to arrange td and tr"? What does your output HTML look like presently? Commented Aug 21, 2017 at 19:56
  • my means how to arrange / or make loop to arrange table rows and column as shown in attachment Commented Aug 21, 2017 at 20:06
  • That doesn't fully answer my question. What are you getting presently? I think this question is rather leaning towards "too broad" or "no MCVE" at present. Commented Aug 21, 2017 at 20:07
  • Dear i have attached a output format , i have to write to write code in such a manner so that i can get desired output Commented Aug 21, 2017 at 20:13
  • Yes, I understand. Asking for a third time: what are you getting at the moment? Commented Aug 21, 2017 at 20:17

1 Answer 1

4

The thing is, in order to get the data in the same order as the headers, you need to loop through the headers again for each row in the data. Since your data uses the headers as array keys, this should be pretty easy.

foreach ( $_POST['competitor'] as $value) {
    echo "<th>". htmlspecialchars($value) ."</th>"  ;
}

foreach ($details as $Keywords => $comp) {
    echo '<tr><td>' . htmlspecialchars($Keywords) .  '</td>';


    // iterate the headers here instead
    foreach ($_POST['competitor'] as $competitor) {

        // echo the value for that header from the current row if it's present
        if (isset($comp[$competitor])) {
            echo '<td>' . htmlspecialchars($comp[$competitor]) . '</td>' ;
        } else {
            echo '<td>Not Found</td>' ;
        }
    }
    echo '</tr>' ;
}

Side note - don't forget to escape your strings properly for HTML output.

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

8 Comments

You might want to make a note about HTML escaping as well, even if you don't actually add it to your example. Characters like & will cause the HTML to be invalid, and an unclosed < will stop the page from rendering at all.
@halfer good point. I was more focused on the logic problem, but that part is certainly important.
@wasimbeg I have. I'm trying to figure out how it could have produced that output.
@wasimbeg ah, so you got it figured out? I was at a loss, honestly.
No , i am waiting for your solution
|

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.