将ouptut的mysql数据格式化为表格 [英] formatting mysql data for ouptut into a table

查看:78
本文介绍了将ouptut的mysql数据格式化为表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据今天早些时候的一个问题,给出此答案以将数据读入数组并将其分离以打印车辆类型,然后打印每个车辆的一些数据。

Following on from a question earlier today this answer was given to read the data into an array and separate it to print vehicle type and then some data for each vehicle.

<?php
$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;    
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
  <?php foreach ($rows as $vehicleData):?>
    <li><?php echo $vehicleData['name'];?></li>
  <?php endforeach ?>
  </ul>
<?php endforeach ?>

这对于我想做的事情几乎是完美的,但是我需要从数据库中打印出两列即福特和蒙迪欧,然后进入第二个foreach循环。我试过打印$ rows ['model']和所有我能想到的其他组合,但是那是行不通的。非常感谢

This is almost perfect for what I want to do but I need to print out two columns from the database ie ford and mondeo before going into the second foreach loop. I've tried print $rows['model'] and all the other combinations I can think of but that doesn't work. Any help much appreciated

推荐答案

您的问题让我有些困惑,但是SQL语句中的SELECT *表示每列来自数据库的数据应该在$ row数组中作为键=>值对出现。因此,如果您需要另一个列,将其输出到HTML列表元素< li> 中,则只需将该列名作为回显即可(注意:不是 print)数组键。因此,如果您需要在名称为 model的列中找到的car列的类型,请执行以下操作:

I'm a bit confused by your question but the SELECT * in the SQL statement means that every column from the database should be present as a key=>value pair in the $row array. So if you needed another "column," output here into an HTML list element <li>, you just echo (note: not "print") that column name as an array key. So if you needed the type of the car column found in a column with the name "model" you'd do this:

<?php
$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;    
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
  <?php foreach ($rows as $vehicleData):?>
    <li><?php echo $vehicleData['name'];?></li>
    <li><?php echo $vehicleData['model'];?></li>
  <?php endforeach ?>
  </ul>
<?php endforeach ?>

编辑:我仍不清楚您的问题,但是否汽车具有相同的vehicleType,在循环浏览所有结果之前,您只是想抓住它一次,我想这样做会做到:

EDIT: I'm still unclear on your question, but if every car has the same vehicleType, and you're just looking to grab that once before looping through all the results, I'm guessing this will do it:

<?php
// Set up a SQL query to grab one row
$query_to_grab_only_vehicle_type = "SELECT vehicleType FROM apparatus WHERE 1 LIMIT 0,1";
// Fetch that row and turn it into an array
$vehicle_type_array = mysql_fetch_array(mysql_query($query_to_grab_only_vehicle_type));
// Initialize a variable with the array value that came from the vehicleType column
$vehicle_type = $vehicle_type_array['vehicleType'];
// You could uncomment and use the following to echo out the variable
// echo "Vehicle type: $vehicle_type";

$sql = "SELECT * FROM apparatus ORDER BY vehicleType";
$getSQL = mysql_query($sql);
// transform the result set:
$data = array();
while ($row = mysql_fetch_assoc($getSQL)) {
$data[$row['vehicleType']][] = $row;    
}
?>
<?php foreach ($data as $type => $rows): ?>
<h2><?php echo $type?></h2>
<ul>
  <?php foreach ($rows as $vehicleData):?>
    <li><?php echo $vehicleData['name'];?></li>
    <li><?php echo $vehicleData['model'];?></li>
  <?php endforeach ?>
  </ul>
<?php endforeach ?>

这篇关于将ouptut的mysql数据格式化为表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆