0

I need help, I am at a loss to understand what about array and loops "for". I have to display the column on the right cell.

The answer of my query. note, I have 13 array like this:

print_r ($toto) = 

Array ( [0] => 

   Array ( [id_depot] => 21758777 
           [code_stp] => CHI 
           [date_depot_const] => 20/11/18 
           [date_depot_decla] => 
           [num_bip] => 0100514871
           [num_depot] => AB421743 
           [typ_depot] => 51004 
           [typ_declaration] => ) 

My html:

 <table>
 <tr>
 <th>ID_DEPOT</th>
 <th>CODE_STP</th> 
 <th>DATE_DEPOT_CONST</th>
 <th>DATE_DEPOT_DECLA</th>
 <th>NUM_BIP</th>
 <th>NUM_DEPOT</th>
 <th>TYP_DEPOT</th>
 <th>TYP_DECLARATION</th>
 </tr>
<?php 
 $i = 0;
 for ($i=0 ;$i <= count($this->toto);$i++){
 echo '<tr>';
 echo '<td>'.$this->toto['id_depot'].'</td>';
 echo '<td>'.$this->toto['code_stp'].'</td>';
 echo '<td>'.$this->toto['date_depot_const'].'</td>';
 echo '<td>Jill</td>';
 echo '<td>Smith</td>';
 echo '<td>50</td>';
 echo '<td>Jill</td>';
 echo '<td>Smith</td>';
 echo '</tr>';
 }   
?>

Of course i have to fill all the column but currently I fill only 3 for the example

The answer :

enter image description here

My first three column are empty and I don't know why :( it should display my array... Do you have an idea?

2
  • echo '<td>'.$this->toto[$i]['id_depot'].'</td>'; Commented Nov 27, 2018 at 14:50
  • If you have an array of arrays, may I ask why using for instead of foreach? Commented Nov 27, 2018 at 15:37

3 Answers 3

5

You are not using the index.

$this->toto['id_depot']

Should be

$this->toto[$i]['id_depot']

And you need to fix the for loop, <= should be only <:

 for ($i=0 ;$i < count($this->toto);$i++){
Sign up to request clarification or add additional context in comments.

Comments

1

Your arrays are in an array... you need to use [$i] as it is the first array with an index of [0] that you are displaying to target that array, then the key you require:

<table>
 <tr>
 <th>ID_DEPOT</th>
 <th>CODE_STP</th> 
 <th>DATE_DEPOT_CONST</th>
 <th>DATE_DEPOT_DECLA</th>
 <th>NUM_BIP</th>
 <th>NUM_DEPOT</th>
 <th>TYP_DEPOT</th>
 <th>TYP_DECLARATION</th>
 </tr>
<?php 
 $i = 0;
 for ($i=0 ;$i <= count($this->toto);$i++){
 echo '<tr>';
 echo '<td>'.$this->toto[$i]['id_depot'].'</td>';
 echo '<td>'.$this->toto[$i]['code_stp'].'</td>';
 echo '<td>'.$this->toto[$i]['date_depot_const'].'</td>';
 echo '<td>Jill</td>';
 echo '<td>Smith</td>';
 echo '<td>50</td>';
 echo '<td>Jill</td>';
 echo '<td>Smith</td>';
 echo '</tr>';
 }   
?>

As you start your loop from zero, you don't need to count all the way up to your array count as that would be one to many.

Comments

1

I understand there is an answer, just though of providing a different approach :)

foreach ($this->toto as $key => $toto) {
     echo sprintf(
             '<tr><td>%d</td>%s<td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>',
               $toto['id_depot'],
               $toto['code_stp'],
               $toto['date_depot_const'],
               `Jill`,
               `Smith`,
               50,
               `Jill`,
               `Smith`
          );
 }

Think the example above will save you headaches and organize your script a bit more :) For the remaining hard-coded examples you have, all you need is to replace for the correct sprintf value and target the desired key from your array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.