PHP Matrix how to fill

5

I want to fill this matrix. It works, but when I try to put more than one result on a single line, only the last one appears. How to correct?

    <h1>Mapa</h1>
<?php
$linha=0;
$coluna=0;

//preencher o mapa
//debug($planeta);
$mapa = array();

$linhas = $planeta->count();
$planeta = $planeta->toArray();

debug($planeta);

?>
<table>
<?php
    for ($coluna=0;$coluna<=10;$coluna++){
        echo "<tr>";
        //echo $mapa[$coluna]['x']." | ".$mapa[$coluna]['y']."<br>" ;
        $x = (!empty($planeta[$coluna]['coordx'])) ? $x = $planeta[$coluna]['coordx'] : $x = NULL;
        $y = (!empty($planeta[$coluna]['coordy'])) ? $y = $planeta[$coluna]['coordx'] : $y = NULL;  

        for ($linha=0;$linha<=10;$linha++){

            //$y = (!empty($planeta[$linha]['coordy'])) ? $y = $planeta[$linha]['coordy'] : $y = NULL;  
            echo $x." - ".$y." - Coluna ".$coluna."<br>";

            if ($linha === $y && $coluna === $x){
                echo    "<td><a href='planetas/planeta?x=".$coluna."&y=".$linha."'>
                        <img src='/gm/img/game/planeta.png' height='30px' width='30px'></a>
                        </td>";
            } else {
                echo "<td><a href='planetas/planeta?x=".$coluna."&y=".$linha."'>".$coluna."|".$linha."</a></td>";   
            }

        }
        echo "</tr>";
        //echo $x." | ".$y."<br>";
    }
    //echo count($mapa);*/
?>
</table>
    
asked by anonymous 10.08.2015 / 02:17

2 answers

2

I resolved by changing the code as follows:

 for ($i=0; $i<$count ;$i++) { 
     $mapa[$planeta[$i]['coordx']][$planeta[$i]['coordy']]['x']=$planeta[$i]['coordx‌​']; 
     $mapa[$planeta[$i]['coordx']][$planeta[$i]['coordy']]['y']=$planeta[$i]['coordy‌​']; 
 } 
    
11.08.2015 / 04:02
0

You can try the following code:

<h1>Mapa</h1>
<table>
<?php

$linhas = $planeta->count();
$planeta = $planeta->toArray();

for($linha = 0; $linha <= 10; $linha++){ //Como você vai iniciar o <tr> aqui, faz muito mais sentido chamar de linha

    echo "<tr>";

    for ($coluna = 0; $coluna <= 10; $coluna++){

        $x = $planeta[$coluna][$linha]['coordx'];
        $y = $planeta[$coluna][$linha]['coordy'];  

        if ($linha == $y && $coluna == $x){
?>
            <td>
                <a href='planetas/planeta?x=".$coluna."&y=".$linha."'>
                    <img src='/gm/img/game/planeta.png' height='30px' width='30px'>
                </a>
            </td>
<?php
        } else {
?>
            <td>
                <a href='planetas/planeta?x=".$coluna."&y=".$linha."'>".$coluna."|".$linha."</a>
            </td>
<?php 
        }

    }

    echo "</tr>";

}

?>
</table>
    
10.08.2015 / 03:18