Seuss
Seuss

Reputation: 77

PHP MySQL vertical table

I have a horizontal table using PHP and MySQL now

table

How can I make a vertical table from this code?

<div class="content-loader">
  <table cellspacing="0" width="100%" id="rank2" class="table table-striped table-hover table-responsive">
    <thead>
      <tr>
        <th>Nick</th>
        <th>Kredity</th>
        <th>Body1</th>
        <th>Body2</th>
        <th>Cas</th>
        <th>online</th>
      </tr>
    </thead>
    <tbody>
      <?php
      require_once 'dbconfig.php';
      $stmt = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance
        FROM ranks
        INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId
        LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId
        WHERE ranks.steamId = ?");
      $stmt->execute(array($steamprofile['steamid']));

      while($row = $stmt->fetch(PDO::FETCH_ASSOC))
      {
          echo "<td>". $row['lastDisplayName']."</td><td>". $row['balance'] ."</td><td>". $row['points'] ."</td><td>". $row['points2'] ."</td><td>". $row['points2'] ."</td>";
      }
      ?>
    </tbody>
  </table>
</div>

Upvotes: 1

Views: 1887

Answers (2)

Xorifelse
Xorifelse

Reputation: 7911

When generating tables, fetch() works on a row by row basis, works very well for horizontally printed tables. But in your case its better to fetchAll() the data before printing it out:

<?php

  function unite(string $prefix, string $suffix, array $array){
    $str = '';
    foreach($array as $value){
      $str.= $prefix . $value . $suffix;
    }

    return $str;
  }

  if($stmt->execute(array($steamprofile['steamid']))){
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  } else {
    die('query failed');
  }

?>

<table>
  <tbody>
    <tr>
      <th>Nick</th><?php echo unite('<td>', '</td>', array_column($rows, 'lastDisplayName')) ?>
    </tr>
    <tr>
      <th>Kredity</th><?php echo unite('<td>', '</td>', array_column($rows, 'balance')) ?>
    </tr>    
  </tbody>
</table>

This way you can grab columns and print them out in 1 go. If you're not expecting any more columns than 1, you can also simply do the following:

<?php

  if($stmt->execute(array($steamprofile['steamid']))){
    if(!is_array($row = $stmt->fetch(PDO::FETCH_ASSOC))){
      die('no results');
    }
  } else {
    die('query failed');
  }

?>

<tr>
  <th>Nick</th><td><?php echo $row['lastDisplayName'] ?></td>
</tr>

Upvotes: 1

Vincent Dapiton
Vincent Dapiton

Reputation: 587

You could try this one

Just copy and paste the prepare and change the variables, same goes to execute

<div class="content-loader">

<?php
  require_once 'dbconfig.php';
  $stmt1 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
  $stmt2 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
  $stmt3 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
  $stmt4 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");
  $stmt5 = $db_con->prepare("SELECT ranks.steamId, ranks.points, ranks.lastDisplayName, ranks.lastUpdated, ranksrussia2.points AS points2, uconomy.balance FROM ranks INNER JOIN ranksrussia2 ON ranks.steamId = ranksrussia2.steamId LEFT JOIN uconomy ON ranks.steamId = uconomy.steamId WHERE ranks.steamId = ?");

  $stmt1->execute(array($steamprofile['steamid']));
  $stmt2->execute(array($steamprofile['steamid']));
  $stmt3->execute(array($steamprofile['steamid']));
  $stmt4->execute(array($steamprofile['steamid']));
  $stmt5->execute(array($steamprofile['steamid']));

  ?>

<table cellspacing="0" width="100%" id="rank2" class="table table-striped table-hover table-responsive">
<thead>
<tr>
<td>Nick</td>
<?php 
while($row = $stmt1->fetch(PDO::FETCH_ASSOC))
  {
      echo "<td>". $row['lastDisplayName']."</td>";
  }
 ?>
</tr>
<tr>
<td>Kredity</td>
<?php 
while($row = $stmt2->fetch(PDO::FETCH_ASSOC))
  {
      echo "<td>". $row['balance'] ."</td>";
  }
 ?>

</tr>
<tr>
<td>Body1</td>
<?php 
while($row = $stmt3->fetch(PDO::FETCH_ASSOC))
  {
      echo "<td>". $row['points'] ."</td>";
  }
 ?>

</tr>
<tr>
<td>Body2</td>
 <?php 
while($row = $stmt4->fetch(PDO::FETCH_ASSOC))
  {
      echo "<td>". $row['points2'] ."</td>";
  }

 ?>


</tr>
<tr>
<td>Cas</td>
 <?php 
while($row = $stmt5->fetch(PDO::FETCH_ASSOC))
  {
      echo "<td>". $row['points2'] ."</td>";
  }

 ?>

</tr>
<tr>
<td>Online</td>

</tr>
</thead>
<tbody>

</tbody>
</table>

</div>

Upvotes: 0

Related Questions