P
P
pudz2014-03-30 17:52:50
PHP
pudz, 2014-03-30 17:52:50

How to format a table based on data from an array?

I have the following array:

Array
(
    [0] => Array
        (
            [use_id] => 1
            [use_login] => admin
            [pro_img] => foto.jpg
            [pro_fam] => Левочкин
            [pro_im] => Владимир
            [pro_otch] => Генадиевич
            [dol_name] => И.о. Профессора
        )

    [1] => Array
        (
            [use_id] => 2
            [use_login] => user
            [pro_img] => foto.jpg
            [pro_fam] => Гаврилов
            [pro_im] => Валерий
            [pro_otch] => Георгиевич
            [dol_name] => Доцент
        )

    [2] => Array
        (
            [use_id] => 5
            [use_login] => buhar
            [pro_img] => foto.png
            [pro_fam] => Бухарест
            [pro_im] => Сергей
            [pro_otch] => Константинович
            [dol_name] => Ассистент
        )

    [3] => Array
        (
            [use_id] => 6
            [use_login] => baytenovalm
            [pro_img] => foto.png
            [pro_fam] => Байтенова
            [pro_im] => Лаура
            [pro_otch] => Маратовна
            [dol_name] => Зав. кафедрой
        )

    [4] => Array
        (
            [use_id] => 9
            [use_login] => glebov
            [pro_img] => foto.jpg
            [pro_fam] => Глебов
            [pro_im] => Валентинович
            [pro_otch] => Валентин
            [dol_name] => Ассистент
        )

)

You need to generate a table like this:
<table>
  <!-- Первая строка, 4 столбца -->
  <tr>
    <td><img src="images/foto.jpg"></td>
    <td><img src="images/foto.jpg"></td>
    <td><img src="images/foto.jpg"></td>
    <td><img src="images/foto.jpg"></td>
  </tr>
  
  <!-- Вторая строка, 4 столбца -->
  <tr>
    <td>
      <p>Левочкин Владимир Генадиевич</p>
      <p>И.о. Профессора</p>
    </td>
    <td>
      <p>Гаврилов Валерий Георгиевич</p>
      <p>Доцент</p>
    </td>
    <td>
      <p>Бухарест Сергей Константинович</p>
      <p>Ассистент</p>
    </td>
    <td>
      <p>Байтенова Лаура Маратовна</p>
      <p>Зав. кафедрой</p>
    </td>
  </tr>
  
  <!-- Третья строка, 4 столбца -->
  <tr>
    <td><img src="images/foto.jpg"></td>
  </tr>
  
  <!-- Четвертая строка, 4 столбца -->
  <tr>
    <td>
      <p>Глебов Валентинович Валентин</p>
      <p>Ассистент</p>
    </td>
  </tr>
</table>

Approximate view of the table:
382914d1396189253
This is the thing I bungled up and many similar options that do not work properly:
<table>
  <? for($i=0; $i<=1; $i++) :?>
    <? if($i == 0) :?>
    <tr>
      <? for($j=0, $line=1; $j<=count($profile)-1; $j++, $line++) :?>
        <? if($line != 5) :?>
          <td><img src="<?=SITE_URL;?>files/profile/<?=$profile[$j]['use_login']."/".$profile[$j]['pro_img'];?>"></td>
        <? else:?>
          <?$line = 0;?>
        <? endif;?>
      <? endfor;?>
    </tr>
    <? elseif($i == 1) :?>
    <tr>
      <? for($j=0, $line=1; $j<=count($profile)-1; $j++, $line++) :?>
        <? if($line != 5) :?>
          <td>									
            <p><a href="<?=SITE_URL;?>profile/user/<?=$profile[$j]['use_id'];?>"><?=$profile[$j]['pro_fam']." ".$profile[$j]['pro_im']." ".$profile[$j]['pro_otch'];?></a></p>	
            <p><?=$profile[$j]['dol_name'];?></p>									
          </td>
        <? else:?>
          <?$line = 0;?>
        <? endif;?>
      <? endfor;?>
    </tr>
                
    <? endif;?>
  <? endfor;?>


</table>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2014-03-30
@pudz

<?php
$rowblock = -2;
$rows = array();
foreach ($profile as $i => $user) {
    if ($i%4 == 0) {
        $rows[] = '<tr>';
        $rows[] = '<tr>';
        $rowblock += 2;
    }
    $rows[$rowblock] .= "<td><img src='/profile/user/{$user['pro_img']}'>";
    $rows[$rowblock+1] .= "<td><p>{$user['pro_fam']} {$user['pro_im']} {$user['pro_otch']}<p>{$user['dol_name']}";
}
for ($i = $i%4; $i < 3; $i++) {
    $rows[$rowblock] .= "<td>";
    $rows[$rowblock+1] .= "<td>";
}
echo "<table>",implode('', $rows),"</table>";
?>

V
Vampiro, 2014-03-30
@Vampiro

Yes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question