dousi2029 2019-06-04 14:56
浏览 84
已采纳

在PHP中将MySQL输出显示为行而不是列

Good afternoon all :)

I would like to display my MySQL output on a PHP page as rows rather than columns for easier mobile viewing and scrolling (so the user can just scroll down the data instead of across).

I'd read about pivots and transposing but wasn't sure what was the most appropriate way to transform the return data output on the webpage. Please can you advise on what is best to use? It looks like it's easier to do it in PHP rather than MySQL?

I'm using a standard post form, connection PDO, isset, thead, php echo td and th etc.

My current code:

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="x-ua-compatible" content="ie=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <title></title>

    <link rel="stylesheet" href="css/style.css" />
  </head>

<body>

<h1>Find people</h1>

    <form method="post">
        <label for="people">Enter person</label>
        <input type="text" id="people" name="people">
        <input type="submit" name="submit" value="View Results">

    </form>

<?php

//error_reporting(-1);
//ini_set('display_errors', 'On');

if (isset($_POST['submit'])) {
  try {
    require "./config.php";
    require "./common.php";

    $connection = new PDO($dsn, $username, $password, $options);

    $sql = "SELECT *
    FROM Basics
    WHERE UniqueID = :people";

    $people = $_POST['people'];

    $statement = $connection->prepare($sql);
    $statement->bindParam(':people', $people, PDO::PARAM_STR);
    $statement->execute();

    $result = $statement->fetchAll();
  } catch(PDOException $error) {
    echo $sql . "<br>" . $error->getMessage();
  }
}
?>


<?php
if (isset($_POST['submit'])) {
  if ($result && $statement->rowCount() > 0) { ?>
    <h2>Results</h2>

    <table>
<?php echo '<table border="1">';?>
      <thead>
<tr>
  <th>Field 1</th>
  <th>Field 2</th>
  <th>Field 3</th>
</tr>
      </thead>
      <tbody>
  <?php foreach ($result as $row) { ?>
      <tr>
<td><?php echo escape($row["Field 1"]); ?></td>
<td><?php echo escape($row["Field 2"]); ?></td>
<td><?php echo escape($row["Field 3"]); ?></td>


      </tr>
    <?php } ?>
      </tbody>
  </table>
  <?php } else { ?>
     <br>No results found for <?php echo escape($_POST['people']); ?>, as the data has likely not been added yet.
  <?php }
} ?>

<!--
<?php if (isset($_POST['submit']) && $statement) { ?>
  <?php echo escape($_POST['people']); ?> successfully found.
<?php } ?>
-->

  </body>
</html>

My current output:

enter image description here

Current example output:

enter image description here

Output example I would like:

enter image description here

Similar to this example I found (can be in a table or not like below):

enter image description here

Update edit:

Looks like I just needed to use ul and li!

<ul>
<li><b>Name:</b> <?php echo escape($row["Name"]); ?></li>
</ul>
  • 写回答

2条回答 默认 最新

  • dongpiansui8755 2019-06-19 14:52
    关注

    Looks like I just needed to use ul and li!

    <ul>
    <li><b>Name:</b> <?php echo escape($row["Name"]); ?></li>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么