PHP Matrix How to Fill

Asked

Viewed 695 times

5

Guys want to fill this matrix. It works but when I try to put more than one result in the same line only the last one appears. How to fix?

    <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>
  • Solved... for ($i=0; $i<$count ;$i++) { &#xA; $mapa[$planeta[$i]['coordx']][$planeta[$i]['coordy']]['x']=$planeta[$i]['coordx'];&#xA; $mapa[$planeta[$i]['coordx']][$planeta[$i]['coordy']]['y']=$planeta[$i]['coordy'];&#xA;}

  • 1

    Because you do not add this comment as an answer and mark it as solved, it could help others in the future, besides not leaving the question open and without "answer" accepted. =)

2 answers

2


I decided to change 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‌​']; 
 } 
  • Philip, you can accept your own answer as correct. So the question will be "closed". Hug!

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.