我正在尝试从数据库加载数据并将其显示在表中,如下所示:
http://img196.imageshack.us/img196/1857/cijene.jpg
在数据库中,我有两个表:
cities (id, name)
prices (id, city1_id, city2_id, price)
例如,4个城市的打印表格如下所示:
<table>
<tr>
<td>city1</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> price_city1-city2</td>
<td>city2</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> price_city1-city3</td>
<td> price_city2-city3</td>
<td>city3</td>
<td> </td>
</tr>
<tr>
<td> price_city1-city4</td>
<td> price_city2-city4</td>
<td> price_city3-city4</td>
<td>city4</td>
</tr>
</table>
有人知道回复这种表的PHP语句是什么?
答案 0 :(得分:1)
我使用了一种非常简单的方法将查询结果转换为HTML表格。 我测试$ query_result为true并将结果作为关联数组获取...
$query_result = mysqli_query($db, $query);
if ($query_result) {
while ($columns = mysqli_fetch_assoc($query_result)) {
echo "<tr>\n";
foreach ($columns as $name => $column) {
echo "<td>";
printf("%s",$columns[$name]);
echo "</td>\n";
}
echo "</tr>\n";
}
}
编辑:现在我已经能够看到你的桌子,我可以看到我没有真正回答你的问题。查询显然非常重要。这个问题是否成为“我如何创建一个返回结果的查询,这些结果可以通过我的简单方法转化为表格?”
我希望别人有一些想法。
答案 1 :(得分:1)
这有点处于实用性的边缘,但我更倾向于将这个问题作为“正确行事”的练习。
我会对每个表做一个简单的选择并插入一个数组:
$query1 = "SELECT * FROM cities"; $query2 = "SELECT * FROM prices"; $results1 = mysql_query( $query1 ); $results2 = mysql_query( $query2 ); while( $rows = mysql_fetch_array( $results1 ) ) $cities[] = $row['name']; while( $rows = mysql_fetch_array( $results2 ) ) $prices[] = array( $row['city1_id'], $row['city2_id'], $row['name'] );
然后我会用它来动态创建两个javascript列表:
$java_array1 = "var Cities = new Array;\n"; foreach( $cities as $key=>$city ) $java_array1 .= "Cities[$key] = \"$city\";\n"; $java_array2 = "var Prices = new Array;\n"; foreach( $cities as $key=>$price ) $java_array2 .= "Prices[$key] = new Array( \"{$price[0]}\", \"{$price[1]}\", \"{$price[2]}\", );\n";
我接下来会为每个单元格输出一个包含精心设计的ID的表格。我会使用与第一个答案非常相似的代码,但我会给每个单元格一个唯一的"cell-<row>-<col>"
ID。
我要做的最后一件事就是掀起一些onload
javascript,它会使用适当的配对填充表格。我的javascript很生锈,但它看起来像这样(注意:伪代码如下):
n = 1; for( var i in cities ) { // set city names on the diagonal document.getElementById( 'cell-'&i&'-'&i ).innerHTML = names[i]; n = n + 1; } for( var j in prices ) { // use the city IDs in the prices array (see earlier code initializing // prices array) to place the prices document.getElementById( 'cell-'&prices[1]&'-'&prices[2] ).innerHTML = prices[0]; }
我几乎肯定搞砸了一些javascript;使用风险自负。
这比上面给出的更简单的答案要复杂得多,但这是我能想到做到这一点的唯一方法,这样才有意义。基本上,使用这种方法,你可以放置应该所在的所有内容,而且你不必担心奇数表和奇怪的选择。
答案 2 :(得分:1)
// This single query gets all required data
// NOTE: this query assumes that your price data is entered
// such that from_city always alphabetically precedes to_city!
$sql =
"SELECT a.name AS from_city, b.name AS to_city, price ".
"FROM prices ".
"INNER JOIN cities AS a ON a.id=prices.city1_id ".
"INNER JOIN cities AS b ON b.id=prices.city2_id";
// connect and do query
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db, $conn);
$q = mysql_query($sql, $conn);
// Stuff all data into an array for manipulation;
// keep list of all cities mentioned
$price = array();
$cities = array();
while (($res = mysql_fetch_assoc($q)) !== false) {
$from = $res['from_city'];
$cities[ $from ] = true;
$to = $res['to_city'];
$cities[ $to ] = true;
$price[$to][$from] = $res['price'];
}
// Invert to get alphabetical list of cities
$cities = array_keys($cities);
sort($cities);
$num = count($cities);
// some utility functions
function table($rows) {
return '<table>'.join($rows).'</table>';
}
function row($cells) {
return '<tr>'.join($cells).'</tr>';
}
function cell($text) {
$text = (string) $text;
if (strlen($text)==0)
$text = ' ';
return '<td>'.$text.'</td>';
}
// The data is now in the desired order;
// produce output as HTML table
$rows = array();
for ($to = 0; $to < $num; ++$to) {
$t = $cities[$to];
$cells = array();
for ($from = 0; $from < $to; ++$from) {
$f = $cities[$from];
if isset($price[$t]) && isset($price[$t][$f])
$text = $price[$t][$f];
else
$text = '';
$cells[]= cell($text);
}
$cells[]= cell($t); // add destination-label
for ($from = $to+1; $from < $num; ++$from) // pad to proper number of cells
$cells[]= cell('');
$rows[]= row($cells);
}
$html = table($rows);
// show results!
echo $html;