3

I am just learning PHP ,and I want to create a table that display echo data that I submit to my database , the problem I have that the table displayed horizontally by default as you see Horizontal default table this my script

<table >
<tr>
<th>Name</th>
<th>Age</th>
<th>Height</th>
</tr>
<?php
$conn = mysqli_connect("localhost", "root", "", "class");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Name, Age, Height FROM student order by Name desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Name"]. "</td><td>" . $row["Age"] . "</td><td>"
. $row["Height"]. "</td></tr>";
}
echo "</table>";
} else //{ echo "0 results"; }//
$conn->close();
?>
</table>

but I want it to be echoed vertically instead like this VERTICAL RESULT I WANT and I tried to change html in echo in my PHP code but I can't get the result at all and the shape of the table is far away from what I want and this is the full script of my page .

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
zak leon
  • 43
  • 3
  • Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman May 30 '20 at 12:09

3 Answers3

3

Like everyone else said, you should convert your horizontal array into a vertical one.

Of course it should be a universal function to convert any query result, as opposed to hardcoding the row headings. The idea is to get each row's array keys and use them as the keys for the new array and then add each corresponding value to the new array item.

Here is how it's done in mysqli:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect('127.0.0.1','root','','test');
$mysqli->query("set names 'UTF8'");

$data = [];
$res = $mysqli->query("SELECT Name, Age, Height FROM student order by Name desc");
while ($row = $res->fetch_assoc()) {
    foreach(array_keys($row) as $key) {
        $data[$key][] = $row[$key];
    }
}

and then you get an array with desired structure which you can output using the code from ROOT's answer:

<table border="1">
  <?php foreach($data as $key => $val): ?>
    <tr>
      <td><?= $key ?></td>
      <?php foreach($val as $field): ?>
        <td><?= $field ?></td>
      <?php endforeach ?>
    </tr>
  <?php endforeach ?>
</table>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Thank you for your great help .I want to name each table manually using html to the second part of the code [table name](https://imgur.com/a/FN2qX3l) – zak leon May 30 '20 at 18:02
1

I mocked your data into normal array, I used a while loop to create a new Array and create the format that we need to be able to flip the columns into rows, here is what I think you want:

<?php
$users  = [
  [
    'name'=> 'James',
    'height'=> 1.75,
    'age'=> 18,
  ],
  [
    'name'=> 'Bill',
    'height'=> 170,
    'age'=> 16,
  ]
];


$newArr = [];

foreach($users as $key => $val) {
  $newArr['name'][$i] = $val['name'];
  $newArr['age'][$i] = $val['age'];
  $newArr['height'][$i] = $val['height'];
  $i++;
}


?>
<table border="1">
  <?php foreach($newArr as $key => $val): ?>
    <tr>
      <td><?php echo $key; ?></td>
      <?php foreach($val as $field): ?>
        <td><?php echo $field; ?></td>
      <?php endforeach; ?>
    </tr>
  <?php endforeach ?>
</table>
ROOT
  • 11,363
  • 5
  • 30
  • 45
  • 1
    FYI, foreach is considered much more convenient loop than four separate operators: count, counter, while and increment – Your Common Sense May 30 '20 at 13:15
  • @YourCommonSense, I agree, thanks for pointing that out. its been 5 years since I did anything in php, almost forgot it :P – ROOT May 30 '20 at 13:21
0

It's not a good idea ! if you have a lot of ROW you will generate a long table not visible for user in screen.

if you want to do it after all you can change table structure but you will not respect html table structure.

I will give you a

Dirty code

<table>
<tr>
<th>Name</th>
<?php foreach ($row as $value) { ?><td><?php echo$value["Name"];  ?></td>
<?php } ?>
</tr>

<tr>
<th>Age</th>
<?php foreach ($row as $value) { ?>
    <td><?php echo $value["Age"];  ?></td>
<?php } ?>
</tr>

<tr>
<th>Height</th>
<?php foreach ($row as $value) { ?>
    <td><?php echo $value["Height"];  ?></td>
<?php } ?>
</tr>
</table>

I recommand to USE CSS instead a table