1

So I try to retrieve all data from user table and put it on Array.

This is the Array output :

Array ( [error] => false 
  [users] => Array ( 
    [0] => Array ( 
      [user_id] => 34
      [name] => test1
      [status] => actived ) 
    [1] => Array ( 
      [user_id] => 35
      [name] => test2
      [status] => actived ) 
  )
)

And this my table code in HTML :

<table class="table table-bordered table-striped table-condensed">
  <thead>
    <tr>
      <th>Code</th>
      <th>Company</th>
      <th class="numeric">ID</th>
      <th class="numeric">Name</th>
      <th class="numeric">Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="numeric"><?php ID VALUE HERE ?></td>
      <td class="numeric"><?php NAME VALUE HERE ?></td>
      <td class="numeric"><?php STATUS VALUE HERE ?></td>
    </tr>
  </tbody>
</table>

I already try foreach like this :

foreach ($error as $key => $val) {

}

and put the print_r ($val[0]['name']); into the NAME VALUE. but it just make the table is worst.

How to put each array value into that table ?

What is the best way to do that ? Thanks :)

2
  • 1
    I think a foreach loop is the way to go Commented Nov 18, 2014 at 10:21
  • already solved thanks guys :) Commented Nov 18, 2014 at 10:47

6 Answers 6

1

you can use like this

 $arr = Array ( [error] => false 
                [users] => Array ( 
                      [0] => Array ( 
                                    [user_id] => 34
                                    [name] => test1
                                    [status] => actived ) 
                      [1] => Array ( 
                                    [user_id] => 35
                                    [name] => test2
                                    [status] => actived ) 
                    ) 
              );

 <table class="table table-bordered table-striped table-condensed">
     <thead>
       <tr>
           <th>Code</th>
           <th>Company</th>
           <th class="numeric">ID</th>
           <th class="numeric">Name</th>
           <th class="numeric">Status</th>
        </tr>
       </thead>
       <tbody>
        <?php foreach($arr['users'] as $user): ?>
         <tr>
           <td class="numeric"><?php echo $user['user_id'] ?></td>
           <td class="numeric"><?php echo $user['name'] ?></td>
           <td class="numeric"><?php echo $user['status'] ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
</table>

this will give you the exact output.

Sign up to request clarification or add additional context in comments.

Comments

1

foreach()

Try this: assuming variable name variable

<tbody>
    <tr>
<?php
foreach($variable['users'] as $user)
{ ?>
  <td class="numeric"><?php echo $user['user_id'] ?></td>
      <td class="numeric"><?php echo $user['name'] ?></td>
      <td class="numeric"><?php echo $user['status'] ?></td
<?php }?>
</tr>
  </tbody>

Comments

1
$data = Array ( [error] => false 
    [users] => Array ( 
                      [0] => Array ( 
                                    [user_id] => 34
                                    [name] => test1
                                    [status] => actived ) 
                      [1] => Array ( 
                                    [user_id] => 35
                                    [name] => test2
                                    [status] => actived ) 
                    ) );


<table class="table table-bordered table-striped table-condensed">
     <thead>
       <tr>
           <th>Code</th>
           <th>Company</th>
           <th class="numeric">ID</th>
           <th class="numeric">Name</th>
           <th class="numeric">Status</th>
        </tr>
       </thead>
       <tbody>
<?php foreach($data['users'] as $user):?>
         <tr>
           <td class="numeric"><?php echo $user['user_id']; ?></td>
           <td class="numeric"><?php echo $user['name']; ?></td>
           <td class="numeric"><?php echo $user['status']; ?></td>
          </tr>
<?php endforeach;?>
        </tbody>
</table>

Comments

1
<table class="table table-bordered table-striped table-condensed">
  <thead>
    <tr>
      <th>Code</th>
      <th>Company</th>
      <th class="numeric">ID</th>
      <th class="numeric">Name</th>
      <th class="numeric">Status</th>
    </tr>
  </thead>
  <tbody>
    <?php 
    foreach($array['user'] as $user) {
        ?>
        <tr>
          <td class="numeric"><?php echo  $user['user_id'] ?></td>
          <td class="numeric"><?php echo  $user['name'] ?></td>
          <td class="numeric"><?php echo  $user['status'] ?></td>
        </tr>   
        <?php 
    }
    ?>
  </tbody>
</table>

Comments

0

Try below code:

<?php foreach($arrayData['users'] as $data){ ?>
     <td class="numeric"><?php echo $data['id']; ?></td>
      }

Comments

0
<table class="table table-bordered table-striped table-condensed">
  <thead>
    <tr>
      <th>Code</th>
      <th>Company</th>
      <th class="numeric">ID</th>
      <th class="numeric">Name</th>
      <th class="numeric">Status</th>
    </tr>
  </thead>
  <tbody>
  <?php
  foreach($my_array['users'] as $value){
    echo '<tr>'.chr(13);
    echo '  <td class="numeric">'.$value['user_id'].'</td>'.chr(13);
    echo '  <td class="numeric">'.$value['name'].'</td>'.chr(13);
    echo '  <td class="numeric">'.$value['status'].'</td>'.chr(13);
    echo '</tr>'.chr(13);
  }
  ?>
  </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.