PHP Mysql-使用子类别循环生成类别
正在寻求更好的PHP代码的帮助,该代码允许我生成带有子类别的类别列表,但要跨三列打印 首先,仅显示表数据演示表-尽管表名和列名正确:PHP Mysql-使用子类别循环生成类别,php,mysql,sql,categories,Php,Mysql,Sql,Categories,正在寻求更好的PHP代码的帮助,该代码允许我生成带有子类别的类别列表,但要跨三列打印 首先,仅显示表数据演示表-尽管表名和列名正确: --- TABLE NAME = walkthroughs --- --------------------------------------------------------------- | id | name | url | cluster | --------------------
--- 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的帮助:
因此,结果按“群集”字段分组,
首先打印“群集”字段的唯一值,然后打印该值的所有“名称”和“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列中
这个问题的一个活生生的例子是@yes,这是一个极客的MMO页面
请注意,在实时示例中,“集群”字段的名称为“集群”或“活动”。我在这里简化了名字,使生活更轻松
--- 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>
我对PHP不是很熟悉,但我知道关联数组在字典中存在。这足以解决你的问题 首先,我将使用and执行此查询: 选择群集, 组_CONCAT 海螺 AS块 来自演练 分组
检索完后,将查询响应放入dictionary cluster=>块中,然后您可以迭代整个集群,只需在页面的适当位置回显HTML块。我很欣赏这个方向。在查看PHP数组链接、一两个sql数组示例、使用您的查询并消除每个块结果之间的逗号后,问题已经解决。@user2901346如果我的答案对您有帮助,您可以将其标记为正确答案。