PHP Table echo empty td

Waleed

Why would it empty an empty td and work properly. If there are is no data, it pushes the next row data up the column. Please suggest me how to fix it.

for($i = 0; $i < count($cheads[$keys[0]]); $i++){
    echo "<tr>";
    for($k = 0; $k < count($keys); $k++){
        echo "<td>";
        if(array_key_exists($i, $cheads[$keys[$k]])){
            echo $cheads[$keys[$k]][$i]['Data'];
        }else{
            echo "N/A";
        }
        echo "</td>";
    }
    echo "</tr>";
}

enter image description here

Data set: enter image description here

Array ( [Last Name] => Array ( [0] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) [1] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) [2] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) [3] => Array ( [Field] => Last Name [Data] => Yo [User] => 5 ) [4] => Array ( [Field] => Last Name [Data] => Test [User] => 5 ) ) [Phone Name] => Array ( [0] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) [1] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) [2] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) [3] => Array ( [Field] => Phone Name [Data] => Yo [User] => 5 ) [4] => Array ( [Field] => Phone Name [Data] => [User] => 5 ) ) [Address Name] => Array ( [0] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 ) [1] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 ) [2] => Array ( [Field] => Address Name [Data] => Yo [User] => 5 ) [3] => Array ( [Field] => Address Name [Data] => [User] => 5 ) ) [Description] => Array ( [0] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) [1] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) [2] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) [3] => Array ( [Field] => Description [Data] => Yo [User] => 5 ) [4] => Array ( [Field] => Description [Data] => [User] => 5 ) ) [Fruit Type] => Array ( [0] => Array ( [Field] => Fruit Type [Data] => Peanuts [User] => 5 ) [1] => Array ( [Field] => Fruit Type [Data] => Apple [User] => 5 ) ) )
Waleed

The trick was to change how I was getting data in my array. I used associated arrays with a different approach.

Before:

cheads:    Array ( 
[Last Name] => Array ( 
    [0] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) 
    [1] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) 
    [2] => Array ( [Field] => Last Name [Data] => 1 [User] => 5 ) 
    [3] => Array ( [Field] => Last Name [Data] => Yo [User] => 5 ) 
    [4] => Array ( [Field] => Last Name [Data] => Test [User] => 5 ) ) 

[Phone Name] => Array ( 
    [0] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) 
    [1] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) 
    [2] => Array ( [Field] => Phone Name [Data] => 2 [User] => 5 ) 
    [3] => Array ( [Field] => Phone Name [Data] => Yo [User] => 5 ) 
    [4] => Array ( [Field] => Phone Name [Data] => [User] => 5 ) ) 

[Address Name] => Array ( 
    [0] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 ) 
    [1] => Array ( [Field] => Address Name [Data] => 3 [User] => 5 ) 
    [2] => Array ( [Field] => Address Name [Data] => Yo [User] => 5 ) 
    [3] => Array ( [Field] => Address Name [Data] => [User] => 5 ) ) 
[Description] => Array ( 
    [0] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) 
    [1] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) 
    [2] => Array ( [Field] => Description [Data] => 4 [User] => 5 ) 
    [3] => Array ( [Field] => Description [Data] => Yo [User] => 5 ) 
    [4] => Array ( [Field] => Description [Data] => [User] => 5 ) ) 
[Fruit Type] => Array ( 
    [0] => Array ( [Field] => Fruit Type [Data] => Peanuts [User] => 5 ) 
    [1] => Array ( [Field] => Fruit Type [Data] => Apple [User] => 5 ) ) )

After:

Matrix:   Array ( 
[D2AA0FA4-4F91-4757-9D27-9F65E0390EF1] => Array ( [Last Name] => 1 [Phone Name] => 1 [Address Name] => 1 [Description] => 1 ) 
[182EDD25-89CE-40F0-87AE-53D218CD0A1F] => Array ( [Last Name] => 2 [Phone Name] => 2 [Address Name] => 2 [Description] => 2 ) 
[59EF4DA3-0D3B-4F37-A415-10DD414351A7] => Array ( [Last Name] => 3 [Phone Name] => 3 [Address Name] => 3 [Description] => 3 [Fruit Type] => Apple ) 
[1C7B67E2-9230-4215-B990-C366077DA6C3] => Array ( [Last Name] => 5 [Phone Name] => 5 [Address Name] => 5 [Description] => 5 ) )

I had an extra Identifier field for identifying each data by user. I made them parent keys and for each identifier I added all fields data under them making field-type key as well for each child data.

Before this I had collect all the keys in another array, I iterated through it and simply echoed like this:

foreach ($matrix as $key => $value) {

    echo "<tr>";

    for($i = 0; $i < count($cheads); $i++){

        $head = $cheads[$i];

        if(isset($value[$head])){
            echo "<td>";
            echo $value[$head];
            echo "</td>";
        }else{
            echo "<td>";
            echo "NA";
            echo "</td>";
        }

    }
    echo "<td><a href='?action=edit&guid=$key'>Edit</a></td>";
    echo "<tr>";

}

Result:

enter image description here

Using associative key and a different approach did it for me.

Thanks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP Table echo empty td

From Dev

Echo Related Table Row If Not Empty - PHP

From Dev

php echo if query is empty

From Dev

Check if table tr td is not empty

From Dev

PHP pdo echo results into table

From Dev

PHP Ajax echo HTML table

From Dev

how to style php echo table

From Dev

Count occurrence in table PHP echo

From Dev

PHP Echo Table Column Alignment

From Dev

Echo a table in PHP array looping

From Dev

how to handle conditional statements in echo"<table><tr><td></td></tr></table>";?

From Dev

PHP wordpress if a text value is empty, echo

From Dev

Php echo not working on empty form field test

From Dev

Change colour of td in dynamic table generated in php

From Dev

Echo json data in php for data table use

From Dev

How to echo data into 3 columns in a table in php?

From Dev

How to display PHP echo in a HTML table?

From Dev

how to echo a hyperlink in a html table via php

From Dev

Echo all table rows in PHP after login

From Dev

PHP echo results in new table, not new row

From Dev

Echo json data in php for data table use

From Dev

php mysql echo value with highest counter in table

From Dev

html - php echo row repeatedly (table reverse)

From Dev

If mysql input is empty echo default image else get picture in a table

From Dev

Php query is not returning anything not even echo from if statement when it is empty

From Dev

How to echo Empty Entry Prohibited Message generated from a query in php?

From Dev

How to echo <tr> using php and ajax to appear inside <table> </table>

From Dev

PHP echo within an echo

From Dev

Count empty or not empty TD with jquery