寻求更好的 PHP 代码的帮助,使我能够生成带有子类别的类别列表,但将其打印在三列中。
首先是表数据(仅演示表-虽然表名和列名是正确的):
--- TABLE NAME = walkthroughs ---
---------------------------------------------------------------
| id | name | url | cluster |
---------------------------------------------------------------
| 1 | A Room with a View | http://... | Happyville |
| 2 | An Outdoor Tower | http://... | Happyville |
| 3 | An Old Cottage | http://... | Happyville |
| 4 | Town Hall | http://... | Misty Vale |
| 5 | Cathedral | http://... | Misty Vale |
| 6 | Babbling Brook | http://... | Old Forest |
| 7 | The Lonely Flower | http://... | Old Forest |
| 8 | The Hollow Tree | http://... | Old Forest |
| 9 | The Secret Garden | http://... | Old Forest |
| 10 | The Forgotten Corale | http://... | Open Plains |
| 11 | Echo Caverns | http://... | Mountains |
| 12 | The Forgotten Corale | http://... | Mountains |
---------------------------------------------------------------
我使用过创建“集群”类别的 PHP 代码,并在其下方列出了相关的名称和 url。这很乱,但可以完成工作!此代码的 HTML 输出都在 1 列中。
--- HTML OUTPUT ---
<h2>Happyville</h2>
<a href="url"> A Room with a View </a>
<a href="url"> An Outdoor Tower </a>
<a href="url"> An Old Cottage </a>
<br>
<h2>Misty Vale</h2>
<a href="url"> Town Hall </a>
<a href="url"> Cathedral </a>
<br>
<h2>Old Forest</h2>
<a href="url"> Babbling Brook </a>
<a href="url"> The Lonely Flower </a>
<a href="url"> The Hollow Tree </a>
<a href="url"> The Secret Garden </a>
<br>
<h2>Open Plains</h2>
<a href="url"> Babbling Brook </a>
<br>
<h2>Mountains</h2>
<a href="url"> Echo Caverns </a>
<a href="url"> The Forgotton Corale </a>
我正在寻求帮助的是 PHP for Mysql:
- 以便结果按“集群”字段分组,
- 首先打印 'cluster' 字段的唯一值,然后是该值的所有 'name' 和 'url' 值,
- 一个 PHP/Mysql 计数,可用于将结果分成 3 列。
最佳 HTML 输出如下所示。
--- HTML OUTPUT ---
<div id="col-1">
<h2>Happyville</div>
<a href="url"> A Room with a View </a>
<a href="url"> An Outdoor Tower </a>
<a href="url"> An Old Cottage </a>
<br>
<h2>Misty Vale</h2>
<a href="url"> Town Hall </a>
<a href="url"> Cathedral </a>
<br>
<div>
<div id="col-2">
<h2>Old Forest</h2>
<a href="url"> Babbling Brook </a>
<a href="url"> The Lonely Flower </a>
<a href="url"> The Hollow Tree </a>
<a href="url"> The Secret Garden </a>
<br>
<h2>Open Plains</h2>
<a href="url"> Babbling Brook </a>
<br>
</div>
<div id="col-3">
<h2>Mountains</h2>
<a href="url"> Echo Caverns </a>
<a href="url"> The Forgotton Corale </a>
<br>
</div>
这是我一直在使用的当前 PHP,我知道它很乱(例如,我不确定是否使用数组),虽然它可以将结果分为 3 列,但它会打印一个标题和第一个值,然后是任何进一步的结果出现在下一列中(或者根本没有,在第 3 列的情况下)。
可以在@http://freethedangler.com/test/walkthroughs/instances-test.php 找到该问题的一个活生生的例子(是的,这是一个令人讨厌的 MMO 页面!)。
请注意,“cluster”字段在实时示例中被命名为“cluster_or_campaigns”。我在这里简化了名称以使生活更轻松。
--- CURRENT PHP ---
<?php
include("../php/lotrodb.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$queryc="SELECT * FROM walkthroughs ORDER BY cluster ASC";
$queryd="SELECT cluster,COUNT(DISTINCT(cluster)) FROM walkthroughs GROUP BY cluster";
$resultc=mysql_query($queryc);
$resultd=mysql_query($queryd);
$numc=mysql_numrows($resultc);
$numd=mysql_numrows($resultd); // used to count the number of unique values for the 'cluster' field
mysql_close();
$i=0; //$i = counter used to count queryc values
$j=0; //$j = counter used to print out <h2>'cluster'</h2> heading
$x=0; //$x = counter used to count queryd values
$y=($numd/3); //$y = 1/3rd of $numd - used to close a column and start a new column
$z=$y*2; //$z = 2/3rds of $numd - used to close a column and start a new column
$cluster_current = null;
?>
<div id="col-1">
<?php
while ($x < $y) {
$name=mysql_result($resultc,$i,"name");
$url=mysql_result($resultc,$i,"url");
$cluster=mysql_result($resultc,$i,"cluster");
if ($cluster != $cluster_current) {
$x=$x+1;
?>
</p>
<br>
<h2><?php echo $cluster; ?> Cluster</h2>
<p>
<?php
}
?>
<a href="<?php echo $url; ?>" class="fade"><?php echo $name; ?></a><br>
<?php
$i=$i+1;
$cluster_current=mysql_result($resultc,$j,"cluster");
$j=$j+1;
}
?>
</div>
<div id="col-2">
<?php
while ($x < $z) {
$name=mysql_result($resultc,$i,"name");
$url=mysql_result($resultc,$i,"url");
$cluster=mysql_result($resultc,$i,"cluster");
if ($cluster != $cluster_current) {
$x=$x+1;
?>
</p>
<br>
<h2><?php echo $cluster; ?> Cluster</h2>
<p>
<?php
}
?>
<a href="<?php echo $url; ?>" class="fade"><?php echo $name; ?></a><br>
<?php
$i=$i+1;
$cluster_current=mysql_result($resultc,$j,"cluster");
$j=$j+1;
}
?>
</div>
<div id="col-3">
<?php
while ($x < $numd) {
$name=mysql_result($resultc,$i,"name");
$url=mysql_result($resultc,$i,"url");
$cluster=mysql_result($resultc,$i,"cluster");
if ($cluster != $cluster_current) {
$x=$x+1;
?>
</p>
<br>
<h2><?php echo $cluster; ?> Cluster</h2>
<p>
<?php
}
?>
<a href="<?php echo $url; ?>" class="fade"><?php echo $name; ?></a><br>
<?php
$i=$i+1;
$cluster_current=mysql_result($resultc,$j,"cluster");
$j=$j+1;
}
?>
</div>