2014-11-18 2 views
1

Итак, я пытаюсь извлечь все данные из таблицы пользователей и поместить их в Array.PHP: как напечатать каждое значение массива в таблице

Это выходной массив:

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

И это мой стол код в 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> 

Я уже попробовать Еогеасп так:

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

} 

и поставить print_r ($val[0]['name']); в NAME VALUE. , но он просто делает стол хуже.

Как поместить каждое значение массива в эту таблицу?

Каков наилучший способ сделать это? Спасибо :)

+1

Я думаю, что петля Еогеасп это путь –

+0

уже решенные спасибо, ребята :) – Monk

ответ

1

вы можете использовать, как это

$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> 

это даст вам точный результат.

1

foreach()

Попробуйте это: Предположим, что имя переменной переменной

<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> 
0

Попробуйте ниже код:

<?php foreach($arrayData['users'] as $data){ ?> 
    <td class="numeric"><?php echo $data['id']; ?></td> 
     } 
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> 
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> 
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> 
Смежные вопросы