Home > Back-end >  how to organise a table with php and html?
how to organise a table with php and html?

Time:11-17

I'm student so I'm beginner with code.

I made a search in a data base of MySQL, and the result was a table of 3 other tables:

array (size=3)
  0 => 
    array (size=4)
      'type_de_sport' => string 'boxe' (length=4)
      'Adresse' => string '33 rue de Paradis' (length=17)
      'ville' => string 'Marseille' (length=9)
      'code_postal' => string '13006' (length=5)
  1 => 
    array (size=4)
      'type_de_sport' => string 'boxe' (length=4)
      'Adresse' => string '44 boulevard Sakakini' (length=21)
      'ville' => string 'Marseille' (length=9)
      'code_postal' => string '13005' (length=5)
  2 => 
    array (size=4)
      'type_de_sport' => string 'BOXE' (length=4)
      'Adresse' => string 'place a moi' (length=11)
      'ville' => string 'Marseille' (length=9)
      'code_postal' => string '13000' (length=5)

I would liked to organise this results and I tried this, but it didn't work:

<table>
<thead>
    <tr>
      <th scope="col">Type de sport</th>
      <th scope="col">Adresse </th>
      <th scope="col">Ville</th>
      <th scope="col">code postal</th>
    </tr>
  </thead>
  <tbody>
  <? for ($i=0; $i >$num ; $i  ){ ?>
    <tr>
      <th scope="row"><?php $row=$i   1 ; ?></th>
      <?php foreach ($resultats[$i] as $contenu) { ?>
      <td><?= $contenu ; ?></td>
      <?php } ?>
    </tr>
    <?php } ?>

  </tbody>
</table>

please do you have anu other ideas?

Best regards

CodePudding user response:

You have an array of arrays. $results should contain the while array of arrays.

<tbody>

<?php 
   // $results is the main array
   foreach($results as $result){ 
   // so when we are looping here $result will contain one single array
   // this $result array is an associative array, so you have to access the values using the key
?>

    <tr>
       <td><?php echo($result['type_de_sport']); ?></td>
       <td><?php echo($result['Adresse']); ?></td>
       <td><?php echo($result['ville']); ?></td>
       <td><?php echo($result['code_postal']); ?></td>
    </tr>

<? } //end of foreach ?>
</tbody>

CodePudding user response:

There are a handful of small logic problems in the code:

  1. $num doesn't seem to be defined.

  2. > should be <, because since you're incrementing $i, it'll either already be greater than $num (if it exists) and you'll get an infinite loop (and eventually a crash), or it won't be to begin with and the loop will never start.

You should be looping while $i is less than the size of the results array.

  1. You never use $row, and it's not clear why you'd put that calculation inside a <th> rather than just in a code block. So I'm guessing you actually wanted to output that as a row number on the screen.

This will fix those little issues and the code should work nicely:

for ($i=0; $i < count($resultats); $i  ){ ?>
    <tr>
      <th scope="row"><?php echo $i   1 ; ?></th>
      <?php foreach ($resultats[$i] as $contenu) { ?>
      <td><?= $contenu ; ?></td>
      <?php } ?>
    </tr>
<?php } ?>

Live demo: http://sandbox.onlinephpfunctions.com/code/6d214f826ec35e959d8420fcf3d95726557f3637

  • Related