[PHP][JSON][ARRAY] How to show array vertical instead of horizontal

Skythrust

Member
Jul 9, 2019
133
7
Hi there,

Since I am busy to create a new project I was wondering if there are possibilities to show an array vertical like the example below

Code:
-- Horizontal view --
John Doe, Jane Doe, Jhon Doe

-- Vertical view --
John Doe
Jane Doe
Jhon Doe

This is the code which I am using now;

PHP:
<?php
    $i =0;
    echo "<table><tr>";
    foreach($JsonstringPersons as $record)
    {
                if ($record['Online'] == 1) { $APBAreadiv = 'TextOn'; $APBCheck = 'checked'; } else { $APBAreadiv = 'TextOff'; $APBCheck = 'unchecked'; }

                $i++;
                echo "<td><div class=".$APBAreadiv.">"
                .' '.$record['Firstname'] .' '. $record['Middlename'] .' '. $record['Lastname'];

                echo

            '</div>                         
            </td>';
        $i++;
        if ($i % 5 == 0) { echo '</tr><tr>'; }
        }                       
        echo '</table>';                                 
    }
?>
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
You're looking for a HTML table, some reading here:

For example
HTML:
<?php
$names = ["John Doe", "Jane Doe", "Jhon Doe"];
?>

<table>
  <thead>
    <tr>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <?php
    foreach($names as $n){
    ?>
    <tr>
      <td><?php echo $n; ?></td>
    </tr>
     <?php } ?>
  </tbody>
</table>
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
I can understand that, but is that also possible with me code?
Well it's practically the same thing - you need to understand how tables work if you want the data aligned like that, something like this:
HTML:
<table>
  <thead>
    <tr>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <?php
    foreach($JsonstringPersons as $record){
    if ($record['Online'] == 1) { $APBAreadiv = 'TextOn'; $APBCheck = 'checked'; } else { $APBAreadiv = 'TextOff'; $APBCheck = 'unchecked'; }
    ?>
    <tr>
      <td><div class="<?php echo $APBAreadiv; ?>"><?php echo $record['Firstname'] .' '. $record['Middlename'] .' '. $record['Lastname']; ?></div></td>
    </tr>
     <?php } ?>
  </tbody>
</table>
 

oxcakmak

New Member
Apr 5, 2021
5
2
first Step:
PHP:
<?php
    $i =0;
    echo "<table>";
    foreach($JsonstringPersons as $record){
        $i++;
        if ($record['Online'] == 1) { $APBCheck = 'checked'; } else { $APBCheck = 'unchecked'; }
        echo '<tr><td><div class=".(($record['Online']==1)?'TextOn':'TextOff').">'.$record['Firstname'].'&nbsp;'.$record['Middlename'].'&nbsp;'.$record['Lastname'].'</div></td></tr>';
    }
    echo '</table>';
?>

second step:
PHP:
<?php
    $userData = array();
    foreach($JsonstringPersons as $record){ $userData[] = $record['Firstname'].'&nbsp;'.$record['Middlename'].'&nbsp;'.$record['Lastname']; }
    /* Usage */
    foreach($userData as $udatRow){ }
?>
 

Users who are viewing this thread

Top